godotengine / godotengine/godot

`PackedVector3Array` function return type from `Array[Vector3i]` assigned to new `PackedVector3Array` variable results in reference

Open
#103,644 0 comments 0 reactions 0 assignees View on GitHub
bug topic:core topic:gdscript
Dominant language
C++
Stars
117k
Forks
26.8k
PR merge metrics
PR metrics pending

Description

### Tested versions

Reproducible on Windows 10, v4.4.stable.official [4c311cbee]

### System information

Godot v4.4.stable - Windows 10 (build 19045) - Multi-window, 1 monitor - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 4070 (NVIDIA; 32.0.15.7260) - Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz (8 threads)

### Issue description

### Findings
Checkout the working _GUT_ testing code:
```gdscript
func _testHelperWithWrongReturnType() -> PackedVector3Array:
var array: Array[Vector3i] = [Vector3i(1, 2, 3), Vector3i(4, 5, 6)]
return array

func test_PackedVector3ArrayFromArrayVector3iWrong():
var packedArray: PackedVector3Array = _testHelperWithWrongReturnType()
var secondArray: PackedVector3Array = packedArray
secondArray.clear()
assert_eq(packedArray, secondArray)

func test_PackedVector3ArrayFromArrayVector3iWrongTwo():
var packedArray: PackedVector3Array = PackedVector3Array(_testHelperWithWrongReturnType())
var secondArray: PackedVector3Array = packedArray
secondArray.clear()
assert_eq(packedArray, secondArray)

func test_PackedVector3ArrayFromArrayVector3iCorrectWithCopyConstructor():
var packedArray: PackedVector3Array = _testHelperWithWrongReturnType()
var secondArray: PackedVector3Array = PackedVector3Array(packedArray)
secondArray.clear()
assert_ne(packedArray, secondArray)

func test_PackedVector3ArrayFromArrayVector3iCorrectWithDuplicate():
var packedArray: PackedVector3Array = _testHelperWithWrongReturnType()
var secondArray: PackedVector3Array = packedArray.duplicate()
secondArray.clear()
assert_ne(packedArray, secondArray)
```
Be aware, that `PackedVector3Array` behaves at least strange when assigned from `Array[Vector3i]`:
* the cases with `assert_eq` are wrong in my eyes
* the cases with `assert_ne` are correct

### Expectation
Assigning variables of the type `PackedVector3Array` should be copied in any case as stated in the documentation.

### Steps to reproduce

see the GDScript example in the issue description

### Minimal reproduction project (MRP)

see the GDScript example in the issue description

Contributor guide

Open the contributing guide

Research direction

Start with the GUT reproduction functions in the issue, especially the assignments from _testHelperWithWrongReturnType() and the copy-constructor comparison. Trace how PackedVector3Array handles these assignments, then add or update a regression test showing that assigned arrays are independent and that the existing copy constructor behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
game-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.