gdextension_cpp_example.rst 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. .. _doc_gdextension_cpp_example:
  2. GDExtension C++ example
  3. =======================
  4. Introduction
  5. ------------
  6. The C++ bindings for GDExtension are built on top of the C GDExtension API
  7. and provide a nicer way to "extend" nodes and other built-in classes in Godot using C++.
  8. This new system allows the extension of Godot to nearly the same
  9. level as statically linked C++ modules.
  10. You can download the included example in the test folder of the godot-cpp
  11. repository `on GitHub <https://github.com/godotengine/godot-cpp>`__.
  12. Setting up the project
  13. ----------------------
  14. There are a few prerequisites you'll need:
  15. - a Godot 4 executable,
  16. - a C++ compiler,
  17. - SCons as a build tool,
  18. - a copy of the `godot-cpp
  19. repository <https://github.com/godotengine/godot-cpp>`__.
  20. See also :ref:`Compiling <toc-devel-compiling>` as the build tools are identical
  21. to the ones you need to compile Godot from source.
  22. You can download the `godot-cpp repository <https://github.com/godotengine/godot-cpp>`__ from GitHub or let Git do the work for you.
  23. Note that this repository has different branches for different versions
  24. of Godot. GDExtensions will not work in older versions of Godot (only Godot 4 and up) and vice versa, so make sure you download the correct branch.
  25. .. note::
  26. To use `GDExtension <https://godotengine.org/article/introducing-gd-extensions>`__
  27. you need to use the godot-cpp branch that matches the version of Godot that you are
  28. targeting. For example, if you're targeting Godot 4.1, use the ``4.1`` branch,
  29. which is what is shown through out this tutorial.
  30. The ``master`` branch is the development branch which is updated regularly
  31. to work with Godot's ``master`` branch.
  32. .. warning::
  33. Our long-term goal is that GDExtensions targeting an earlier version of Godot will work
  34. in later minor versions, but not vice-versa. For example, a GDExtension targeting Godot 4.2
  35. should work just fine in Godot 4.3, but one targeting Godot 4.3 won't work in Godot 4.2.
  36. However, GDExtension is currently *experimental*, which means that we may break compatibility
  37. in order to fix major bugs or include critical features. For example, GDExtensions created
  38. for Godot 4.0 aren't compatible with Godot 4.1
  39. (see :ref:`updating_your_gdextension_for_godot_4_1`).
  40. If you are versioning your project using Git, it is recommended to add it as
  41. a Git submodule:
  42. .. code-block:: none
  43. mkdir gdextension_cpp_example
  44. cd gdextension_cpp_example
  45. git init
  46. git submodule add -b 4.2 https://github.com/godotengine/godot-cpp
  47. cd godot-cpp
  48. git submodule update --init
  49. Alternatively, you can also clone it to the project folder:
  50. .. code-block:: none
  51. mkdir gdextension_cpp_example
  52. cd gdextension_cpp_example
  53. git clone -b 4.2 https://github.com/godotengine/godot-cpp
  54. .. note::
  55. If you decide to download the repository or clone it into your folder,
  56. make sure to keep the folder layout the same as we've setup here. Much of
  57. the code we'll be showcasing here assumes the project has this layout.
  58. If you cloned the example from the link specified in the introduction, the
  59. submodules are not automatically initialized. You will need to execute the
  60. following commands:
  61. .. code-block:: none
  62. cd gdextension_cpp_example
  63. git submodule update --init
  64. This will initialize the repository in your project folder.
  65. Building the C++ bindings
  66. -------------------------
  67. Now that we've downloaded our prerequisites, it is time to build the C++
  68. bindings.
  69. The repository contains a copy of the metadata for the current Godot release,
  70. but if you need to build these bindings for a newer version of Godot, simply
  71. call the Godot executable:
  72. .. code-block:: none
  73. godot --dump-extension-api
  74. The resulting ``extension_api.json`` file will be created in the executable's
  75. directory. Copy it to the project folder and add ``custom_api_file=<PATH_TO_FILE>``
  76. to the scons command below.
  77. To generate and compile the bindings, use this command (replacing ``<platform>``
  78. with ``windows``, ``linux`` or ``macos`` depending on your OS):
  79. The build process automatically detects the number of CPU threads to use for
  80. parallel builds. To specify a number of CPU threads to use, add ``-jN`` at the
  81. end of the SCons command line where ``N`` is the number of CPU threads to use.
  82. .. code-block:: none
  83. cd godot-cpp
  84. scons platform=<platform> custom_api_file=<PATH_TO_FILE>
  85. cd ..
  86. This step will take a while. When it is completed, you should have static
  87. libraries that can be compiled into your project stored in ``godot-cpp/bin/``.
  88. .. note::
  89. You may need to add ``bits=64`` to the command on Windows or Linux.
  90. Creating a simple plugin
  91. ------------------------
  92. Now it's time to build an actual plugin. We'll start by creating an empty Godot
  93. project in which we'll place a few files.
  94. Open Godot and create a new project. For this example, we will place it in a
  95. folder called ``demo`` inside our GDExtension's folder structure.
  96. In our demo project, we'll create a scene containing a Node called "Main" and
  97. we'll save it as ``main.tscn``. We'll come back to that later.
  98. Back in the top-level GDExtension module folder, we're also going to create a
  99. subfolder called ``src`` in which we'll place our source files.
  100. You should now have ``demo``, ``godot-cpp``, and ``src``
  101. directories in your GDExtension module.
  102. Your folder structure should now look like this:
  103. .. code-block:: none
  104. gdextension_cpp_example/
  105. |
  106. +--demo/ # game example/demo to test the extension
  107. |
  108. +--godot-cpp/ # C++ bindings
  109. |
  110. +--src/ # source code of the extension we are building
  111. In the ``src`` folder, we'll start with creating our header file for the
  112. GDExtension node we'll be creating. We will name it ``gdexample.h``:
  113. .. code-block:: cpp
  114. #ifndef GDEXAMPLE_H
  115. #define GDEXAMPLE_H
  116. #include <godot_cpp/classes/sprite2d.hpp>
  117. namespace godot {
  118. class GDExample : public Sprite2D {
  119. GDCLASS(GDExample, Sprite2D)
  120. private:
  121. double time_passed;
  122. protected:
  123. static void _bind_methods();
  124. public:
  125. GDExample();
  126. ~GDExample();
  127. void _process(double delta) override;
  128. };
  129. }
  130. #endif
  131. There are a few things of note to the above. We include ``sprite2d.hpp`` which
  132. contains bindings to the Sprite2D class. We'll be extending this class in our
  133. module.
  134. We're using the namespace ``godot``, since everything in GDExtension is defined
  135. within this namespace.
  136. Then we have our class definition, which inherits from our Sprite2D through a
  137. container class. We'll see a few side effects of this later on. The
  138. ``GDCLASS`` macro sets up a few internal things for us.
  139. After that, we declare a single member variable called ``time_passed``.
  140. In the next block we're defining our methods, we have our constructor
  141. and destructor defined, but there are two other functions that will likely look
  142. familiar to some, and one new method.
  143. The first is ``_bind_methods``, which is a static function that Godot will
  144. call to find out which methods can be called and which properties it exposes.
  145. The second is our ``_process`` function, which will work exactly the same
  146. as the ``_process`` function you're used to in GDScript.
  147. Let's implement our functions by creating our ``gdexample.cpp`` file:
  148. .. code-block:: cpp
  149. #include "gdexample.h"
  150. #include <godot_cpp/core/class_db.hpp>
  151. using namespace godot;
  152. void GDExample::_bind_methods() {
  153. }
  154. GDExample::GDExample() {
  155. // Initialize any variables here.
  156. time_passed = 0.0;
  157. }
  158. GDExample::~GDExample() {
  159. // Add your cleanup here.
  160. }
  161. void GDExample::_process(double delta) {
  162. time_passed += delta;
  163. Vector2 new_position = Vector2(10.0 + (10.0 * sin(time_passed * 2.0)), 10.0 + (10.0 * cos(time_passed * 1.5)));
  164. set_position(new_position);
  165. }
  166. This one should be straightforward. We're implementing each method of our class
  167. that we defined in our header file.
  168. Note our ``_process`` function, which keeps track of how much time has passed
  169. and calculates a new position for our sprite using a sine and cosine function.
  170. There is one more C++ file we need; we'll name it ``register_types.cpp``. Our
  171. GDExtension plugin can contain multiple classes, each with their own header
  172. and source file like we've implemented ``GDExample`` up above. What we need now
  173. is a small bit of code that tells Godot about all the classes in our
  174. GDExtension plugin.
  175. .. code-block:: cpp
  176. #include "register_types.h"
  177. #include "gdexample.h"
  178. #include <gdextension_interface.h>
  179. #include <godot_cpp/core/defs.hpp>
  180. #include <godot_cpp/godot.hpp>
  181. using namespace godot;
  182. void initialize_example_module(ModuleInitializationLevel p_level) {
  183. if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
  184. return;
  185. }
  186. GDREGISTER_CLASS(GDExample);
  187. }
  188. void uninitialize_example_module(ModuleInitializationLevel p_level) {
  189. if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
  190. return;
  191. }
  192. }
  193. extern "C" {
  194. // Initialization.
  195. GDExtensionBool GDE_EXPORT example_library_init(GDExtensionInterfaceGetProcAddress p_get_proc_address, const GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) {
  196. godot::GDExtensionBinding::InitObject init_obj(p_get_proc_address, p_library, r_initialization);
  197. init_obj.register_initializer(initialize_example_module);
  198. init_obj.register_terminator(uninitialize_example_module);
  199. init_obj.set_minimum_library_initialization_level(MODULE_INITIALIZATION_LEVEL_SCENE);
  200. return init_obj.init();
  201. }
  202. }
  203. The ``initialize_example_module`` and ``uninitialize_example_module`` functions get
  204. called respectively when Godot loads our plugin and when it unloads it. All
  205. we're doing here is parse through the functions in our bindings module to
  206. initialize them, but you might have to set up more things depending on your
  207. needs. We call the function ``register_class`` for each of our classes in our library.
  208. The important function is the third function called ``example_library_init``.
  209. We first call a function in our bindings library that creates an initialization object.
  210. This object registers the initialization and termination functions of the GDExtension.
  211. Furthermore, it sets the level of initialization (core, servers, scene, editor, level).
  212. At last, we need the header file for the ``register_types.cpp`` named
  213. ``register_types.h``.
  214. .. code-block:: cpp
  215. #ifndef GDEXAMPLE_REGISTER_TYPES_H
  216. #define GDEXAMPLE_REGISTER_TYPES_H
  217. #include <godot_cpp/core/class_db.hpp>
  218. using namespace godot;
  219. void initialize_example_module(ModuleInitializationLevel p_level);
  220. void uninitialize_example_module(ModuleInitializationLevel p_level);
  221. #endif // GDEXAMPLE_REGISTER_TYPES_H
  222. Compiling the plugin
  223. --------------------
  224. We cannot easily write by hand a ``SConstruct`` file that SCons would use for
  225. building. For the purpose of this example, just use
  226. :download:`this hardcoded SConstruct file <files/cpp_example/SConstruct>` we've
  227. prepared. We'll cover a more customizable, detailed example on how to use these
  228. build files in a subsequent tutorial.
  229. .. note::
  230. This ``SConstruct`` file was written to be used with the latest ``godot-cpp``
  231. master, you may need to make small changes using it with older versions or
  232. refer to the ``SConstruct`` file in the Godot 4.0 documentation.
  233. Once you've downloaded the ``SConstruct`` file, place it in your GDExtension folder
  234. structure alongside ``godot-cpp``, ``src`` and ``demo``, then run:
  235. .. code-block:: bash
  236. scons platform=<platform>
  237. You should now be able to find the module in ``demo/bin/<platform>``.
  238. .. note::
  239. Here, we've compiled both godot-cpp and our gdexample library as debug
  240. builds. For optimized builds, you should compile them using the
  241. ``target=template_release`` switch.
  242. Using the GDExtension module
  243. ----------------------------
  244. Before we jump back into Godot, we need to create one more file in
  245. ``demo/bin/``.
  246. This file lets Godot know what dynamic libraries should be
  247. loaded for each platform and the entry function for the module. It is called ``gdexample.gdextension``.
  248. .. code-block:: none
  249. [configuration]
  250. entry_symbol = "example_library_init"
  251. compatibility_minimum = "4.2"
  252. [libraries]
  253. macos.debug = "res://bin/libgdexample.macos.template_debug.framework"
  254. macos.release = "res://bin/libgdexample.macos.template_release.framework"
  255. windows.debug.x86_32 = "res://bin/libgdexample.windows.template_debug.x86_32.dll"
  256. windows.release.x86_32 = "res://bin/libgdexample.windows.template_release.x86_32.dll"
  257. windows.debug.x86_64 = "res://bin/libgdexample.windows.template_debug.x86_64.dll"
  258. windows.release.x86_64 = "res://bin/libgdexample.windows.template_release.x86_64.dll"
  259. linux.debug.x86_64 = "res://bin/libgdexample.linux.template_debug.x86_64.so"
  260. linux.release.x86_64 = "res://bin/libgdexample.linux.template_release.x86_64.so"
  261. linux.debug.arm64 = "res://bin/libgdexample.linux.template_debug.arm64.so"
  262. linux.release.arm64 = "res://bin/libgdexample.linux.template_release.arm64.so"
  263. linux.debug.rv64 = "res://bin/libgdexample.linux.template_debug.rv64.so"
  264. linux.release.rv64 = "res://bin/libgdexample.linux.template_release.rv64.so"
  265. android.debug.x86_64 = "res://bin/libgdexample.android.template_debug.x86_64.so"
  266. android.release.x86_64 = "res://bin/libgdexample.android.template_release.x86_64.so"
  267. android.debug.arm64 = "res://bin/libgdexample.android.template_debug.arm64.so"
  268. android.release.arm64 = "res://bin/libgdexample.android.template_release.arm64.so"
  269. This file contains a ``configuration`` section that controls the entry function of the module.
  270. You should also set the minimum compatible Godot version with ``compatability_minimum``,
  271. which prevents older version of Godot from trying to load your extension.
  272. The ``libraries`` section is the important bit: it tells Godot the location of the
  273. dynamic library in the project's filesystem for each supported platform. It will
  274. also result in *just* that file being exported when you export the project,
  275. which means the data pack won't contain libraries that are incompatible with the
  276. target platform.
  277. Finally, the ``dependencies`` section allows you to name additional dynamic
  278. libraries that should be included as well. This is important when your GDExtension
  279. plugin implements someone else's library and requires you to supply a
  280. third-party dynamic library with your project.
  281. Here is another overview to check the correct file structure:
  282. .. code-block:: none
  283. gdextension_cpp_example/
  284. |
  285. +--demo/ # game example/demo to test the extension
  286. | |
  287. | +--main.tscn
  288. | |
  289. | +--bin/
  290. | |
  291. | +--gdexample.gdextension
  292. |
  293. +--godot-cpp/ # C++ bindings
  294. |
  295. +--src/ # source code of the extension we are building
  296. | |
  297. | +--register_types.cpp
  298. | +--register_types.h
  299. | +--gdexample.cpp
  300. | +--gdexample.h
  301. Time to jump back into Godot. We load up the main scene we created way back in
  302. the beginning and now add a newly available GDExample node to the scene:
  303. .. image:: img/gdextension_cpp_nodes.webp
  304. We're going to assign the Godot logo to this node as our texture, disable the
  305. ``centered`` property:
  306. .. image:: img/gdextension_cpp_sprite.webp
  307. We're finally ready to run the project:
  308. .. image:: img/gdextension_cpp_animated.gif
  309. Adding properties
  310. -----------------
  311. GDScript allows you to add properties to your script using the ``export``
  312. keyword. In GDExtension you have to register the properties with a getter and
  313. setter function or directly implement the ``_get_property_list``, ``_get`` and
  314. ``_set`` methods of an object (but that goes far beyond the scope of this
  315. tutorial).
  316. Lets add a property that allows us to control the amplitude of our wave.
  317. In our ``gdexample.h`` file we need to add a member variable and getter and setter
  318. functions:
  319. .. code-block:: cpp
  320. ...
  321. private:
  322. double time_passed;
  323. double amplitude;
  324. public:
  325. void set_amplitude(const double p_amplitude);
  326. double get_amplitude() const;
  327. ...
  328. In our ``gdexample.cpp`` file we need to make a number of changes, we will only
  329. show the methods we end up changing, don't remove the lines we're omitting:
  330. .. code-block:: cpp
  331. void GDExample::_bind_methods() {
  332. ClassDB::bind_method(D_METHOD("get_amplitude"), &GDExample::get_amplitude);
  333. ClassDB::bind_method(D_METHOD("set_amplitude", "p_amplitude"), &GDExample::set_amplitude);
  334. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "amplitude"), "set_amplitude", "get_amplitude");
  335. }
  336. GDExample::GDExample() {
  337. // Initialize any variables here.
  338. time_passed = 0.0;
  339. amplitude = 10.0;
  340. }
  341. void GDExample::_process(double delta) {
  342. time_passed += delta;
  343. Vector2 new_position = Vector2(
  344. amplitude + (amplitude * sin(time_passed * 2.0)),
  345. amplitude + (amplitude * cos(time_passed * 1.5))
  346. );
  347. set_position(new_position);
  348. }
  349. void GDExample::set_amplitude(const double p_amplitude) {
  350. amplitude = p_amplitude;
  351. }
  352. double GDExample::get_amplitude() const {
  353. return amplitude;
  354. }
  355. Once you compile the module with these changes in place, you will see that a
  356. property has been added to our interface. You can now change this property and
  357. when you run your project, you will see that our Godot icon travels along a
  358. larger figure.
  359. Let's do the same but for the speed of our animation and use a setter and getter
  360. function. Our ``gdexample.h`` header file again only needs a few more lines of
  361. code:
  362. .. code-block:: cpp
  363. ...
  364. double amplitude;
  365. double speed;
  366. ...
  367. void _process(double delta) override;
  368. void set_speed(const double p_speed);
  369. double get_speed() const;
  370. ...
  371. This requires a few more changes to our ``gdexample.cpp`` file, again we're only
  372. showing the methods that have changed so don't remove anything we're omitting:
  373. .. code-block:: cpp
  374. void GDExample::_bind_methods() {
  375. ...
  376. ClassDB::bind_method(D_METHOD("get_speed"), &GDExample::get_speed);
  377. ClassDB::bind_method(D_METHOD("set_speed", "p_speed"), &GDExample::set_speed);
  378. ADD_PROPERTY("GDExample", PropertyInfo(Variant::FLOAT, "speed", PROPERTY_HINT_RANGE, "0,20,0.01"), "set_speed", "get_speed");
  379. }
  380. GDExample::GDExample() {
  381. time_passed = 0.0;
  382. amplitude = 10.0;
  383. speed = 1.0;
  384. }
  385. void GDExample::_process(double delta) {
  386. time_passed += speed * delta;
  387. Vector2 new_position = Vector2(
  388. amplitude + (amplitude * sin(time_passed * 2.0)),
  389. amplitude + (amplitude * cos(time_passed * 1.5))
  390. );
  391. set_position(new_position);
  392. }
  393. ...
  394. void GDExample::set_speed(const double p_speed) {
  395. speed = p_speed;
  396. }
  397. double GDExample::get_speed() const {
  398. return speed;
  399. }
  400. Now when the project is compiled, we'll see another property called speed.
  401. Changing its value will make the animation go faster or slower.
  402. Furthermore, we added a property range which describes in which range the value can be.
  403. The first two arguments are the minimum and maximum value and the third is the step size.
  404. .. note::
  405. For simplicity, we've only used the hint_range of the property method.
  406. There are a lot more options to choose from. These can be used to
  407. further configure how properties are displayed and set on the Godot side.
  408. Signals
  409. -------
  410. Last but not least, signals fully work in GDExtension as well. Having your extension
  411. react to a signal given out by another object requires you to call ``connect``
  412. on that object. We can't think of a good example for our wobbling Godot icon, we
  413. would need to showcase a far more complete example.
  414. This is the required syntax:
  415. .. code-block:: cpp
  416. some_other_node->connect("the_signal", Callable(this, "my_method"));
  417. To connect our signal ``the_signal`` from some other node with our method
  418. ``my_method``, we need to provide the ``connect`` method with the name of the signal
  419. and a ``Callable``. The ``Callable`` holds information about an object on which a method
  420. can be called. In our case, it associates our current object instance ``this`` with the
  421. method ``my_method`` of the object. Then the ``connect`` method will add this to the
  422. observers of ``the_signal``. Whenever ``the_signal`` is now emitted, Godot knows which
  423. method of which object it needs to call.
  424. Note that you can only call ``my_method`` if you've previously registered it in
  425. your ``_bind_methods`` method. Otherwise Godot will not know about the existence
  426. of ``my_method``.
  427. To learn more about ``Callable``, check out the class reference here: :ref:`Callable <class_Callable>`.
  428. Having your object sending out signals is more common. For our wobbling
  429. Godot icon, we'll do something silly just to show how it works. We're going to
  430. emit a signal every time a second has passed and pass the new location along.
  431. In our ``gdexample.h`` header file, we need to define a new member ``time_emit``:
  432. .. code-block:: cpp
  433. ...
  434. double time_passed;
  435. double time_emit;
  436. double amplitude;
  437. ...
  438. This time, the changes in ``gdexample.cpp`` are more elaborate. First,
  439. you'll need to set ``time_emit = 0.0;`` in either our ``_init`` method or in our
  440. constructor. We'll look at the other 2 needed changes one by one.
  441. In our ``_bind_methods`` method, we need to declare our signal. This is done
  442. as follows:
  443. .. code-block:: cpp
  444. void GDExample::_bind_methods() {
  445. ...
  446. ADD_PROPERTY("GDExample", PropertyInfo(Variant::FLOAT, "speed", PROPERTY_HINT_RANGE, "0,20,0.01"), "set_speed", "get_speed");
  447. ADD_SIGNAL(MethodInfo("position_changed", PropertyInfo(Variant::OBJECT, "node"), PropertyInfo(Variant::VECTOR2, "new_pos")));
  448. }
  449. Here, our ``ADD_SIGNAL`` macro can be a single call with a ``MethodInfo`` argument.
  450. ``MethodInfo``'s first parameter will be the signal's name, and its remaining parameters
  451. are ``PropertyInfo`` types which describe the essentials of each of the method's parameters.
  452. ``PropertyInfo`` parameters are defined with the data type of the parameter, and then the name
  453. that the parameter will have by default.
  454. So here, we add a signal, with a ``MethodInfo`` which names the signal "position_changed". The
  455. ``PropertyInfo`` parameters describe two essential arguments, one of type ``Object``, the other
  456. of type ``Vector2``, respectively named "node" and "new_pos".
  457. Next, we'll need to change our ``_process`` method:
  458. .. code-block:: cpp
  459. void GDExample::_process(double delta) {
  460. time_passed += speed * delta;
  461. Vector2 new_position = Vector2(
  462. amplitude + (amplitude * sin(time_passed * 2.0)),
  463. amplitude + (amplitude * cos(time_passed * 1.5))
  464. );
  465. set_position(new_position);
  466. time_emit += delta;
  467. if (time_emit > 1.0) {
  468. emit_signal("position_changed", this, new_position);
  469. time_emit = 0.0;
  470. }
  471. }
  472. After a second has passed, we emit our signal and reset our counter. We can add
  473. our parameter values directly to ``emit_signal``.
  474. Once the GDExtension library is compiled, we can go into Godot and select our sprite
  475. node. In the **Node** dock, we can find our new signal and link it up by pressing
  476. the **Connect** button or double-clicking the signal. We've added a script on
  477. our main node and implemented our signal like this:
  478. .. code-block:: gdscript
  479. extends Node
  480. func _on_Sprite2D_position_changed(node, new_pos):
  481. print("The position of " + node.get_class() + " is now " + str(new_pos))
  482. Every second, we output our position to the console.
  483. Next steps
  484. ----------
  485. We hope the above example showed you the basics. You can
  486. build upon this example to create full-fledged scripts to control nodes in Godot
  487. using C++.