This is an attempt to make the lifecycle of wrapped objects clearer.
Godot keeps track of bindings' userdata for each object it creates.
This allows allocating the memory of the wrapper only once per object
even if that object is passed multiple times between binding code and
godot code.
The binding information is composed of multiple functions, this includes
a callback for when the userdata is to be allocated (called once) and
for when the userdata is to be deallocated (again, called once).
When allocating data with "memnew" we set the object bindings during the
postinitialize phase, but surely we shouldn't do that when allocating
the userdata as a result of bindings callback themselves.
Additionally, since we let Godot handle (and track) raw memory
allocation and de-allocation, we need to manually call the deconstructor
of the wrapper class during the free callback, to ensure that its
non-trivial members are correctly de-initialized.
This commit changes the platform detection order to detect mingw
compiling for windows (which defines `__GNUC__`).
This commit also wraps the definition around a guard so it can be
overridden via a define at build-time.
The returned value "ret" may be nullptr in which case the code would crash because "object_get_instance_binding" can't be called on nullptr input.
This should be very easy to reproduce, I encountered it pretty much any time I called a method that returned Ref<Something>. E.g.:
Ref<GeometryInstance3D> instance;
instance.instantiate();
instance.get_mesh(); // Crash because no mesh was set for the instance and the returned value was nullptr.
Proper initialization for godot-cpp classes with memnew.
Extension classes (i.e. the `GDCLASS` macro) behave differently from
regular wrapped classes, and requires Godot to initialize them during
object construction.
This commit update the GDCLASS macro to not create/destroy the instance
during the bindings callback, but during the extension callbacks.
When setting the object instance, the bindings instance is set to the
pointer of the extension instance so that it can later be retrieved
normally via `object_get_instance_bindings`.
This makes sure custom constructors are always called on extension
classes. However, note that constructors should not take any parameters,
since Godot doesn't support that. Parameters are ignore in memnew macro.
Use memnew(MyClass()) instead of memnew(MyClass) since it now needs a
value instead of a class name. This macro calls MyClass::_new() (define
in GDCLASS macro) which ultimately calls Godot to create the object,
ensuring that both the Godot and the extension instances are created.
Non Godot classes (that don't derive godot::Object) are constructed as
usual an can have parameters.
memdelete is also changed for the same reason, as it needs to destroy
the Godot object as well, and that automatically frees the bound
extension instance.
This is consistent with the core Godot source code, and ensures the
license isn't detached from its original code when individual files
are distributed.
This is taken from the Godot repository, so formatting is similar. This
updates the style rules as well.
Also fix style in files to conform with this version.
Some functions return a new instance of such containers,
but instead we made a copy of them, without taking ownership of the
original created by the function.
Now we use a specific constructor taking ownership on the godot_* struct.
The member _godot_string should never be straight out overwritten ever without
first destroying the underlying string object's memory. This change solves the
problem through the introduction of a new private constructor to create String
objects with a pre-existing godot_string handle.
Fixes Vector 2 and 3 bounce and reflect methods to match gdscript
Co-Authored-By: Bruno Campos <brunocu@msn.com>
Move calculation to reflect
fix commit
squash
fix style
Changed error message macros to actually use Godot's error reporting
facilities instead of outputting straight to stderr. This enables
GDNative errors to actually show up inside the editor.
Messages and set of available macros now also better matches that of
the engine itself.
The [] operator of Basis was returning a reference to a temporary, so fixed it.
There was no * operator in Transform equivalent to the xform function, which is
not in line with GDScript behavior.
Also fixed remaining cases where Transform relied on the old behavior of the
[] operator of Basis (i.e. that it returns the row, not the column).
Per https://github.com/godotengine/godot/issues/14553:
Godot stores Basis in row-major layout for more change for efficiently
taking advantage of SIMD instructions, but in scripts Basis looks like and
is accessible in a column-major format.
This change modifies the C++ binding so that from the script's perspective
Basis does look like if it was column-major while retaining a row-major
in-memory representation. This is achieved using a set of helper template
classes which allow accessing individual columns whose components are
non-continues in memory as if it was a Vector3 type. This ensures script
interface compatibility without needing to transpose the Basis every time
it is passed over the script-engine boundary.
Also made most of the Vector2 and Vector3 class interfaces inlined in the
process for increased performance.
While unrelated (but didn't want to file a separate PR for it), this change
adds the necessary flags to have debug symbol information under MSVC.
Fixes#241.
implemented instance binding data usage
This commit changes the way C++ wrapper classes work.
Previously, wrapper classes were merely wrapper *interfaces*.
They used the `this` pointer to store the actual foreign Godot
Object.
With the NativeScript 1.1 extension it is now possible to have
low-overhead language binding data attached to Objects.
The C++ bindings use that feature to implement *proper* wrappers
and enable regular C++ inheritance usage that way.
Some things might still be buggy and untested, but the C++
SimpleDemo works with those changes.
new and free change, custom free will crash engine, be wary
fix exporting of non-object types
fix free() crash with custom resources
added type tags and safe object casting
fix global type registration order
fix cast_to
changed build system to be more self contained
updated .gitignore
use typeid() for type tags now
fix indentation in bindings generator
remove accidentally added files
fix gitignore
Fixed up registering tool and updated godot_headers
Fix crash when calling String::split/split_floats
Was casting to the wrong object type.
Also adds parse_ints function to String with the same logic
Better warning/error macros
Change gitignore so we get our gen folders
New documentation based on nativescript 1.1
Fixed GODOT_SUBCLASS macro
Preventing crash when function returned null ptr
Adds needed include <typeinfo>
Solves this issue #168 due to not having the include of typeinfo
Fix compile error of 'WARN_PRINT' and 'ERR_PRINT'.
cannot pass non-trivial object of type 'godot::String' to variadic function; expected type from format string was 'char *' [-Wnon-pod-varargs]
update vector3::distance_to
Remove godot_api.json as its now in the godot_headers submodule (api.json)
This is needed since that operator returns a local copy, not
an lvalue. Attempting to write to the return value of these operators
wouldn't change the array element. PoolVectors need locking when
writing, so this operator can't return a writable reference.
To update a Pool*Array, use the `set()` method which locks and unlocks
the array. For multiple writes, use the `write()` method which returns
a locked writable view, and unlocks when it goes out of scope.