GDExtension: Allow directly getting `ObjectID` from `Variant`
Presently, we don't have a way to directly get the ObjectID from a Variant that holds an Object.
In godot-cpp (and probably other bindings), we implement getting the ObjectID by first getting the Object *, however, this won't work for one common use of getting the ObjectID: checking if the Object is still valid.
This adds a new variant_get_object_instance_id() function to gdextension_interface.h.
See PR https://github.com/godotengine/godot-cpp/pull/1591 for integrating this with godot-cpp.
Nice! 🙂
I think the behavior should be specified if:
- the
Varianthas another type thanOBJECT - the pointed-to object is dead
Can the two cases be differentiated by just this API? Or are follow-up calls (is_instance_id_valid, Variant::get_type) needed?
- the
Varianthas another type thanOBJECT
It's currently coded to return 0 if the type isn't object. This is different than the vanilla Variant::operator ObjectID() which will also return the integer value if the variant is an integer. However, I feel like what's in the current PR aligns better with what we actually need this function for. If bindings want to replicate the Variant::operator ObjectID() behavior, they can do it themselves.
I'll add a note about this to the docs.
the pointed-to object is dead
It returns the instance ID of the object, even if it's no longer valid. This matches Godot's internal behavior, and if we don't do that, we don't have a way replicate it.
This is also worth putting in the docs.
Or are follow-up calls (
is_instance_id_valid,Variant::get_type) needed?
I personally think follow-up calls are the way to go.
Thanks!