![]() |
ImagoEngine
A C++ 2D game engine built on a sparse-set ECS, with scenes, scheduled systems, events, and action-based input
|
Contains the logic that reads and changes component to make your game do things.
Use this when anything in your game needs to change over time: movement, timers, AI, input handling, scoring, ect.
Include: Imago/Engine/Scene.hpp
In ImagoEngine, all game logic lives in systems. Systems do not belong to any entity, class, or game object, but instead they loop over all the entities that they care about (usually with a Survey), and execute game logic by reading and changing their component data.
There are two main types of systems: subscribed systems and scheduled systems. Both systems receive SceneContext& ctx as their first argument. This is the object by which you can access persistent engine objects (See SceneContext):
Subscribed systems execute once when a particular game event occurs and are invoked through the Broadcaster. These systems contain logic that does not need to execute every frame, but instead should perform their changes in a single go after a specific event happens. For example, a PlayerDamage system should not damage the player every frame, but just once when a different system detects that the player should take damage. See Events for more information on how to create and emit events in order to invoke subscribed systems. Subscribed systems receive an EventType* event as their second argument. This is a struct that you define which contains any additional data that a subscribed system will need in order to execute.
Scheduled systems execute every frame or fixed step of the scene that they are registered in. These system contain logic that apply changes to components or have to read component data continuously while the game is executing. For example, an EnemyAI script needs to make decisions about component data and move the enemy accordingly every frame, so it would be a scheduled system. Scheduled systems receive dt in addition to ctx. dt is the time step in seconds and should be multiplied to anything that changes over time so that it is not affected by framerate.
Scheduled systems are registered in you scene’s on_load, through the scene’s _scheduler member. From then on, the engine calls them for you every frame or fixed step until the scene unloads.
Scheduler systems run in this order, each frame:
There are two kinds of scheduled systems, and the only difference is how often they run:
| Update system | Fixed system | |
|---|---|---|
| Registered with | register_update | register_fixed |
| Runs | Once per frame | At a steady rate (60 times per second by default) |
| dt is | The real time since the last frame (varies) | Always the same: the fixed timestep |
| Use for | Things the player sees or feels: camera, UI, effects, menus | Things game rules depend on: movement, physics, collision, gameplay timers |
Frame times vary from machine to machine and from moment to moment. For most thing that is fine, because multiplying by dt keeps speed correct. However, for physics it is not enough. A jump calculated in big steps on a slow machine comes out slightly lower than a jump in small steps on a fast one, and a long frame can move an object right through a wall without ever touching it. Fixed systems always use the same small step, so the game behaves the same everywhere.
The general rule is: if logic in a system could change the outcome of the game if ran at a different rate, then it should be a fixed system. For example, a gravity system would have to be fixed because it changes how far and how high things move, so running it at a different rate would change the outcome of the game. On the other hand the camera would be a frame system because it only changes what is drawn so it should be updated every time the screen does.
Everything a system can reach comes through ctx:
| Field | What it is | See |
|---|---|---|
| ctx.nexus | This scene's entities and components | {Entities and Components |
| ctx.broadcaster | This scene's events | Events |
| ctx.input | The current state of every input action | Input |
| ctx.engine | Switching scenes and quitting | Engine Requests |
| ctx.window | The game window | |
| ctx.assets | Not available yet — always nullptr for now |
nexus and broadcaster always belong to the scene the system is running in. input, engine, and window are shared by every scene.
Register in on_load. Each kind of system runs in the order you register it.
Order matters when one system uses another’s results. For example, above, MoveSystem needs to be registered second because it uses the velocity GravitySystem just changed.
This can only be done within the Scene, not within systems themselves.
Systems can not be registered or unregistered within systems themselves (only within Scenes). Instead a system’s behavior should stop as a response to the component data it is reading. For example an EnemyAI system should not execute if the enemy is stunned, so, it should have a guard the skips execution when an enemy is stunned, rather than be unregistered once an enemy is stunned:
The fixed rate is 60 fixed steps per second with 8 max fixed steps, but this can be changed to fit your game’s specific needs:
| Function | What it does | Notes |
|---|---|---|
| register_update(system) | Runs system once per frame | Registering twice does nothing |
| register_fixed(system) | Runs system every fixed step | Registering twice does nothing |
| unregister(system) | Removes system | Warns if it wasn't registered |
| set_user_order({...}) | Reorders your update systems | Must list exactly the registered update systems |
| set_fixed_timestep(seconds) | Sets the fixed step size | Default 1.0f / 60.0f; must be > 0 |
| set_max_fixed_steps(n) | Caps fixed steps per frame | Default 8; must be > 0 |