Godot Version
4.2
Question
When passing a PackedInt64Array to a GDExtension C++ plugin it seems to be passed by value instead of reference. This strikes me as odd since at least the documentation for GDscript mentions that such arrays are always passed as reference.
Simple example
void foo(PackedInt64Array arg){
arg[0] = 1;
}
Calling that from GDscript doesn’t change the value for me.
I also tried Ref<PackedInt64Array>
but that doesn’t seem to work.
Any suggestions?
Try a pointer. C++ passes by value unless specified otherwise.
void foo(PackedInt64Array *arg){
(*arg)[0] = 1;
}
that gives me
godot-cpp/include/godot_cpp/core/binder_common.hpp:150:89: error: incomplete type 'godot::GetTypeInfo<godot::PackedInt64Array*, void>' used in nested name specifier
150 | GDExtensionVariantType argtype = GDExtensionVariantType(GetTypeInfo<T>::VARIANT_TYPE);
I looked up that error. Seems like this happens when the type isn’t supported by GDScript. But I am not sure that is a correct conclusion because I found plenty of examples that return pointers to objects. Just none that use a pointer for passing the data in.
I think you need to add a header for the Packed array types, I would bet this one
#include <godot_cpp/variant/packed_int64_array.hpp>
I’ve been doing some more testing. Seems like you cannot pass a PackedInt64Array
by reference or pointer.
The only thing that works is a const reference
or pointer.
I found that by looking at the engine code where there are places in which packed arrays are passed to GDScript. All of those are const.
Which probably makes sense because modifying the packed array on c++ might change the memory layout in a way that GDScript doesn’t expect.
In the end I solved my issue by not exposing the array to GDScript directly but rather to hide it behind getters and setters that work on a per index basis.