Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to cast custom objects for Variant #42060

Closed
wants to merge 1 commit into from
Closed

Allow to cast custom objects for Variant #42060

wants to merge 1 commit into from

Conversation

Xrayez
Copy link
Contributor

@Xrayez Xrayez commented Sep 14, 2020

Allows to cast custom object types to be compatible with Variant for binding methods.

This PR is created for discussion purposes, feel free to close if you think that the implementation is too complex or wrong, or when a better solution comes in, but if useful, I could continue working on this. 🙂

Usage

Basically the same as for casting enums:

VARIANT_OBJECT_CAST(ListNode);

This is useful for implementing custom object types in C++ modules without having to manually cast Objects in code, and allows for such a custom class to appear in the documentation methods API! For concrete use case, see goostengine/goost#12, where this is implemented locally for a single class.

Note that casting builtin Node works because there's already a support for that in Variant natively.

There are other stuff which can/should be implemented to make this work with C# I think (PtrToArg?), and additionally allow to cast const * objects, but this is just to showcase the implementation to @reduz currently.

#godotengine-devel IRC discussion:

Xrayez> reduz: do you think it makes sense to add VARIANT_OBJECT_CAST macro to allow binding methods which accept custom Object datatypes? (useful for implementing other structures via C++ modules such as linked list, where there could be ListNode implemented as an Object, for instance). Similar to VARIANT_ENUM_CAST. Currently, only Node is able to be exposed like that, because it's natively supported by Variant.
reduz> Xrayez: with C++03 i never found a way to properly do this, I wonder if its possible with C++11 that of course does not require crating an extra macro for each class you bind
Xrayez> reduz: well I'm asking because it seems like I've managed to implement such a macro already, it would work similarly to VARIANT_ENUM_CAST. :)
reduz Xrayez: it may be possible to do with a template in newer C++ but I am not sure, did not investigate
Xrayez> I can submit a PR for this for testing purposes, if you don't mind.
reduz> sure but i prefer not to have to add a macro on every node you define if we can do automagically it would be great
Xrayez> reduz: what do you mean by defining a macro for each class? You'd just VARIANT_OBJECT_CAST(ListNode), for instance. It would be good enough in my eyes. xD
reduz> well, I want to make it work without actually doing that, I think it should be possible
Xrayez> I've dedicated 3 hours to figure how to do this, I wish it could be there in Godot since I already know about VARIANT_ENUM_CAST, for instance.
Ah ok then.

@TechnoPorg
Copy link
Contributor

This can be closed now that #57205 is merged.

@Calinou Calinou closed this Jan 28, 2022
@Calinou
Copy link
Member

Calinou commented Jan 28, 2022

Superseded by #57205. Thanks for the contribution nonetheless!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants