@@ -155,7 +155,7 @@ GDExtension node we'll be creating. We will name it ``gdexample.h``:
155155 namespace godot {
156156
157157 class GDExample : public Sprite2D {
158- GD_CLASS (GDExample, Sprite2D)
158+ GDCLASS (GDExample, Sprite2D)
159159
160160 private:
161161 float time_passed;
@@ -370,7 +370,6 @@ also result in *just* that file being exported when you export the project,
370370which means the data pack won't contain libraries that are incompatible with the
371371target platform.
372372
373- TODO: check if dependencies section is supported
374373Finally, the ``dependencies `` section allows you to name additional dynamic
375374libraries that should be included as well. This is important when your GDExtension
376375plugin implements someone else's library and requires you to supply a
@@ -394,40 +393,41 @@ Adding properties
394393-----------------
395394
396395GDScript allows you to add properties to your script using the ``export ``
397- keyword. In GDExtension you have to register the properties and there are two ways
398- of doing this. You can either bind directly to a member or use a setter and
399- getter function.
396+ keyword. In GDExtension you have to register the properties with a getter and
397+ sett function or directly implement the ``_get_property_list ``, ``_get `` and
398+ ``_set `` methods of an object (but that goes far beyond the scope of this
399+ tutorial.
400400
401- .. note ::
402-
403- There is a third option, just like in GDScript you can directly implement the
404- ``_get_property_list ``, ``_get `` and ``_set `` methods of an object but that
405- goes far beyond the scope of this tutorial.
406-
407- We'll examine both starting with the direct bind. Lets add a property that
408- allows us to control the amplitude of our wave.
401+ Lets add a property that allows us to control the amplitude of our wave.
409402
410- In our ``gdexample.h `` file we simply need to add a member variable like so:
403+ In our ``gdexample.h `` file we need to add a member variable and getter and setter
404+ functions:
411405
412406.. code-block :: C++
413407
414408 ...
415409 private:
416410 float time_passed;
417411 float amplitude;
412+
413+ public:
414+ void set_amplitude(const float amplitude);
415+ float get_amplitude() const;
418416 ...
419417
420418In our ``gdexample.cpp `` file we need to make a number of changes, we will only
421419show the methods we end up changing, don't remove the lines we're omitting:
422420
423421.. code-block :: C++
424422
425- void GDExample::_register_methods() {
426- register_method("_process", &GDExample::_process);
427- register_property<GDExample, float>("amplitude", &GDExample::amplitude, 10.0);
423+ void GDExample::_bind_methods() {
424+ ClassDB::bind_method(D_METHOD("get_amplitude"), &GDExample::get_amplitude);
425+ ClassDB::bind_method(D_METHOD("set_amplitude", "p_amplitude"), &GDExample::set_amplitude);
426+
427+ ClassDB::add_property("GDExample", PropertyInfo(Variant::FLOAT, "amplitude"), "set_amplitude", "get_amplitude");
428428 }
429429
430- void GDExample::_init () {
430+ void GDExample::GDExample () {
431431 // initialize any variables here
432432 time_passed = 0.0;
433433 amplitude = 10.0;
@@ -444,21 +444,19 @@ show the methods we end up changing, don't remove the lines we're omitting:
444444 set_position(new_position);
445445 }
446446
447+ void GDExample::set_amplitude(const float p_amplitude) {
448+ amplitude = p_amplitude;
449+ }
450+
451+ float GDExample::get_amplitude() const {
452+ return amplitude;
453+ }
454+
447455Once you compile the module with these changes in place, you will see that a
448456property has been added to our interface. You can now change this property and
449457when you run your project, you will see that our Godot icon travels along a
450458larger figure.
451459
452- .. note ::
453-
454- The ``reloadable `` property in the ``gdexample.gdnlib `` file must be set to
455- ``true `` for the Godot editor to automatically pick up the newly added
456- property.
457-
458- However, this setting should be used with care, especially when tool classes
459- are used, as the editor might hold objects then that have script instances
460- attached to them that are managed by a GDExtension library.
461-
462460Let's do the same but for the speed of our animation and use a setter and getter
463461function. Our ``gdexample.h `` header file again only needs a few more lines of
464462code:
@@ -479,13 +477,13 @@ showing the methods that have changed so don't remove anything we're omitting:
479477
480478.. code-block :: C++
481479
482- void GDExample::_register_methods () {
483- register_method("_process" , &GDExample::_process );
484- register_property<GDExample, float>("amplitude" , &GDExample::amplitude, 10.0 );
485- register_property< GDExample, float>( "speed", &GDExample:: set_speed, &GDExample:: get_speed, 1.0 );
480+ void GDExample::_bind_methods () {
481+ ClassDB::bind_method(D_METHOD("get_speed") , &GDExample::get_speed );
482+ ClassDB::bind_method(D_METHOD("set_speed", "p_speed") , &GDExample::set_speed );
483+ ClassDB::add_property(" GDExample", PropertyInfo(Variant::FLOAT, "speed"), " set_speed", " get_speed" );
486484 }
487485
488- void GDExample::_init () {
486+ void GDExample::GDExample () {
489487 // initialize any variables here
490488 time_passed = 0.0;
491489 amplitude = 10.0;
@@ -507,7 +505,7 @@ showing the methods that have changed so don't remove anything we're omitting:
507505 speed = p_speed;
508506 }
509507
510- float GDExample::get_speed() {
508+ float GDExample::get_speed() const {
511509 return speed;
512510 }
513511
@@ -524,14 +522,10 @@ need to make additional choices based on the state of your object.
524522.. note ::
525523
526524 For simplicity, we've left out the optional parameters in the
527- register_property<class, type> method call. These parameters are
528- ``rpc_mode ``, ``usage ``, ``hint `` and ``hint_string ``. These can be used to
525+ add_property() method call. These parameters are
526+ ``hint ``, ``hint_string ``, ``usage `` and ``class_name ``. These can be used to
529527 further configure how properties are displayed and set on the Godot side.
530528
531- Modern C++ compilers are able to infer the class and variable type and allow
532- you to omit the ``<GDExample, float> `` part of our ``register_property ``
533- method. We've had mixed experiences with this however.
534-
535529Signals
536530-------
537531
0 commit comments