Skip to content
Download

Wait and repeat

You want something to happen later (the shot reloading, the bomb exploding) or from time to time (an enemy shooting every second).

#include <brasa/brasa.hpp>
using namespace brasa;
class Bomb : public Entity {
public:
void onSpawn() override {
afterSeconds(3, [this] { destroy(); }); // explodes in 3 seconds
every(30, [this] { visible = !visible; }); // blinks every half second
}
};
BRASA_ENTITY(Bomb);
  • The game runs at 60 steps per second. after(60, ...) waits 1 second, and after(30, ...) waits half.
  • after(steps, ...) runs the lambda once, after that many steps.
  • every(steps, ...) runs it always, every that many steps.
  • afterSeconds(seconds, ...) is after counted in seconds.
  • The timers go away by themselves when the object is destroyed.

Cancel. Every timer function returns a TimerId. Keep it to cancel later:

// in the class: TimerId blink_;
blink_ = every(30, [this] { visible = !visible; });
// ... later:
cancel(blink_);
visible = true;

An interval that changes. A function that schedules itself again with after can change the interval each time, like the Spawner of the tutorial.