# rmp::Ref

> An optional reference: something that may not be there, and is not yours.

Source: https://omardev29.github.io/rmp-docs/reference/classes/rmp-Ref.html

class `#include <rmp/object.h>` [include/rmp/object.h:383](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L383-L406)

template <class T> class Ref

An optional reference: something that may not be there, and is not yours.

```cpp
if (auto health = player.get<Health>()) health->hp -= 1;
```

What [`Object::get`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#get)<B>() and [`Handle::get()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Handle.html#get) hand back. It is empty, or it refers to something; it owns nothing, and asking it is the `if` above — true when there is something, and then -> and * reach it. Keep it within the frame, like the reference `spawn()` returns; across frames, keep the Handle.

Not a `T *`: a pointer cannot say whether it may be null or whose it is, and this type says both. Not std::optional<std::reference_wrapper<T>> either, which says the same for the price of <functional> in every file with an object in it, and reads as `health->get().hp`.

REACHING INTO AN EMPTY ONE is a mistake in the game, and is said once, per type: * and -> then reach a stand-in, a default T made for the purpose and made again on every use, so the line that forgot the `if` writes to nothing instead of taking the player's game down with it. (`[dev] strict` stops a debug build there instead.) A type with no default constructor has no stand-in, and that one mistake ends the program — with the same message.

### `Ref`

explicit Ref(T &target)

function [include/rmp/object.h:388](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L388)

Refers to `target`, which stays whoever's it was.

### `operator bool`

explicit operator bool() const

function [include/rmp/object.h:391](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L391)

True when it refers to something.

### `operator*`, `operator->`, `operator==`

T &operator*() const

T *operator->() const

bool operator==(const Ref &ref, std::nullptr_t)

function [include/rmp/object.h:393](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L393-L396)

What it refers to — or, empty, the stand-in above, and a line in the log.
