problem with passing dictionary values

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Nitsuga

I have run into a problem and wanted to know if it is a Godot error or my ignorance.

func _ready():
var path = [Vector2(0, 1), Vector2(1, 1)]
var player_path = path
player_path[0] = Vector2(32, 32)

What prints is this:
[(32, 32), (1, 1)]
[(32, 32), (1, 1)]
And what I expected it to print is this:
[(32, 32), (1, 1)]
[(0, 1), (1, 1)]

Why when I change player_path values ​​do path values ​​change?

Please choose better titles. Somthing like ‘Problem with Dictionary’

whiteshampoo | 2021-01-30 08:09

:bust_in_silhouette: Reply From: whiteshampoo


var player_path = path.duplicate()

Dictionaries and Arrays are a bit tricky. You set the reference with ‘=’, not the values/data.
Very similar to Python.