0 votes

I have recently run into a peculiar problem. I have a global variable called board (2d array). And within a function, I have a local variable called modboard, which I set to be equal to board:
var modboard=board
Then I change something about modboard:
modboard[1][0]='something'
But for some reason, that same change is applied to board. I do not understand why that happens or how to fix that. Please help.

Godot version 3.2.1
in Engine by (19 points)

1 Answer

0 votes
Best answer

so you see, array is usually a pointer(or a list of them), and by "copying" an array you just copy to pointer for later use, you want to actually copy the array, and its sub-arrays, to do that you need to use Array.duplicate(true) so you will have a deep copy(meaning every sub-array is also being duplicated instead of a pointer)

by (1,204 points)
selected by

Thanks, that makes sense.

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 Frequently asked questions and 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 [email protected] with your username.