Skip to content

Commit caa995b

Browse files
committed
updated with suggestions
1 parent f234f44 commit caa995b

File tree

3 files changed

+24
-47
lines changed

3 files changed

+24
-47
lines changed

tutorials/scripting/gdextension/files/cpp_example/SConstruct

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,14 @@ sources = Glob("src/*.cpp")
1818

1919
if env["platform"] == "macos":
2020
library = env.SharedLibrary(
21-
"demo/bin/osx/libgdexample.{}.framework".format(
22-
env["platform"],
21+
"demo/bin/libgdexample.{}.{}.framework/libgdexample.{}.{}".format(
22+
env["platform"], env["target"], env["platform"], env["target"]
2323
),
2424
source=sources,
2525
)
26-
elif env["platform"] == "windows":
27-
library = env.SharedLibrary(
28-
"demo/bin/win64/libgdexample.{}.{}{}".format(env["platform"], env["arch"], env["SHLIBSUFFIX"]),
29-
source=sources,
30-
)
3126
else:
3227
library = env.SharedLibrary(
33-
"demo/bin/linux/libgdexample.{}.{}{}".format(env["platform"], env["arch"], env["SHLIBSUFFIX"]),
28+
"demo/bin/libgdexample{}{}".format(env["suffix"], env["SHLIBSUFFIX"]),
3429
source=sources,
3530
)
3631

tutorials/scripting/gdextension/gdextension_cpp_example.rst

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Introduction
77
------------
88

99
The C++ bindings for GDExtension are built on top of the C GDExtension API
10-
and provide a nicer way to "extend" nodes in Godot using C++.
10+
and provide a nicer way to "extend" nodes and other built-in classes in Godot using C++.
1111
This new system allows the extension of Godot to nearly the same
1212
level as statically linked C++ modules.
1313

@@ -34,14 +34,12 @@ of Godot. GDExtensions will not work in older versions of Godot (only Godot 4 an
3434

3535
.. note::
3636

37-
`GDExtension <https://godotengine.org/article/introducing-gd-extensions>`__
38-
has been merged in the ``master`` branch of godot-cpp,
39-
but it is only compatible with the upcoming Godot 4.0.
40-
Therefore, you need to use the ``master`` branch of godot-cpp to use GDExtension
41-
and follow this example.
37+
To use `GDExtension <https://godotengine.org/article/introducing-gd-extensions>`__
38+
you need to use the ``master`` branch of godot-cpp,
39+
which is only compatible with Godot 4.0 and follow this example.
4240

43-
If you are versioning your project using Git, it is a good idea to add them as
44-
Git submodules:
41+
If you are versioning your project using Git, it is a good idea to add it as
42+
a Git submodule:
4543

4644
.. code-block:: none
4745
@@ -63,16 +61,11 @@ Do make sure you clone recursively to pull in both repositories:
6361
6462
mkdir gdextension_cpp_example
6563
cd gdextension_cpp_example
66-
git clone --recursive -b master https://github.com/godotengine/godot-cpp
64+
git clone -b master https://github.com/godotengine/godot-cpp
6765
6866
.. note::
6967

70-
``godot-cpp`` now includes ``godot-headers`` as a nested submodule, if you've
71-
manually downloaded them please make sure to place ``godot-headers`` inside
72-
of the ``godot-cpp`` folder.
73-
74-
You don't have to do it this way, but we've found it easiest to manage. If you
75-
decide to download the repositories or clone them into your folder,
68+
If you decide to download the repository or clone it into your folder,
7669
make sure to keep the folder layout the same as we've setup here. Much of
7770
the code we'll be showcasing here assumes the project has this layout.
7871

@@ -83,9 +76,9 @@ following commands:
8376
.. code-block:: none
8477
8578
cd gdextension_cpp_example
86-
git submodule update --init --recursive
79+
git submodule update --init
8780
88-
This will clone these two repositories into your project folder.
81+
This will initialize the repository in your project folder.
8982

9083
Building the C++ bindings
9184
-------------------------
@@ -179,8 +172,6 @@ GDExtension node we'll be creating. We will name it ``gdexample.h``:
179172
GDExample();
180173
~GDExample();
181174

182-
void _init(); // our initializer called by Godot
183-
184175
void _process(float delta);
185176
};
186177

@@ -208,9 +199,7 @@ familiar to some, and one new method.
208199
The first is ``_bind_methods``, which is a static function that Godot will
209200
call to find out which methods can be called and which properties it exposes.
210201
The second is our ``_process`` function, which will work exactly the same
211-
as the ``_process`` function you're used to in GDScript. The third is
212-
our ``_init`` function which is called after Godot has properly set up
213-
our object.
202+
as the ``_process`` function you're used to in GDScript.
214203

215204
Let's implement our functions by creating our ``gdexample.cpp`` file:
216205

@@ -222,7 +211,6 @@ Let's implement our functions by creating our ``gdexample.cpp`` file:
222211
using namespace godot;
223212

224213
void GDExample::_bind_methods() {
225-
ClassDB::bind_method(D_METHOD("_process"), &GDExample::_process);
226214
}
227215

228216
GDExample::GDExample() {
@@ -247,9 +235,6 @@ that we defined in our header file.
247235

248236
Note our ``_process`` function, which keeps track of how much time has passed
249237
and calculates a new position for our sprite using a sine and cosine function.
250-
What stands out is calling ``owner->set_position`` to call one of the built-in
251-
methods of our Sprite2D. This is because our class is a container
252-
class; ``owner`` points to the actual Sprite2D node our script relates to.
253238

254239
There is one more C++ file we need; we'll name it ``register_types.cpp``. Our
255240
GDExtension plugin can contain multiple classes, each with their own header
@@ -369,9 +354,9 @@ loaded for each platform and the entry function for the module. It is called ``g
369354
370355
[libraries]
371356
372-
linux.64="res://bin/linux/libgdexample.linux.64.so"
373-
windows.x86_64="res://bin/win64/libgdexample.windows.x86_64.dll"
374-
macos="res://bin/osx/libgdexample.macos.framework"
357+
linux.64="res://bin/libgdexample.linux.64.so"
358+
windows.x86_64="res://bin/libgdexample.windows.x86_64.dll"
359+
macos="res://bin/libgdexample.macos.framework"
375360
376361
This file contains a ``configuration`` section that controls the entry function of the module.
377362

@@ -428,7 +413,7 @@ Adding properties
428413

429414
GDScript allows you to add properties to your script using the ``export``
430415
keyword. In GDExtension you have to register the properties with a getter and
431-
sett function or directly implement the ``_get_property_list``, ``_get`` and
416+
setter function or directly implement the ``_get_property_list``, ``_get`` and
432417
``_set`` methods of an object (but that goes far beyond the scope of this
433418
tutorial.
434419

@@ -457,7 +442,6 @@ show the methods we end up changing, don't remove the lines we're omitting:
457442
void GDExample::_bind_methods() {
458443
ClassDB::bind_method(D_METHOD("get_amplitude"), &GDExample::get_amplitude);
459444
ClassDB::bind_method(D_METHOD("set_amplitude", "p_amplitude"), &GDExample::set_amplitude);
460-
461445
ClassDB::add_property("GDExample", PropertyInfo(Variant::FLOAT, "amplitude"), "set_amplitude", "get_amplitude");
462446
}
463447

@@ -512,6 +496,7 @@ showing the methods that have changed so don't remove anything we're omitting:
512496
.. code-block:: C++
513497

514498
void GDExample::_bind_methods() {
499+
...
515500
ClassDB::bind_method(D_METHOD("get_speed"), &GDExample::get_speed);
516501
ClassDB::bind_method(D_METHOD("set_speed", "p_speed"), &GDExample::set_speed);
517502
ClassDB::add_property("GDExample", PropertyInfo(Variant::FLOAT, "speed"), "set_speed", "get_speed");
@@ -535,6 +520,8 @@ showing the methods that have changed so don't remove anything we're omitting:
535520
set_position(new_position);
536521
}
537522

523+
...
524+
538525
void GDExample::set_speed(float p_speed) {
539526
speed = p_speed;
540527
}

tutorials/scripting/gdextension/what_is_gdextension.rst

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ at run-time. You can use it to run native code without compiling it with the eng
1414
:ref:`GDScript <doc_gdscript>`.
1515

1616
Differences between GDExtension and C++ modules
17-
--------------------------------------------
17+
-----------------------------------------------
1818

1919
You can use both GDExtension and :ref:`C++ modules <doc_custom_modules_in_c++>` to
2020
run C or C++ code in a Godot project.
@@ -23,11 +23,11 @@ They also both allow you to integrate third-party libraries into Godot. The one
2323
you should choose depends on your needs.
2424

2525
Advantages of GDExtension
26-
^^^^^^^^^^^^^^^^^^^^^^
26+
^^^^^^^^^^^^^^^^^^^^^^^^^
2727

2828
Unlike modules, GDExtension doesn't require compiling the engine's source code,
2929
making it easier to distribute your work. It gives you access to most of the API
30-
available to GDScript C#, allowing you to code game logic with full control
30+
available to GDScript and C#, allowing you to code game logic with full control
3131
regarding performance. It's ideal if you need high-performance code you'd like
3232
to distribute as an add-on in the :ref:`asset library <doc_what_is_assetlib>`.
3333

@@ -54,8 +54,6 @@ GDExtension isn't enough:
5454
static modules
5555
- You can use C++ modules to provide additional features in a project without
5656
carrying native library files around. This extends to exported projects.
57-
(- C++ modules can be faster than GDExtension, especially when the code requires a
58-
lot of communication through the scripting API.) -> TODO: check if this still holds up
5957

6058
Supported languages
6159
-------------------
@@ -90,9 +88,6 @@ The bindings below are developed and maintained by the community:
9088
Version compatibility
9189
---------------------
9290

93-
(:ref:`Unlike Godot itself <doc_release_policy>`, GDExtension has stricter version
94-
compatibility requirements as it relies on low-level *ptrcalls* to function.) -> TODO: check up on this
95-
9691
GDExtension add-ons compiled for a given Godot version are only guaranteed to work
9792
with the same minor release series. For example, a GDExtension add-on compiled for
9893
Godot 4.0 will only work with Godot 4.0, 4.0.1, 4.0.2. In addition, GDExtension is

0 commit comments

Comments
 (0)