How to create constructor that accepts arguments in GDExtension?

Godot Version

4.7

Question

I’m creating a Resource in GDExtension. I need to validate all the parameters I’m using in this resource before I set it up, so I need to pass in all the parameters I need in the constructor. Is there a way to do this?

Alternately, I could use a static method, but I’m not sure how to set it up:

    static Ref<CyDataBuffer> create(DataType _type, int _size, int _tuple_size = 1) {
        CyDataBuffer a(_type, _size, _tuple_size);
        return a;
    }

The return a is not valid syntax. Not sure how I can turn my object into a reference.

As far as I know it is not possible to implement a parametrized _init(). You’ll have to work around either by implementing an initializer method that you call after object creation, or a static factory function.

You can initialize Ref<> using the constructor that takes the object pointer:

	Ref<MyClass> object = Ref<MyClass>(memnew(MyClass(parameter)));
	return object;

Or by calling Ref::instanitate() and pass the object constructor parameters to it:

	Ref<MyClass> object;
	object.instantiate(parameter)
	return object;