Instances
These functions work with the instances of the current room. The <T> is the object: spawn<Enemy>, count<Coin>.
| Function | What it does | Example |
|---|---|---|
T* spawn<T>(float atX, float atY, std::string_view layer) |
Creates an instance of T at the given position, in the layer layer. Its onSpawn runs right away. Returns the instance created (or nullptr if the room is already ending). |
spawn<Bullet>(x, y - 8, "Tiros"); |
void each<T>(fn) |
Calls fn for each living instance of T. The ones created during the call aren’t included. |
each<Enemy>([](Enemy& e) { e.destroy(); }); |
int count<T>() |
How many living instances of T exist. |
if (count<Enemy>() == 0) goTo("Victory"); |
bool any<T>() |
true if there is at least one instance of T. |
if (!any<Player>()) restart(); |
T* closest<T>() |
The closest instance of T (not counting this one), or nullptr. |
if (Player* p = closest<Player>()) moveToward(p->x, p->y, 1); |
void destroyAll<T>() |
Destroys all the instances of T. |
destroyAll<Bullet>(); |
T* instance<T>() |
The first living instance of T (it can be this one), or nullptr. Made for objects that have only one instance, such as a global controller. |
instance<Controle>()->moedas++; |
By the object’s name
Section titled “By the object’s name”each, count, any and destroyAll also take the object’s name in quotes. It is for objects without code, which have no class. each receives an Entity&.
| Function | Example |
|---|---|
int count(std::string_view object) |
if (count("Moeda") == 0) goTo("Fase2"); |
bool any(std::string_view object) |
if (!any("Chave")) ... |
void each(std::string_view object, fn) |
each("Caixa", [](Entity& caixa) { caixa.visible = false; }); |
void destroyAll(std::string_view object) |
destroyAll("Moeda"); |
The name of an instance’s object is in objectName().
- “Living” means they haven’t been destroyed yet.
- Subclasses count: if
Bossinherits fromEnemy,count<Enemy>()counts the bosses too. - Always check that the pointer returned isn’t
nullptrbefore using it, as in the examples withif (... = ...). - To keep a reference to an instance for more than one step, use a Handle.
