Objects and events
Every object of the game is a class that inherits from brasa::Entity and is registered with BRASA_ENTITY. You override only the events you need.
class Player : public brasa::Entity {public: void onSpawn() override { /* ... */ } void onUpdate() override { /* ... */ }};BRASA_ENTITY(Player);The name passed to BRASA_ENTITY is the name of the object in the editor.
The files of an object
Section titled “The files of an object”Novo objeto (new object) in the editor creates two code files next to the object’s data:
- the
.hpphas the class: the events it uses, the fields and the functions; - the
.cpphas the body of each function and theBRASA_ENTITYline.
#pragma once#include <brasa/brasa.hpp>
class Player : public brasa::Entity {public: BRASA_VARS(Player)
void onSpawn() override; void onUpdate() override;};#include "Player.hpp"
using namespace brasa;
void Player::onSpawn() { /* ... */ }void Player::onUpdate() { /* ... */ }
BRASA_ENTITY(Player);BRASA_VARS(Player)creates in the class the fields of the variables made in the editor. It goes right afterpublic:.- Every object sees all the others with no
#include: insidePlayer.cppyou can already writespawn<Bullet>(x, y, "Instancias"). - The class needs to be outside any
namespace.
An object with the whole class inside the .cpp, as in projects from 0.1, still works. It just doesn’t receive variables from the editor.
Events
Section titled “Events”| Event | When it runs |
|---|---|
void onSpawn() |
Once, right after the instance is created, with x and y already set. Use it in place of the constructor. |
void onUpdate() |
Once per step (60 per second), after the timers. |
void onPausedUpdate() |
Once per step while the game is paused (setPaused), instead of onUpdate. See Interface. |
void onRender() |
To draw the instance. The default calls drawSelf(), which draws the sprite. |
void onRenderGui() |
To draw on top of everything, without the camera (scoreboard, menus). |
void onDestroy() |
When the instance is removed, at the end of the step. |
void onAnimationEnd() |
When the sprite’s animation completes a loop. |
The order inside a step is in Step order.
Position and appearance
Section titled “Position and appearance”| Property | Type | What it is |
|---|---|---|
x, y |
float |
Position in the world (where the origin of the sprite sits). y grows downwards. |
sprite |
std::string |
Name of the sprite. "" = no sprite and no collision. |
frame |
float |
Current frame of the animation, starting at 0. It goes back to 0 by itself when sprite is changed. |
animSpeed |
float |
Speed of the animation. If the sprite has an fps, it is a multiplier (1 = the speed of the sprite). Without an fps, it is frames per step. Default 1; 0 stops the animation. |
scaleX, scaleY |
float |
Scale of the drawing. -1 flips it. |
angle |
float |
Rotation in degrees, clockwise. It only affects the drawing, not the collision. |
depth |
int |
Drawing order: higher goes behind. It comes from the instance’s layer. |
visible |
bool |
false = neither onRender nor onRenderGui is called. |
solid |
bool |
Taken into account by placeFree and moveAndStop. |
mask |
std::string |
Name of a sprite. When set, the collision box comes from it, not from the current sprite. |
flipX |
bool |
Mirrors the drawing and the collision box horizontally, without touching the scale. |
persistent |
bool |
true = the instance survives goTo and restart. See Keep data between rooms. |
Speed, gravity and friction are in Movement.
Life cycle
Section titled “Life cycle”| Function | What it does | Example |
|---|---|---|
void destroy() |
Marks the instance to be removed at the end of the step. It can be called more than once. | destroy(); |
bool isDestroyed() const |
true if destroy() has already been called. |
if (isDestroyed()) return; |
const std::string& objectName() const |
The name of the project object this instance came from. | if (objectName() == "Chefe") ... |
The values set in the editor (sprite, visible, solid, mask, physics and variables) are already applied when onSpawn runs.
