From 1e9a774ac24ecbbec9b0aa4b903f3f1525f4d525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Sat, 24 Oct 2020 13:50:58 +0200 Subject: [PATCH] Make Variant aware that an Object may be a Reference --- core/variant.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/core/variant.cpp b/core/variant.cpp index 1a2b4d934b7d..3a7b08fd0ea7 100644 --- a/core/variant.cpp +++ b/core/variant.cpp @@ -2338,12 +2338,20 @@ Variant::Variant(const RID &p_rid) { Variant::Variant(const Object *p_object) { type = OBJECT; + Object *obj = const_cast(p_object); memnew_placement(_data._mem, ObjData); + Reference *ref = Object::cast_to(obj); + if (unlikely(ref)) { + *reinterpret_cast *>(_get_obj().ref.get_data()) = Ref(ref); #ifdef DEBUG_ENABLED - _get_obj().rc = p_object ? const_cast(p_object)->_use_rc() : NULL; -#else - _get_obj().obj = const_cast(p_object); + _get_obj().rc = NULL; + } else { + _get_obj().rc = likely(obj) ? obj->_use_rc() : NULL; +#endif + } +#if !defined(DEBUG_ENABLED) + _get_obj().obj = obj; #endif }