Skip to content

Improve logic around using Ref<T> with GDExtension virtual functions - #69902

Merged
akien-mga merged 1 commit into
godotengine:masterfrom
BastiaanOlij:improve_gdextension_virtual_ref
Dec 14, 2022
Merged

akien-mga merged 1 commit into
godotengine:masterfrom
BastiaanOlij:improve_gdextension_virtual_ref

Conversation

@BastiaanOlij

@BastiaanOlij BastiaanOlij commented Dec 11, 2022 •

Copy link
Copy Markdown
Contributor

As discussed during the GDExtension meeting, we have a problem when calling GDExtension virtual functions when Ref<T> is used as the data type. Unlike "normal" functions, which use Virtual as an intermediate which ensures refcounting is done, with virtual functions we send pointers to Ref<T> objects. Unfortunately GDExtension can't make heads nor tails of this and right now we assign values in a rather dangerous way as well as introducing the risk of returned objects being destroyed before Godot is able to reference them.

This PR introduces two support functions that allow us to get or set an object on a Godot side Ref<T> pointer.

This PR has a companion PR on the godot-cpp side which uses these functions: godotengine/godot-cpp#958

@BastiaanOlij BastiaanOlij added this to the 4.0 milestone Dec 11, 2022
@BastiaanOlij BastiaanOlij self-assigned this Dec 11, 2022
@BastiaanOlij
BastiaanOlij force-pushed the improve_gdextension_virtual_ref branch 2 times, most recently from 338bffd to 787ca6e Compare December 11, 2022 11:04
@BastiaanOlij

Copy link
Copy Markdown
Contributor Author

Note that we're using the new naming convention using GDExtension instead of GDNative (pending PR)

@DmitriySalnikov

Copy link
Copy Markdown
Contributor

For me, this pair of PR's solved random crash when repeatedly transferring a native class from std::map to GDScript through a couple of methods. (these methods returned null multiple times and godot crashes)
But it's strange to me that there is another pair of methods nearby that creates a native object, it is saved in std::map and returns to GDScript without problems. 🤷‍♂️

DmitriySalnikov added a commit to DmitriySalnikov/godot_debug_draw_3d that referenced this pull request Dec 12, 2022
restored groupedText renderer
added Ref<T> patch.
Need godotengine/godot#69902 to work
@BastiaanOlij
BastiaanOlij force-pushed the improve_gdextension_virtual_ref branch 2 times, most recently from 77c1e8d to a1b5dbb Compare December 12, 2022 23:34
@BastiaanOlij
BastiaanOlij force-pushed the improve_gdextension_virtual_ref branch from a1b5dbb to 7502e1d Compare December 12, 2022 23:55
@akien-mga
akien-mga merged commit eba33c6 into godotengine:master Dec 14, 2022
@akien-mga

Copy link
Copy Markdown
Member

Thanks!

@BastiaanOlij
BastiaanOlij deleted the improve_gdextension_virtual_ref branch December 24, 2022 02:13
migueldeicaza added a commit to migueldeicaza/SwiftGodot that referenced this pull request Jan 9, 2026
migueldeicaza added a commit to migueldeicaza/SwiftGodot that referenced this pull request Jan 9, 2026
… Ref<> virtual returns (#759)

* Changed the generated virtual method return handling so that when a virtual
returns a RefCounted (for Resource), we write a proper Ref<> via ref_set_object
instead of writing a raw Object* handle.

I was observing that overwritten methods in GDScript that returned Resource-derived types were returning nil.

* Add method, this is to support the special ABI in godotengine/godot#69902
csueiras pushed a commit to cafecito-games/SwiftGodot that referenced this pull request May 9, 2026
… Ref<> virtual returns (#759)

* Changed the generated virtual method return handling so that when a virtual
returns a RefCounted (for Resource), we write a proper Ref<> via ref_set_object
instead of writing a raw Object* handle.

I was observing that overwritten methods in GDScript that returned Resource-derived types were returning nil.

* Add method, this is to support the special ABI in godotengine/godot#69902
BendyLand pushed a commit to BendyLand/voltaire that referenced this pull request Aug 2, 2026
…nsion_virtual_ref

Improve logic around using Ref<T> with GDExtension virtual functions
BendyLand pushed a commit to BendyLand/voltaire that referenced this pull request Aug 2, 2026
…nsion_virtual_ref

Improve logic around using Ref<T> with GDExtension virtual functions
wangshucheng pushed a commit to wangshucheng/godot that referenced this pull request Aug 27, 2026
…nsion_virtual_ref

Improve logic around using Ref<T> with GDExtension virtual functions
Shane-Gadsby pushed a commit to Shane-Gadsby/godotwebgpu that referenced this pull request Sep 21, 2026
…nsion_virtual_ref

Improve logic around using Ref<T> with GDExtension virtual functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants