From 992d85e6f845d798ab26237fe52d1aa6461452c5 Mon Sep 17 00:00:00 2001 From: Bastiaan Olij Date: Sun, 11 Dec 2022 23:20:14 +1100 Subject: [PATCH] Fix virtual GDExtension method Ref conversion --- godot-headers/godot/gdextension_interface.h | 7 +++++++ include/godot_cpp/classes/ref.hpp | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/godot-headers/godot/gdextension_interface.h b/godot-headers/godot/gdextension_interface.h index b59a6b5..66fca76 100644 --- a/godot-headers/godot/gdextension_interface.h +++ b/godot-headers/godot/gdextension_interface.h @@ -154,6 +154,8 @@ typedef const void *GDExtensionMethodBindPtr; typedef int64_t GDExtensionInt; typedef uint8_t GDExtensionBool; typedef uint64_t GDObjectInstanceID; +typedef void *GDExtensionRefPtr; +typedef const void *GDExtensionConstRefPtr; /* VARIANT DATA I/O */ @@ -551,6 +553,11 @@ typedef struct { GDExtensionObjectPtr (*object_get_instance_from_id)(GDObjectInstanceID p_instance_id); GDObjectInstanceID (*object_get_instance_id)(GDExtensionConstObjectPtr p_object); + /* REFERENCE */ + + GDExtensionObjectPtr (*ref_get_object)(GDExtensionConstRefPtr p_ref); + void (*ref_set_object)(GDExtensionRefPtr p_ref, GDExtensionObjectPtr p_object); + /* SCRIPT INSTANCE */ GDExtensionScriptInstancePtr (*script_instance_create)(const GDExtensionScriptInstanceInfo *p_info, GDExtensionScriptInstanceDataPtr p_instance_data); diff --git a/include/godot_cpp/classes/ref.hpp b/include/godot_cpp/classes/ref.hpp index 4e2eb49..5a26548 100644 --- a/include/godot_cpp/classes/ref.hpp +++ b/include/godot_cpp/classes/ref.hpp @@ -240,13 +240,24 @@ public: template struct PtrToArg> { _FORCE_INLINE_ static Ref convert(const void *p_ptr) { - return Ref(reinterpret_cast(godot::internal::gde_interface->object_get_instance_binding(*reinterpret_cast(const_cast(p_ptr)), godot::internal::token, &T::___binding_callbacks))); + GDExtensionRefPtr ref = (GDExtensionRefPtr)p_ptr; + ERR_FAIL_NULL_V(ref, Ref()); + + T *obj = reinterpret_cast(godot::internal::gde_interface->object_get_instance_binding(godot::internal::gde_interface->ref_get_object(ref), godot::internal::token, &T::___binding_callbacks)); + return Ref(obj); } typedef Ref EncodeT; _FORCE_INLINE_ static void encode(Ref p_val, void *p_ptr) { - *reinterpret_cast(p_ptr) = p_val->_owner; + GDExtensionRefPtr ref = (GDExtensionRefPtr)p_ptr; + ERR_FAIL_NULL(ref); + + // This code assumes that p_ptr points to an unset Ref variable on the Godot side + // so we only set it if we have an object to set. + if (p_val.is_valid()) { + godot::internal::gde_interface->ref_set_object(ref, p_val->_owner); + } } };