Merge pull request #983 from rburing/variant_to_from_objectid

Add conversions between `Variant` and `ObjectID`
pull/1000/head
Rémi Verschelde 2023-01-18 11:41:57 +01:00 committed by GitHub
commit 0dfe0d0166
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 0 deletions

View File

@ -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;

View File

@ -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<Object *>(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());