Why are these variables not increasing [SOLVED]
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 18
- Joined: Tue Apr 24, 2018 7:37 pm
Why are these variables not increasing [SOLVED]
From this, I would expect x and z to increase, but for some reason they do not. Why is this?
- Attachments
-
- main.lua
- (176 Bytes) Downloaded 112 times
-
- luaproj.love
- (246 Bytes) Downloaded 105 times
Last edited by ITISITHEBKID on Sat Nov 03, 2018 9:21 pm, edited 1 time in total.
Re: Why are these variables not increasing
Could have just put the code in the post like
What's happening there is, instead of putting the x and z variables into the y table, You're putting 42 and 42 into it.
Do this instead
and it should work as expected.
Code: Select all
io.stdout:setvbuf("no")
function love.load()
x = 42
z = 42
y = {x,z}
end
function love.update()
for i,v in ipairs(y) do
y[i] = y[i] + 2
end
print(x,z)
end
Do this instead
Code: Select all
y = {x = 42, z = 42}
Re: Why are these variables not increasing
x,z are numbers and you cannot have multiple references to a number variable.
So when you write x=42 z=42 y = {x,z} it's equivalent to writing y = {42,42}
Your code changes y[1] and y[2] but does not change x,z.
Note that the assignment operator "=" works differently with numbers as opposed to tables:
a = 42 b = a means that b = 42
a = {} b = a means that both a and b reference the same table
It's a common mistake, you just get used to it.
So when you write x=42 z=42 y = {x,z} it's equivalent to writing y = {42,42}
Your code changes y[1] and y[2] but does not change x,z.
Note that the assignment operator "=" works differently with numbers as opposed to tables:
a = 42 b = a means that b = 42
a = {} b = a means that both a and b reference the same table
It's a common mistake, you just get used to it.
-
- Prole
- Posts: 18
- Joined: Tue Apr 24, 2018 7:37 pm
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 6 guests