Skip to content
Download

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.

  1. Click the object in Recursos (Resources).
  2. In the Inspector, in the Variáveis (variables) section, click + Variável and choose the type.
  3. Click the name (it starts as variavel) and type the name you want, for example velocidade (speed).
  4. 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)

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.

  1. Open the room and click the instance.
  2. In the Inspector, under Valores próprios (own values), check the small box next to the variable.
  3. 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); });
  • 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.

  • When you rename, the editor updates the instances in every room and asks whether to change the name in the object’s .hpp and .cpp too. 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.

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.