0 votes

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)
print(player_path)
print(path)

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?

in Engine by (106 points)
edited by

Please choose better titles. Somthing like 'Problem with Dictionary'

1 Answer

+1 vote
Best answer

Solution:

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.

by (1,448 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.