0 votes
var P1 = 5
var P2 = 10
var P3 = 15
var Points
Points = [P1, P2, P3]
for i in Points:
       i = 0

That's my code
It prints 5. Why can't I change a value in a table?

in Engine by (33 points)

1 Answer

0 votes

The for loop i is a copy of each element in the array, the same way this doesn't change P1:

var P1 = 5
var P2 = P1
P2 = 0
print(P1) # 5

If you want to change an array element, you need to loop using a index:

for i in Points.size():
    Points[i] = 0
print(Points[0]) # zero
by (826 points)
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.