Skip to content

Commit e6294b3

Browse files
committed
Update SConstruct and AddingProperties section
1 parent a4bd105 commit e6294b3

File tree

2 files changed

+57
-135
lines changed

2 files changed

+57
-135
lines changed
Lines changed: 23 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,8 @@
1-
#!python
1+
#!/usr/bin/env python
22
import os
3+
import sys
34

4-
opts = Variables([], ARGUMENTS)
5-
6-
# Gets the standard flags CC, CCX, etc.
7-
env = DefaultEnvironment()
8-
9-
# Define our options
10-
opts.Add(EnumVariable('target', "Compilation target", 'debug', ['d', 'debug', 'r', 'release']))
11-
opts.Add(EnumVariable('platform', "Compilation platform", '', ['', 'windows', 'linuxbsd', 'linux', 'osx']))
12-
opts.Add(EnumVariable('p', "Compilation target, alias for 'platform'", '', ['', 'windows', 'linuxbsd', 'linux', 'osx']))
13-
opts.Add(BoolVariable('use_llvm', "Use the LLVM / Clang compiler", 'no'))
14-
opts.Add(PathVariable('target_path', 'The path where the lib is installed.', 'demo/bin/'))
15-
opts.Add(PathVariable('target_name', 'The library name.', 'libgdexample', PathVariable.PathAccept))
16-
17-
# Local dependency paths, adapt them to your setup
18-
godot_headers_path = "godot-cpp/godot-headers/"
19-
cpp_bindings_path = "godot-cpp/"
20-
cpp_library = "libgodot-cpp"
21-
22-
# only support 64 at this time..
23-
bits = 64
24-
25-
# Updates the environment with the option variables.
26-
opts.Update(env)
27-
28-
# Process some arguments
29-
if env['use_llvm']:
30-
env['CC'] = 'clang'
31-
env['CXX'] = 'clang++'
32-
33-
if env['p'] != '':
34-
env['platform'] = env['p']
35-
36-
if env['platform'] == '':
37-
print("No valid target platform selected.")
38-
quit()
5+
env = SConscript("godot-cpp/SConstruct")
396

407
# For the reference:
418
# - CCFLAGS are compilation flags shared between C and C++
@@ -45,65 +12,26 @@ if env['platform'] == '':
4512
# - CPPDEFINES are for pre-processor defines
4613
# - LINKFLAGS are for linking flags
4714

48-
# Check our platform specifics
49-
if env['platform'] == "osx":
50-
env['target_path'] += 'osx/'
51-
cpp_library += '.osx'
52-
env.Append(CCFLAGS=['-arch', 'x86_64'])
53-
env.Append(CXXFLAGS=['-std=c++17'])
54-
env.Append(LINKFLAGS=['-arch', 'x86_64'])
55-
if env['target'] in ('debug', 'd'):
56-
env.Append(CCFLAGS=['-g', '-O2'])
57-
else:
58-
env.Append(CCFLAGS=['-g', '-O3'])
59-
60-
elif env['platform'] in ('linuxbsd', 'linux'):
61-
env['target_path'] += 'linuxbsd/'
62-
cpp_library += '.linux'
63-
env.Append(CCFLAGS=['-fPIC'])
64-
env.Append(CXXFLAGS=['-std=c++17'])
65-
if env['target'] in ('debug', 'd'):
66-
env.Append(CCFLAGS=['-g3', '-Og'])
67-
else:
68-
env.Append(CCFLAGS=['-g', '-O3'])
69-
70-
elif env['platform'] == "windows":
71-
env['target_path'] += 'win64/'
72-
cpp_library += '.windows'
73-
# This makes sure to keep the session environment variables on windows,
74-
# that way you can run scons in a vs 2017 prompt and it will find all the required tools
75-
env.Append(ENV=os.environ)
76-
77-
env.Append(CPPDEFINES=['WIN32', '_WIN32', '_WINDOWS', '_CRT_SECURE_NO_WARNINGS'])
78-
env.Append(CCFLAGS=['-W3', '-GR'])
79-
env.Append(CXXFLAGS='/std:c++17')
80-
if env['target'] in ('debug', 'd'):
81-
env.Append(CPPDEFINES=['_DEBUG'])
82-
env.Append(CCFLAGS=['-EHsc', '-MDd', '-ZI'])
83-
env.Append(LINKFLAGS=['-DEBUG'])
84-
else:
85-
env.Append(CPPDEFINES=['NDEBUG'])
86-
env.Append(CCFLAGS=['-O2', '-EHsc', '-MD'])
87-
88-
if env['target'] in ('debug', 'd'):
89-
cpp_library += '.debug'
90-
else:
91-
cpp_library += '.release'
92-
93-
cpp_library += '.' + str(bits)
94-
95-
# make sure our binding library is properly includes
96-
env.Append(CPPPATH=['.', godot_headers_path, cpp_bindings_path + 'include/', cpp_bindings_path + 'include/core/', cpp_bindings_path + 'include/gen/'])
97-
env.Append(LIBPATH=[cpp_bindings_path + 'bin/'])
98-
env.Append(LIBS=[cpp_library])
99-
10015
# tweak this if you want to use different folders, or more folders, to store your source code in.
101-
env.Append(CPPPATH=['src/'])
102-
sources = Glob('src/*.cpp')
103-
104-
library = env.SharedLibrary(target=env['target_path'] + env['target_name'] , source=sources)
16+
env.Append(CPPPATH=["src/"])
17+
sources = Glob("src/*.cpp")
18+
19+
if env["platform"] == "macos":
20+
library = env.SharedLibrary(
21+
"demo/bin/osx/libgdexample.{}.framework".format(
22+
env["platform"],
23+
),
24+
source=sources,
25+
)
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+
)
31+
else:
32+
library = env.SharedLibrary(
33+
"demo/bin/linux/libgdexample.{}.{}{}".format(env["platform"], env["arch"], env["SHLIBSUFFIX"]),
34+
source=sources,
35+
)
10536

10637
Default(library)
107-
108-
# Generates help for the -h scons option.
109-
Help(opts.GenerateHelpText(env))

tutorials/scripting/gdextension/gdextension_cpp_example.rst

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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,
370370
which means the data pack won't contain libraries that are incompatible with the
371371
target platform.
372372

373-
TODO: check if dependencies section is supported
374373
Finally, the ``dependencies`` section allows you to name additional dynamic
375374
libraries that should be included as well. This is important when your GDExtension
376375
plugin implements someone else's library and requires you to supply a
@@ -394,40 +393,41 @@ Adding properties
394393
-----------------
395394

396395
GDScript 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

420418
In our ``gdexample.cpp`` file we need to make a number of changes, we will only
421419
show 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+
447455
Once you compile the module with these changes in place, you will see that a
448456
property has been added to our interface. You can now change this property and
449457
when you run your project, you will see that our Godot icon travels along a
450458
larger 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-
462460
Let's do the same but for the speed of our animation and use a setter and getter
463461
function. Our ``gdexample.h`` header file again only needs a few more lines of
464462
code:
@@ -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-
535529
Signals
536530
-------
537531

0 commit comments

Comments
 (0)