C++ in Brasa
Files of an object
Section titled “Files of an object”New object… creates Name.obj.json, Name.hpp and Name.cpp. The .hpp holds the class; the .cpp, the function bodies and the registration.
#pragma once#include <brasa/brasa.hpp>
class Player : public brasa::Entity {public: BRASA_VARS(Player)
void onSpawn() override; void onUpdate() override;
private: bool podeAtirar_ = true;};#include "Player.hpp"
using namespace brasa;
void Player::onSpawn() {}
void Player::onUpdate() { hspeed = axis("esquerda", "direita") * velocidade;}
BRASA_ENTITY(Player);BRASA_ENTITY(Name)registers the class under the object name in the editor.BRASA_VARS(Name), right afterpublic:, creates the fields for the Inspector variables.- Every object sees the others with no
#include:spawn<Bullet>(...)works directly inPlayer.cpp. - The class stays outside any
namespace. - Standalone files (
src/game.hpp,src/game.cpp) are compiled along and serve what applies to the whole game.
Callbacks
Section titled “Callbacks”Timers, collisions and buttons take a lambda. [this] gives access to the members of the instance; the parameter is the other instance, by reference.
after(10, [this] { podeAtirar_ = true; });
onCollide<Enemy>([this](Enemy& inimigo) { inimigo.destroy();});The registrations end when the instance is destroyed.
Class or name
Section titled “Class or name”Functions over objects accept the class (count<Enemy>(), onCollide<Coin>(...)) or the name in quotes (count("Enemy")). With the class, the result has its type; with the name, it is Entity. The name is the only way to refer to an object without code.
Pointers and handles
Section titled “Pointers and handles”spawn, closest, instance, findAt and touching return a pointer, which may be nullptr. It is valid on this step; to keep an instance for longer, use a Handle.
if (Enemy* alvo = closest<Enemy>()) moveToward(alvo->x, alvo->y, 1.5f);Compile errors
Section titled “Compile errors”On an error, the game does not open and the Output shows the file, the line and the message. Clicking the error opens the code at that point.
Player.cpp(23,5): error C2143: erro de sintaxe: ';' ausente antes de 'if'| Message | Usual cause |
|---|---|
missing ';' |
The ; is missing at the end of the previous line. |
undeclared identifier |
Wrong name, or the #include of a standalone file is missing. |
cannot convert |
Different types, such as int and float. |
'}' expected |
A brace was left open. |
To write to the Output: brasa::logInfo(std::format("vidas = {}", vidas));.
