Modern Object Pascal Introduction for Programmers – Castle Game Engine
An Introduction to Modern Object Pascal for Developers
🎯 Purpose of this Guide
I wanted to outline the capabilities of modern Object Pascal—a language featuring classes, generics, interfaces, and units—to show how these elements integrate into a cohesive system. My goal was to create a resource that is both practical and succinct for experienced software engineers.
Because this is written for fellow programmers, I will skip the elementary definitions. For instance, I won't spend pages explaining what a variable is; I'll simply state that a variable is a named container for a value that can change over time and move forward.
Modern Pascal has evolved significantly. It is no longer the Turbo Pascal many remember from school. In terms of functionality, it is now comparable to languages like C#, Java, or C++.
Key Language Characteristics
- High Performance: Compiles directly to fast, native machine code.
- Type Safety: Extremely rigorous type checking.
- Versatility: High-level abstractions are available, but it can drop to low-level operations when necessary.
- Modern Toolset: Includes generics, interfaces, and object-oriented classes.
🛠️ The Ecosystem
Depending on your needs, there are several ways to develop in Pascal:
| Tool | Type | Description | Link |
|---|---|---|---|
| FPC | Compiler | The open-source Free Pascal Compiler. | freepascal.org |
| Lazarus | IDE | Open-source editor, debugger, and visual form designer. | lazarus.freepascal.org |
| Delphi | IDE/Compiler | A powerful commercial suite by Embarcadero. | embarcadero.com |
Additional Resources:
- Libraries: Check out the Awesome Pascal list on GitHub.
- Editor Support: Integration for
VS Codeis available via castle-engine.io/vscode. - Game Development: I created the Castle Game Engine, an open-source 2D/3D engine that targets Windows, Linux, macOS, Android, iOS, Nintendo Switch, and WebGL.

🚀 Your First Program: "Hello World"
Below is a standard entry-point for a modern FPC console application.
// Directives for modern FPC compatibility
{$ifdef FPC}
{$mode objfpc} // Use Object Pascal mode
{$H+} // Enable long strings
{$J-} // Disable range checking
{$endif}
// Ensure a console window appears on Windows
{$ifdef MSWINDOWS}
{$apptype CONSOLE}
{$endif}
program MyProgram;
begin
WriteLn('Hello world!');
end.
📋 Execution Checklist
- Using FPC CLI: Save as
myprogram.dprrunfpc myprogram.dpr. - Using Lazarus:
ProjectNew ProjectSimple ProgramCtrl + F9. - Using Delphi:
FileNewConsole ApplicationCtrl + F9.
Note: If the console window closes instantly when running from an IDE, add a Readln call at the end of the begin...end block to pause for user input.
🏗️ Compilation Workflow
⚙️ Syntax Modes and Compilers
This guide focuses on the ObjFpc syntax mode. While FPC supports multiple modes, ObjFpc is the standard for new projects in Lazarus and the Castle Game Engine.
It differs slightly from the Delphi mode (which prioritizes compatibility with Embarcadero's implementation), but these differences are minor. You will see conditional compilation blocks like {$ifdef FPC} ... {$endif} in examples to ensure the code works across both environments. If you are only using one compiler, you can safely remove these wrappers.
📦 Basic Constructs: Functions and Procedures
Pascal distinguishes between procedures (which perform actions) and functions (which return a value).
{$ifdef FPC} {$mode objfpc} {$H+} {$J-} {$endif}
{$ifdef MSWINDOWS} {$apptype CONSOLE} {$endif}
program MyProgram;
// A procedure does not return a value
procedure MyProcedure(const A: Integer);
begin
// Example of a simple calculation:
WriteLn('A + 10 is: ', A + 10);
end;
// A function returns a value of a specific type
function MyFunction(const S: string): string;
begin
Result := S + ' strings are automatically handled';
end;
In the example above, the operation performed in MyProcedure can be represented mathematically as:
While this guide focuses on command-line basics, Pascal is highly capable of creating complex GUIs. By selecting "Application" in Lazarus or "Multi-Device Application" in Delphi, you can leverage a massive library of visual components to build cross-platform software with a native look and feel. The standard libraries also provide robust support for:
- Networking and Threading
- Databases
- File formats (JSON, XML, Images)
- And, of course, the Castle Game Engine!