![]() |
ImagoEngine
A C++ 2D game engine built on a sparse-set ECS, with scenes, scheduled systems, events, and action-based input
|
Create game objects and attach data to them.
Use this when you need anything to exist in your game world, a player, a bullet, a camera target, an enemy, a platform, ect.
Include: Imago/Engine/Scene.hpp (gives you Imago::ECS::Nexus)
ImagoEngine uses an Entity Component System (ECS) design pattern. This means that an entity is nothing but an unsigned integer ID, it has no behavior, and no data. A component is a plain struct holding ONLY data. This data would be something like position, or a health value, or an attack stat, ect. You assign data to an entity by binding components to it. However, neither of these contain any behavior. Instead, systems iterate through pools of components and execute behavior by reading and changing the data.
All entities and components in a scene live in that scene’s Nexus. You can access a Scene’s Nexus through the _nexus member in your scene’s on_load or through ctx.nexus from within a system.
Important thing to know: adding and removing components, and destroying entities is deferred. These changes are structural changes to the component pool, and applying them during iteration of those pools can cause undefined behavior. For that reason, the changes are queued and applied at the end of the frame (or the end of on_load), not when you call them. So, remember that adding or removing a component, or destroying an entity, will not happen until the end of the frame, but reading and changing component values happens immediately.
Components are plain structs. Give fields default values so they are easy to construct. By convention, component names start with c
Components must be copyable. Avoid members like std::unique_ptr.
Here, it is safe to use get because within a Survey, an entity is guaranteed to have the component. When you are not sure if the entity has a component, use try_to_get and do a null check.
| Function | What it does | Takes effect |
|---|---|---|
| create() | Makes a new entity with no components | Immediately |
| destroy(e) | Removes e and all its components | End of frame |
| is_valid(e) | true if e hasn't been destroyed | Immediately |
| bind<T>(e, value) | Adds component T to e | End of frame |
| unbind<T>(e) | Removes component T from e | End of frame |
| get<T>(e) | Reference to e's T (must exist) | Immediately |
| try_to_get<T>(e) | Pointer to e's T, or nullptr | Immediately |
| has<T>(e) | true if e has T | Immediately |
| patch<T>(e, value) | Overwrites e's existing T | Immediately |