I have been trying to figure out if it is possible to create something akin to _init function inside of C++.
You can specify any kind of constructor for your class in C++, but I have not seen any way to set some constructor to be the default way to create your object.
Let’s say I have class defined in C++ called Component and in it I would define
void _init(StringName name, int number)
After binding such method I should be able to pass name and number in gdscript like so:
Component.new(name, number)
to construct this object.
But this does not happen. As a matter of fact if you try to bind a method with the name “_init”, it will make it not show up as this objects method at all…
Is it even possible to somehow specify custom construct from the C++ for GDScript?
GDExtension allows classes to be registered without a default constructor. They can either have custom constructors (static methods) that are invokable from GDScript, or non-exposed constructors only available in the binding language. This can be done by not providing create and recreate function pointers during class registration, and optionally marking a class as abstract.
Thank you for the reply
I will try setting create_instance_func and recreate_instance_func, but it’s a little bit complicated and might not be very intended
Because I will have to define my custom _register_class function in my library
And then provide function pointers to create_instnace_function and recreate_instance_function for class info structure
Even after this done, the create_instance_function is expecting void* data as input in any case, so we would need to find a way to tell the engine which arguments the Class.new() call is expecting, and currently I have no idea where that could be specified.