godot icon indicating copy to clipboard operation
godot copied to clipboard

GDExtension: Allow directly getting `ObjectID` from `Variant`

Open dsnopek opened this issue 1 year ago • 2 comments

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.

dsnopek avatar Sep 17 '24 14:09 dsnopek

Nice! 🙂

I think the behavior should be specified if:

  • the Variant has another type than OBJECT
  • 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?

Bromeon avatar Sep 17 '24 15:09 Bromeon

  • the Variant has another type than OBJECT

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.

dsnopek avatar Sep 17 '24 16:09 dsnopek

Thanks!

akien-mga avatar Oct 04 '24 15:10 akien-mga