godotengine / godotengine/godot

Copy of a PackedVector2Array from a resource edits the original and doesn't reset on scene reload

Open
#87,351 3 comments 0 reactions 0 assignees View on GitHub
discussion needs testing topic:gdscript
Dominant language
C++
Stars
117k
Forks
26.8k
PR merge metrics
PR metrics pending

Description

### Tested versions

reproducible in v4.2.1.stable.official [b09f793f5]

### System information

Godot v4.2.1.stable - Windows 10.0.22631 - Vulkan (Mobile) - dedicated NVIDIA GeForce RTX 4060 Ti (NVIDIA; 31.0.15.4665) - 11th Gen Intel(R) Core(TM) i5-11400F @ 2.60GHz (12 Threads)

### Issue description

This is a very weird bug, that might be hard to explain.
In a game I'm working on, I have a simple position-recording system, to play back those positions later. I save the positions in a resource, then in-game, copy the PackedVector2Array from the resource onto an identical copy.
I tried doing it in several ways:
```py
var vector_array: PackedVector2Array = preload("res://Vec2PositionArray.tres").VecArray
const vector_array: PackedVector2Array = preload("res://Vec2PositionArray.tres").VecArray
@onready var vector_array: PackedVector2Array = preload("res://Vec2PositionArray.tres").VecArray
# as well as using an @export
```

Then the system iterates through the array like this on loop:
```py
icon.position = vector_array[0]
vector_array.remove_at(0)
```
The issue arises when i restart the scene, vector_array does not reset to the default value, looking at the remote tab it seems that the resource itself has been modified, even though I've only been removing the members in a copy of it. When the game is reset, the array is reset properly.
This is a very weird and hard to explain issue for me, but it is causing major problems. I've left an example project in case it's easier to see what's wrong.

### Steps to reproduce

In the example project:
- Press the next pos button a few times
- Press the scene reload button
- Press the next pos button again, the positions will continue from before the reload, rather than from the beginning, indicating that the array has not been properly reset.

### Minimal reproduction project (MRP)

[PackedVector2Array bug.zip](https://github.com/godotengine/godot/files/13980487/PackedVector2Array.bug.zip)

Contributor guide

Open the contributing guide

Research direction

Start with the attached minimal reproduction project and reproduce the scene reload sequence using the PackedVector2Array loaded from the resource. Trace how the array and resource behave across scene reloads, then verify that removing entries from the working array no longer changes the resource and that the positions restart from the beginning.

Written by the indexing model from the issue text.

Assessment

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.