# The access rule

> Why a method the framework calls on your type starts with an underscore, why a function you hand to the framework starts with on_, and why the two can live on the same object.

Source: https://omardev29.github.io/rmp-docs/framework/why/the-access-rule.html

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:

| Hook | On a scene | On an object | On a behavior |
|---|---|---|---|
| `_ready` | entering the stack | after `spawn()` | when it is added |
| `_update` | every frame | every frame | every frame, before the move |
| `_late_update` |  |  | every frame, after the move |
| `_draw` | in screen space | after its sprite or shape | after the object is drawn |
| `_collision` |  | once per touching pair per frame | the same |
| `_end` | leaving for good | on destruction | when removed, or the object dies |
| `_suspend` | something was pushed over it |  |  |
| `_resume` | it 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](https://omardev29.github.io/rmp-docs/framework/the-frame.html) gives the exact order.

```cpp
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.

```cpp
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`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#on_click), [`on_drag`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#on_drag) and [`on_collision`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#on_collision) on an object, [`on_object`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Tilemap.html#on_object) on a map, and the `on_` fields of the behaviors that report something — [`Health`](https://omardev29.github.io/rmp-docs/reference/behaviors/Health.html)'s `on_death` and `on_damage`, [`Timer`](https://omardev29.github.io/rmp-docs/reference/behaviors/Timer.html)'s `on_timeout`, [`Spawner`](https://omardev29.github.io/rmp-docs/reference/behaviors/Spawner.html)'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](https://omardev29.github.io/rmp-docs/guidelines/naming.html), 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.
