![]() |
ImagoEngine
A C++ 2D game engine built on a sparse-set ECS, with scenes, scheduled systems, events, and action-based input
|
Loop over every entity that has a particular set of components.
Use this when a system needs to execute logic for every entity with a specified set of components. For example a gravity system would need to apply gravity to every entity with a cVelocity and a cGravity component.
Include: Imago/Engine/Scene.hpp
Most subscribed systems follow the same shape: find all entities with specific components, then read or change those components. ImagoEngine provides 2 different ways to do that:
NOTE: get is always safe inside the loop for a Survey over Bond over specific component types because every entity the Survey or Bond gives you is guaranteed to have every listed component.
A survey finds every entity that has all of the component types you specify. You create one with:
and loop over it with a range-based for loop. Each pass gives you an Entity, and you use get to reach its components.
Surveys are cheap to create, so make a new one each time you need it; this is usually once per system call.
If you need a component only some of the entitles have, do not add it to the survey and check inside the loop.
All three structural changes are safe inside a survey loop. They’re queued and applied at the end of the frame, so the loop always finishes over an unchanged set of entities:
Survey work in on_load too. Use _nexus member instead of ctx.nexus
Remember that entities and components you bound earlier in the same on_load are not applied yet, so the survey won’t find them.
| Function | What it does | Notes |
|---|---|---|
| survey<A, B, ...>() | Returns a survey of every entity with all of A, B, ... | Loop it with for (Entity e : ...) |
The first bond<...>() call creates the bond and sorts the existing entities which takes some time. Every later call with the same component types just returns the existing bond, so it is cheap to call every frame. Therefore, you should create the bond once when the scene loads, then use it in a system:
A bond has the same use-case as a survey but is a permanent group of component types. When you create one, the engine rearranges those components’ storage so that every entity having all of them sits together at the front of their respective pool, in the same order. It then ensures that this ordering remains as components are added and removed.
Because all matching entities are grouped together for every component pool, unlike surveys, looping over a bond never has to check whether or not the entity is in every pool. It simply visits exactly the entities you want to check and does nothing else. That makes bonds the fastest way to loop over entities with specific components, with a couple important caveats:
Since bonds rearrange storage, each component type can only be in one bond per scene. Two bonds sharing the same component type would fight over ordering. Asking for a bond that overlaps on an existing one crashes the program:
Similarly, since binds, unbinds, and destroys could change which entities should belong in a bond, these calls become more expensive since it has to notify the Bond about structural changes so it can maintain its order.
Because each component type can be in only one bond per scene, which pools you decide to bond should be carefully considered. Use a survey be default and reach for a bond when all of these are true:
Common candidates for bonds are large component sets that define what an entity is and stay with it for its whole life:
| Bond | Example use |
|---|---|
| bond<cBoid, cFlocking>() | Flocking birds or fish |
| bond<cCrop, cGrowth>() | Every growing plant in a farming game |
| bond<cTurret, cTargeting>() | Every turret choosing a target |
However, choosing whether or not to use bond is not always straight forward. Particles are commonly considered for bonds because they are usually a large amount of entities. However, particles are usually created and destroyed constantly, which works against a bond, meaning that a survey might be just as fast. Use surveys by default and weigh all of the advantages and disadvantages of using a bond in your game before deciding to use it.
| Function | What it does | Notes |
|---|---|---|
| bond<A, B, ...>() | Creates the bond the first time; returns it after that | Loop it with for (Entity e : ...) |