@@ -30,6 +30,8 @@ The equivalent C# code has been included in another tab for convenience.
30
30
31
31
.. seealso :: To learn more about C#, head to the :ref:`C# basics <doc_c_sharp>` page.
32
32
33
+ .. seealso :: To learn more about GDExtension and godot-cpp, head to the :ref:`What is GDExtension? <doc_what_is_gdextension>` page.
34
+
33
35
Project setup
34
36
-------------
35
37
@@ -107,6 +109,35 @@ the following line of code:
107
109
{
108
110
}
109
111
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
+
110
141
Every GDScript file is implicitly a class. The ``extends `` keyword defines the
111
142
class this script inherits or extends. In this case, it's ``Sprite2D ``, meaning
112
143
our script will get access to all the properties and functions of the Sprite2D
@@ -150,6 +181,17 @@ Add the following code to your script:
150
181
GD.Print("Hello, world!");
151
182
}
152
183
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
+
153
195
154
196
Let's break it down. The ``func `` keyword defines a new function named
155
197
``_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
188
230
private int _speed = 400;
189
231
private float _angularSpeed = Mathf.Pi;
190
232
233
+ .. code-tab :: cpp C++
234
+
235
+ int speed = 400;
236
+ float angular_speed = Math_PI;
237
+
191
238
Member variables sit near the top of the script, after any "extends" lines,
192
239
but before functions. Every node
193
240
instance 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:
231
278
Rotation += _angularSpeed * (float)delta;
232
279
}
233
280
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
+
234
288
The ``func `` keyword defines a new function. After it, we have to write the
235
289
function's name and arguments it takes in parentheses. A colon ends the
236
290
definition, and the indented blocks that follow are the function's content or
@@ -278,6 +332,14 @@ them.
278
332
279
333
Position += velocity * (float)delta;
280
334
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
+
281
343
As we already saw, the ``var `` keyword defines a new variable. If you put it at
282
344
the top of the script, it defines a property of the class. Inside a function, it
283
345
defines 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.
343
405
Position += velocity * (float)delta;
344
406
}
345
407
}
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