@@ -30,6 +30,8 @@ The equivalent C# code has been included in another tab for convenience.
3030
3131.. seealso :: To learn more about C#, head to the :ref:`C# basics <doc_c_sharp>` page.
3232
33+ .. seealso :: To learn more about GDExtension and godot-cpp, head to the :ref:`What is GDExtension? <doc_what_is_gdextension>` page.
34+
3335Project setup
3436-------------
3537
@@ -107,6 +109,35 @@ the following line of code:
107109 {
108110 }
109111
112+ .. code-tab :: cpp C++
113+
114+ #ifndef MY_SPRITE_2D_H
115+ #define MY_SPRITE_2D_H
116+
117+ #include <godot_cpp/classes/sprite2d.hpp>
118+
119+ using namespace godot;
120+
121+ namespace CustomNamespace {
122+
123+ // MySprite2D also needs to be bound in your GDExtension's register_types.cpp file.
124+ // More info on this in the GDExtension example setup tutorial.
125+ class MySprite2D : public Sprite2D {
126+ GDCLASS(MySprite2D, Sprite2D)
127+
128+ protected:
129+ // _bind_methods is required for all GDCLASS's or compilation will fail!
130+ static void _bind_methods() {}
131+
132+ public:
133+ // Default constructor with no parameters is required or compilation will fail!
134+ MySprite2D() {}
135+ };
136+
137+ } // CustomNamespace
138+
139+ #endif // MY_SPRITE_2D_H
140+
110141Every GDScript file is implicitly a class. The ``extends `` keyword defines the
111142class this script inherits or extends. In this case, it's ``Sprite2D ``, meaning
112143our script will get access to all the properties and functions of the Sprite2D
@@ -150,6 +181,17 @@ Add the following code to your script:
150181 GD.Print("Hello, world!");
151182 }
152183
184+ .. code-tab :: cpp C++
185+
186+ // Add this include at the top of your header file.
187+ #include <godot_cpp/variant/utility_functions.hpp>
188+
189+ // Add this inside your MySprite2D class.
190+ public:
191+ MySprite2D() {
192+ UtilityFunctions::print("Hello, world!");
193+ }
194+
153195
154196Let's break it down. The ``func `` keyword defines a new function named
155197``_init ``. This is a special name for our class's constructor. The engine calls
@@ -188,6 +230,11 @@ angular speed in radians per second. Add the following after the ``extends Spri
188230 private int _speed = 400;
189231 private float _angularSpeed = Mathf.Pi;
190232
233+ .. code-tab :: cpp C++
234+
235+ int speed = 400;
236+ float angular_speed = Math_PI;
237+
191238Member variables sit near the top of the script, after any "extends" lines,
192239but before functions. Every node
193240instance with this script attached to it will have its own copy of the ``speed ``
@@ -231,6 +278,13 @@ At the bottom of the script, define the function:
231278 Rotation += _angularSpeed * (float)delta;
232279 }
233280
281+ .. code-tab :: cpp C++
282+
283+ void _process(double p_delta) override {
284+ // Note that properties (like rotation) are accessed via setters and getters in godot-cpp.
285+ set_rotation(get_rotation() + angular_speed * p_delta);
286+ }
287+
234288The ``func `` keyword defines a new function. After it, we have to write the
235289function's name and arguments it takes in parentheses. A colon ends the
236290definition, and the indented blocks that follow are the function's content or
@@ -278,6 +332,14 @@ them.
278332
279333 Position += velocity * (float)delta;
280334
335+ .. code-tab :: cpp C++
336+
337+ // Note that the directional Vector2 constants do not exist in godot-cpp. So Vector2(0, -1) must be used.
338+ Vector2 velocity = Vector2(0, -1).rotated(get_rotation()) * speed;
339+
340+ set_position(get_position() + velocity * p_delta);
341+
342+
281343As we already saw, the ``var `` keyword defines a new variable. If you put it at
282344the top of the script, it defines a property of the class. Inside a function, it
283345defines a local variable: it only exists within the function's scope.
@@ -343,3 +405,39 @@ Here is the complete ``sprite_2d.gd`` file for reference.
343405 Position += velocity * (float)delta;
344406 }
345407 }
408+
409+ .. code-tab :: cpp C++
410+
411+ #ifndef MY_SPRITE_2D_H
412+ #define MY_SPRITE_2D_H
413+
414+ #include <godot_cpp/classes/sprite2d.hpp>
415+ #include <godot_cpp/core/math.hpp>
416+
417+ using namespace godot;
418+
419+ namespace CustomNamespace {
420+
421+ class MySprite2D : public Sprite2D {
422+ GDCLASS(MySprite2D, Sprite2D)
423+
424+ int speed = 400;
425+ float angular_speed = Math_PI;
426+
427+ protected:
428+ static void _bind_methods() {}
429+
430+ public:
431+ MySprite2D() {}
432+
433+ void _process(double p_delta) override {
434+ set_rotation(get_rotation() + angular_speed * p_delta);
435+
436+ Vector2 velocity = Vector2(0, -1).rotated(get_rotation()) * speed;
437+ set_position(get_position() + velocity * p_delta);
438+ }
439+ };
440+
441+ } // CustomNamespace
442+
443+ #endif // MY_SPRITE_2D_H
0 commit comments