Variables
You want to tune the player’s speed, an enemy’s health or a door’s destination in the editor, without opening the code for every test.
A variable is a named value you create on the object. In the code, it is an ordinary field of the class. In the room, each instance can have its own value.
Create a variable
Section titled “Create a variable”- Click the object in Recursos (Resources).
- In the Inspector, in the Variáveis (variables) section, click + Variável and choose the type.
- Click the name (it starts as
variavel) and type the name you want, for examplevelocidade(speed). - Next to the name, set the default value.
The ⋮ button at the end of the row changes the Tipo (type), changes the order (Subir, up; Descer, down) and deletes the variable (Excluir).
| Type | What it holds | In the code |
|---|---|---|
| número (number) | A number with decimals | float |
| inteiro (integer) | A whole number | int |
| sim/não (yes/no) | True or false | bool |
| texto (text) | A text | std::string |
| cor (color) | A color | Color |
| room, objeto, sprite, som | A resource of the project, chosen from a list | std::string (the name) |
Use it in the code
Section titled “Use it in the code”The object’s class needs the BRASA_VARS line, right after public:. Novo objeto (new object) already creates the .hpp with it:
class Player : public brasa::Entity {public: BRASA_VARS(Player)
void onUpdate() override;};From then on, the variable is a field like any other:
void Player::onUpdate() { x += axis("esquerda", "direita") * velocidade;}Don’t declare a field with the same name in the class: BRASA_VARS already does that.
A different value on each instance
Section titled “A different value on each instance”- Open the room and click the instance.
- In the Inspector, under Valores próprios (own values), check the small box next to the variable.
- Change the value.
Unchecked, the instance uses the object’s default value. An example: a flag with the variable destino (destination), of type room. Each flag in the level points to a different room, and the code is the same:
onTouchStart<Player>([this](Player&) { goTo(destino); });What recompiles
Section titled “What recompiles”- Changing a value (the default or an instance’s) recompiles nothing: press Play.
- Creating, renaming, deleting or changing the type of a variable recompiles the game on the next Play.
The values are already on the object when onSpawn runs.
Rename and delete
Section titled “Rename and delete”- When you rename, the editor updates the instances in every room and asks whether to change the name in the object’s
.hppand.cpptoo. Other files that use the variable are up to you. - When you delete, the instances lose their own value, and the code that used the variable stops compiling until you take it out.
- Renaming a room, an object, a sprite or a sound updates the variables that point to it.
If a yellow warning shows up
Section titled “If a yellow warning shows up”The warning says the class has no BRASA_VARS: the variables are saved, but the values don’t reach the game. Click Inserir BRASA_VARS no .hpp (insert BRASA_VARS in the .hpp) and the editor writes the line for you.
In an old object, with the class inside the .cpp, move the class to a .hpp first. The class also needs to be outside any namespace.
See also
Section titled “See also”- Inspector: the sections of the object and of the instance.
- Objects and events: the object’s class.
