I encountered an interesting issue. I will just show it instead of trying to explain.
local q = {}--short for queue
local c = 0--short for count
local max = 4
for x = 1, max do
for y = 1, max do
c = c + 1
q[c] = function() print(c.."/"..max^2, x, y) end
end
end
for f = 1, #q do
q[f]()
end
The anonymous functions should print their count out of all of them. I expected
1/16 1 1
2/16 1 2
…
instead, this results in
16/16 1 1
16/16 1 2
…
As in, count is always 16. I am guessing this is because it is retrieving the variable after it has been upped all the way to 16. However, as you can see x and y are okay, which confuses me.
Can somebody explain this and is there any other way to get the ‘correct’ result besides passing a parameter into the functions (such as passing f to serve as c)? I use this technique on a larger scale and passing parameters is not going to be nice.