Add conversions between Variant and ObjectID

pull/983/head
Ricardo Buring 2023-01-01 21:27:08 +01:00
parent 3da6db4fe4
commit d4e1dfe5dc
2 changed files with 23 additions and 0 deletions

View File

@ -42,6 +42,8 @@
namespace godot { namespace godot {
class ObjectID;
class Variant { class Variant {
uint8_t opaque[GODOT_CPP_VARIANT_SIZE]{ 0 }; uint8_t opaque[GODOT_CPP_VARIANT_SIZE]{ 0 };
@ -187,6 +189,7 @@ public:
Variant(const StringName &v); Variant(const StringName &v);
Variant(const NodePath &v); Variant(const NodePath &v);
Variant(const godot::RID &v); Variant(const godot::RID &v);
Variant(const ObjectID &v);
Variant(const Object *v); Variant(const Object *v);
Variant(const Callable &v); Variant(const Callable &v);
Variant(const Signal &v); Variant(const Signal &v);
@ -230,6 +233,7 @@ public:
operator StringName() const; operator StringName() const;
operator NodePath() const; operator NodePath() const;
operator godot::RID() const; operator godot::RID() const;
operator ObjectID() const;
operator Object *() const; operator Object *() const;
operator Callable() const; operator Callable() const;
operator Signal() 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) { Variant::Variant(const Callable &v) {
from_type_constructor[CALLABLE](_native_ptr(), v._native_ptr()); 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)); 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 { Variant::operator Callable() const {
Callable result; Callable result;
to_type_constructor[CALLABLE](result._native_ptr(), _native_ptr()); to_type_constructor[CALLABLE](result._native_ptr(), _native_ptr());