Godot Version
4.3
Question
Say I have a Variant
object in C++ that is of type PACKED_VECTOR2_ARRAY
. How do I access this packed array without making a copy of it? It seems that the internal PackedArrayRefBase *packed_array
of Variant
counts references and holds a pointer to the Vector<T>
object, so I expect that I should be able to access and modify the packed array as a reference instead of a full copy.
I see that Variant::_data
is private
, so I can’t access the PackedArrayRefBase*
directly. Also, simply assigning the Variant
object to a PackedVector2Array
creates a copy rather than giving me a reference.
Here is some code that demonstrates the what I’m struggling with:
PackedVector2Array packedArray;
packedArray.push_back(Vector2(1, 1));
TypedArray<PackedVector2Array> parentArray;
parentArray.push_back(packedArray);
Variant variant = parentArray.get(0);
WARN_PRINT(Variant::get_type_name(variant.get_type())); // prints PackedVector2Array
PackedVector2Array packedArrayCopy = variant;
packedArrayCopy.insert(0, Vector2(2, 2));
PackedVector2Array packedArrayCopy2 = variant;
packedArrayCopy2.insert(0, Vector2(3, 3));
WARN_PRINT(packedArray[0]); // Prints (1, 1)
WARN_PRINT(packedArrayCopy[0]); // Prints (2, 2)
WARN_PRINT(packedArrayCopy2[0]); // Prints (3, 3)
I want the last three lines to all print (3, 3)
instead of each being different copies.
As another example, this is trivial in GDScript. The same code, but written in GDScript, will print (3, 3)
for all cases:
var packedArray: PackedVector2Array
packedArray.push_back(Vector2(1, 1))
var parentArray: Array[PackedVector2Array]
parentArray.push_back(packedArray)
var variant: Variant = parentArray[0]
print(type_string(typeof(variant))) # Prints PackedVector2Array
var packedArrayCopy: PackedVector2Array = variant
packedArrayCopy.insert(0, Vector2(2, 2))
var packedArrayCopy2: PackedVector2Array = variant
packedArrayCopy2.insert(0, Vector2(3, 3))
print(packedArray[0]); # Prints (3, 3)
print(packedArrayCopy[0]) # Prints (3, 3)
print(packedArrayCopy2[0]) # Prints (3, 3)