On this page
  1. _name: the framework calls your method
  2. on_name: you hand the framework a function
  3. Both at once
  4. Why an underscore
  5. The third kind: detail

The access rule

There are two ways for the framework to run your code, and the name says which one it is. A method whose name starts with _ is one of yours that the framework calls: you override it and never call it yourself. A function whose name starts with on_ is one you hand to the framework, which keeps it and calls it when something happens.

_name: the framework calls your method#

These are the hooks, and there are exactly eight names:

HookOn a sceneOn an objectOn a behavior
_readyentering the stackafter spawn()when it is added
_updateevery frameevery frameevery frame, before the move
_late_updateevery frame, after the move
_drawin screen spaceafter its sprite or shapeafter the object is drawn
_collisiononce per touching pair per framethe same
_endleaving for goodon destructionwhen removed, or the object dies
_suspendsomething was pushed over it
_resumeit came off again

A scene and an object override them as virtual functions; a behavior simply declares the ones it wants, and the framework finds them when it is compiled. Either way the rule is the same: you write it, the framework decides when it runs. One frame gives the exact order.

class Coin : public rmp::Object {
    void _ready() override { shape = rmp::circle(8); }
    void _update(float delta) override { rotation += 90 * delta; }
    void _collision(rmp::Object &other) override {
        if (other.solid) destroy();
    }
};

on_name: you hand the framework a function#

Sometimes a subclass is more than you want: one coin out of many needs to do something special when it is touched, or a scene wants to know when an object it spawned is clicked. Then you hand the framework a function instead, and the name says that is what you are doing.

auto &coin = spawn({ .position = { 300, 200 }, .size = { 16, 16 } });
coin.on_collision([](rmp::Object &self, rmp::Object &other) {
    if (other.solid) {
        score += 10;
        self.destroy();
    }
});
coin.on_click([](rmp::Object &self) { self.visible = false; });

The functions the framework takes this way are on_click, on_drag and on_collision on an object, on_object on a map, and the on_ fields of the behaviors that report something — Health's on_death and on_damage, Timer's on_timeout, Spawner's on_spawn. A game that writes its own entry point names its three hooks the same way, on_ready, on_frame and on_exit, because those too are functions it hands to the framework. Setting a callback a second time replaces the first: there is no list of handlers, because a list of handlers is a signal system, and that is a bigger idea than this needs.

Both at once#

The two spellings never collide, which is the reason there are two. An object can override _collision and carry an on_collision, and both run: the override first, then its behaviors', then the function it was handed. Nobody has to choose between subclassing and a callback, and reading either one, you know which way the call goes.

Why an underscore#

Because it is the one mark that is legal exactly where it is needed. In C++ a name that starts with an underscore is reserved at global scope, for the compiler and the C library — and a hook is never at global scope: it is a member of your class, where _ready is an ordinary, legal name. That is also why the hooks of an entry point, which are free functions, are on_ready and not _ready.

The scene's last hook is _end and not _exit for a related reason: exit() is a function of the C library, and a member with that name in a class you write would hide it.

The framework holds itself to the same rule. clang-tidy is given the eight hook names and refuses any other method that starts with an underscore, so a helper of the framework's never looks like a hook, and a private field is never named like one: an object keeps the functions it was handed in members called _click_handler, _drag_handler and _collision_handler. Naming, in the Guidelines, has the whole table this rule is a part of, and what each tool says about a name that breaks it.

The third kind: detail#

One more spelling marks what is neither yours nor for you. A namespace called detail, or a member whose name starts with detail_, belongs to the framework and its tests. It is public only because C++ needs it to be — a template in a header has to reach it — and it is not part of the API: the reference does not show it.