0 votes

I define a constant and a variable. I assign the constant to the variable and manipulate the variable but I don't know why the constant also changes.

const DEF = [[0,2],
var deflist
deflist[0][1] *= 1.5

Then the output shows that DEF[0][1] also changes to 3.
It is weird that a constant is not constant even I have done nothing on it.
Does anyone know what happens?

in Engine by (12 points)

1 Answer

0 votes

You need to duplicate with deep=true:

var deflist = DEF.duplicate(true)
deflist[0][2] *= 1.5

Click here to run the code above.

Seems like const doesn't keep arrays from being changed.

by (4,097 points)

Oh, I see. Let me try. Thx XD

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.