Skip to content
Download

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.

Novo objeto (new object) in the editor creates two code files next to the object’s data:

  • the .hpp has the class: the events it uses, the fields and the functions;
  • the .cpp has the body of each function and the BRASA_ENTITY line.
Player.hpp
#pragma once
#include <brasa/brasa.hpp>
class Player : public brasa::Entity {
public:
BRASA_VARS(Player)
void onSpawn() override;
void onUpdate() override;
};
Player.cpp
#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 after public:.
  • Every object sees all the others with no #include: inside Player.cpp you can already write spawn<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.

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.

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.

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.