From d4e1dfe5dce4364baeb1781b0534909e1d895024 Mon Sep 17 00:00:00 2001 From: Ricardo Buring Date: Sun, 1 Jan 2023 21:27:08 +0100 Subject: [PATCH] Add conversions between Variant and ObjectID --- include/godot_cpp/variant/variant.hpp | 4 ++++ src/variant/variant.cpp | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include/godot_cpp/variant/variant.hpp b/include/godot_cpp/variant/variant.hpp index 3d91a0a5..6d265011 100644 --- a/include/godot_cpp/variant/variant.hpp +++ b/include/godot_cpp/variant/variant.hpp @@ -42,6 +42,8 @@ namespace godot { +class ObjectID; + class Variant { uint8_t opaque[GODOT_CPP_VARIANT_SIZE]{ 0 }; @@ -187,6 +189,7 @@ public: Variant(const StringName &v); Variant(const NodePath &v); Variant(const godot::RID &v); + Variant(const ObjectID &v); Variant(const Object *v); Variant(const Callable &v); Variant(const Signal &v); @@ -230,6 +233,7 @@ public: operator StringName() const; operator NodePath() const; operator godot::RID() const; + operator ObjectID() const; operator Object *() const; operator Callable() const; operator Signal() const; diff --git a/src/variant/variant.cpp b/src/variant/variant.cpp index 21b17b30..9d3bae53 100644 --- a/src/variant/variant.cpp +++ b/src/variant/variant.cpp @@ -191,6 +191,10 @@ Variant::Variant(const Object *v) { } } +Variant::Variant(const ObjectID &p_id) : + Variant(p_id.operator uint64_t()) { +} + Variant::Variant(const Callable &v) { from_type_constructor[CALLABLE](_native_ptr(), v._native_ptr()); } @@ -410,6 +414,21 @@ Variant::operator Object *() const { return reinterpret_cast(internal::gde_interface->object_get_instance_binding(obj, internal::token, &Object::___binding_callbacks)); } +Variant::operator ObjectID() const { + if (get_type() == Type::INT) { + return ObjectID(operator uint64_t()); + } else if (get_type() == Type::OBJECT) { + Object *obj = operator Object *(); + if (obj != nullptr) { + return ObjectID(obj->get_instance_id()); + } else { + return ObjectID(); + } + } else { + return ObjectID(); + } +} + Variant::operator Callable() const { Callable result; to_type_constructor[CALLABLE](result._native_ptr(), _native_ptr());