18 posts
Posted 30 December 2012 - 10:28 AM
This error baffles me.
Spoiler
args = {...}
width = tonumber(args[2])
cells = {}
for i = 1,width do
cells[i] = i
end
function range(index)
ret = -1
i = index
v = cells[i]
while i <= width and v == cells[i] do
i = i + 1
ret = ret + 1
end
return ret
end
function selectVertical()
ret = {}
dug = {}
for i = 1,width do ret[i] = 0 end
for i = 1,width do dug[i] = 0 end
for i = width,1,-1 do
ret[i] = 0
if dug[cells[i]] == 0 then
if (i == 1) or (cells[i-1] ~= cells[i]) then
j = math.random(i, i + range(i))
ret[j] = 1
dug[cells[i]] = 1
end
end
end
return ret
end
The error (Index expected, got number) is in this line:
ret[j] = 1
I didn't include all the code, only the parts relevant to this problem.
This is part of an implementation of Eller's maze generation algorithm.
1688 posts
Location
'MURICA
Posted 30 December 2012 - 11:14 AM
My best guess as to what the problem is, is that your range() function overwrites the ret variable you set in your selectVertical() function when you call it. At the line you've pointed out, it's trying to do something like 1[2] = 3, which doesn't really work. Try localizing your variables, so that they're unique to that one function, and it never gets overwritten.
Localization example:
local ret = -1
Though I'm not sure why Lua doesn't have variables automatically localized. Hm.
78 posts
Location
Poland
Posted 30 December 2012 - 12:36 PM
As Kingdaro said: Your problem is that you don't have "ret" declared as "local"
@Kingdaro
lua doesn't localize variables automatically, because it has "local" word not "global" ;)/> and also local variables are more easy to see when they have keyword before them.
1688 posts
Location
'MURICA
Posted 30 December 2012 - 12:48 PM
lua doesn't localize variables automatically, because it has "local" word not "global" ;)/>
That's like saying "lua has a local keyword because it has a local keyword" though.
and also local variables are more easy to see when they have keyword before them.
There's one side of the spectrum, but instead, why not just have each variable local by default and have a "global" or "export" keyword instead? More times than not you'll probably be typing "local" for all of your variables.
To be fair, for CC's API system, you'd need to type "global" for all of your API functions, so.
18 posts
Posted 30 December 2012 - 01:48 PM
Wow. So I can declare a variable within the scope of a function and Lua will automatically upgrade it to global. Does this mean that every variable without "local" before its declaration is global by default?
1688 posts
Location
'MURICA
Posted 30 December 2012 - 01:49 PM
Wow. So I can declare a variable within the scope of a function and Lua will automatically upgrade it to global. Does this mean that every variable without "local" before its declaration is global by default?
Yep.
2005 posts
Posted 30 December 2012 - 08:10 PM
The reason for this is that it makes programming a good bit more robust. If you don't specify the scope, then hey the scope is global, because it isn't specified anywhere specific. If you do specify it, then hey the place the scope starts is marked with a keyword.
Most languages use the global keyword so that you don't accidentally make things global. Which makes sense. But it does mean that tracking down the beginning of scope for a local (without a helpful IDE) can be more difficult, particularly if the same identifier is used for more than one local value.