Skip to content

Commit e49da7b

Browse files
committed
Document new property API
1 parent d75e7cd commit e49da7b

File tree

1 file changed

+36
-62
lines changed

1 file changed

+36
-62
lines changed

docs/godot_docs/cppexamples.md

+36-62
Original file line numberDiff line numberDiff line change
@@ -327,43 +327,33 @@ Once connected, the Godot engine will directly call the function `_on_body_enter
327327
328328
static float jump_velocity = -300.0f;
329329
static float player_speed = 150.0f;
330+
static std::string player_name = "Slight Knight";
331+
332+
int main() {
333+
ADD_API_FUNCTION(_physics_process, "void", "double delta");
334+
ADD_API_FUNCTION(_process, "void");
335+
336+
add_property("player_speed", Variant::FLOAT, player_speed,
337+
[]() -> Variant { return player_speed; },
338+
[](Variant value) -> Variant { return player_speed = value; });
339+
add_property("player_jump_vel", Variant::FLOAT, jump_velocity,
340+
[]() -> Variant { return jump_velocity; },
341+
[](Variant value) -> Variant { return jump_velocity = value; });
342+
add_property("player_name", Variant::STRING, player_name,
343+
[]() -> Variant { return player_name; },
344+
[](Variant value) -> Variant { return player_name = value.as_std_string(); });
345+
346+
halt();
347+
}
348+
```
330349

331-
SANDBOXED_PROPERTIES(3, {
332-
.name = "player_speed",
333-
.type = Variant::FLOAT,
334-
.getter = []() -> Variant { return player_speed; },
335-
.setter = [](Variant value) -> Variant { return player_speed = value; },
336-
.default_value = Variant{player_speed},
337-
}, {
338-
.name = "player_jump_vel",
339-
.type = Variant::FLOAT,
340-
.getter = []() -> Variant { return jump_velocity; },
341-
.setter = [](Variant value) -> Variant { return jump_velocity = value; },
342-
.default_value = Variant{jump_velocity},
343-
}, {
344-
.name = "player_name",
345-
.type = Variant::STRING,
346-
.getter = []() -> Variant { return "Slide Knight"; },
347-
.setter = [](Variant value) -> Variant { return Nil; },
348-
.default_value = Variant{"Slight Knight"},
349-
});
350-
351-
// More code ...
352-
```
353-
354-
Properties in the Sandbox are supported. They are stored in the global scope, and each one has a custom getter and setter function.
350+
Properties in the Sandbox are supported. They are added through the `add_property()` API function, and each one has a custom getter and setter function.
355351

356352
![alt text](/img/cppexamples/properties.png)
357353

358354
Properties can be edited in the Godot inspector and is a powerful and simple way to expose data from the script. The values of these properties are saved in the Godot project and restored on reopening the project.
359355

360356

361-
:::note
362-
363-
In this example, the players name cannot be changed in the editor, as the property will just keep returning the same value, _effectively making it read-only_. That is, the getter for the `player_name` property only returns `"Slide Knight"`.
364-
365-
:::
366-
367357
### Per-instance Sandboxed Properties
368358

369359
Sometimes we want properties per instance, even if we have several. In that case, we can access the property based on the value of `get_node()`. A `std::unordered_map` on the `get_node().address()` can be used to achieve this:
@@ -381,42 +371,26 @@ static PlayerState &get_player_state() {
381371
return GetPlayerState(get_node());
382372
}
383373

384-
SANDBOXED_PROPERTIES(3, {
385-
.name = "player_speed",
386-
.type = Variant::FLOAT,
387-
.getter = []() -> Variant { return get_player_state().player_speed; },
388-
.setter = [](Variant value) -> Variant { return get_player_state().player_speed = value; },
389-
.default_value = Variant{get_player_state().player_speed},
390-
}, {
391-
.name = "player_jump_vel",
392-
.type = Variant::FLOAT,
393-
.getter = []() -> Variant { return get_player_state().jump_velocity; },
394-
.setter = [](Variant value) -> Variant { return get_player_state().jump_velocity = value; },
395-
.default_value = Variant{get_player_state().jump_velocity},
396-
}, {
397-
.name = "player_name",
398-
.type = Variant::STRING,
399-
.getter = []() -> Variant { return get_player_state().player_name; },
400-
.setter = [](Variant value) -> Variant { return get_player_state().player_name = value.as_std_string(); },
401-
.default_value = Variant{"Slide Knight"},
402-
});
374+
int main() {
375+
ADD_API_FUNCTION(_physics_process, "void", "double delta");
376+
ADD_API_FUNCTION(_process, "void");
377+
378+
add_property("player_speed", Variant::FLOAT, 150.0f,
379+
[]() -> Variant { return get_player_state().player_speed; },
380+
[](Variant value) -> Variant { return get_player_state().player_speed = value; });
381+
add_property("player_jump_vel", Variant::FLOAT, -300.0f,
382+
[]() -> Variant { return get_player_state().jump_velocity; },
383+
[](Variant value) -> Variant { return get_player_state().jump_velocity = value; });
384+
add_property("player_name", Variant::STRING, "Slide Knight",
385+
[]() -> Variant { return get_player_state().player_name; },
386+
[](Variant value) -> Variant { return get_player_state().player_name = value.as_std_string(); });
387+
388+
halt();
389+
}
403390
```
404391
405392
In this program, each instance will have their own separate properties.
406393
407-
### Adding properties dynamically
408-
409-
It's possible to add properties during `main()` using `add_property`:
410-
411-
```cpp
412-
add_property("player_speed", Variant::FLOAT,
413-
[]() -> Variant { return player_speed; },
414-
[](Variant value) -> Variant { return player_speed = value; },
415-
player_speed);
416-
```
417-
418-
This feature is intended to make it easier to support languages with limited capability of instantiating an array of structs on the global scope.
419-
420394
421395
## Timers
422396

0 commit comments

Comments
 (0)