godotengine / godotengine/godot-cpp

Packed*Array CoW behavior doesnt mirror GDScript's

Open
#1,968 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
2.7k
Forks
809
Avg merge
1d 3h
Merged PRs (30d)
8

Description

### Godot version

4.6

### godot-cpp version

4.6

### System information

win 10

### Issue description

gdscript example that works as expected
```gdscript
class Something:
func addToPacked (a:PackedVector2Array) -> void:
a.append(Vector2.ONE)

func addToTyped (a:Array[Vector2]) -> void:
a.append(Vector2.ONE)

var it := Something.new()
var a := PackedVector2Array()
var b: Array[Vector2] = []

it.addToPacked(a)
it.addToTyped(b)

# a is [(1,1)]
# b is [(1,1)]

```
recreating in C++ (with other stuff omitted for brevity)
```cpp

auto Something::addToPacked (PackedVector2Array array) -> void {
array.append(Vector2(1,1));
}

auto Something::addToTyped (TypedArray array) -> void {
array.append(Vector2(1,1));
}

auto Something::_bind_methods () -> void {
ClassDB::bind_method(D_METHOD("addToPacked", "array"), &Something::addToPacked);
ClassDB::bind_method(D_METHOD("addToTyped", "array"), &Something::addToTyped);
}

```

doing the above gdscript snippet with the gdextension version, `a` will be `[]` and `b` will be `[(1,1)]`
afaict the reason is that the packed array is having its Copy-on-Write triggered when it probably shouldnt be? (since both the above purely gdscript version as well as the docs say that packed arrays are passed by reference)
extremely obnoxious if intended (since the docs say that packed arrays are passed by reference, which they do in the GD version but not the C version)

### Steps to reproduce

^

### Minimal reproduction project

N/a

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.