I know I’m going to get a bunch of crap for this, but my script simply takes more than 200 local variables. I see no reason to split up my code into modules(I understand it all, and I’m not interested in co-developing), and it seriously is that much scripting. I can’t be too much more efficient without getting unnecessarily complex. What is the real purpose of limiting the local variables to 200? Does roblox lua break past that or something?
edit 2019: this is a very old post, I dont agree with it anymore lol. One should be able to use modules and not get anywhere near 200 local variables
You can get by this by making tables of variables whenever possible.
local vartab = {}
vartab[1] = 'variable1'
vartab[2] = 123
vartab[3] = {'mulah',23,'gulah')
etc. etc.
--and then call them like
print(vartab[1],vartab[2],vartab[3][1],vartab[3][2],vartab[3][3]}
I know I can just do _={} print(_.variableName) but why must I do that? I don’t get the point of the limit unless it holds roblox back or breaks something.
The performance may just as fast using non-local (global?) variables. Think of it like having favorites in your contacts. Once you add 200 favorites there’s no point anymore, it just takes to long to find everyone.
That cap is in stock Lua. Either use a bypass as johnnygadget suggested, or try to define as many variables outside the function as possible. Things like non-scope dependent constants, counters, etc.
(also, in response to the title, 200 upvalue limit is only for functions, not to any specific scope)
If that is actually the case, then fair enough. It’s not like I have variables that could totally work inside functions, as global locals. I really do have that much stuff in my main client script.
local This = true
do
local That = false
local Two = 2
end
do
local Foo = "Bar"
local This = "That"
local Bar = "Foo"
end
do
local Lenny = "( ͡° ͜ʖ ͡°)"
local Happy = ":)"
end
Using do and end creates a new scope for variables and parts of code, that other parts of the code can’t see, removing the local variable issue.
Also, the reason I would not recommend @johnnygadget’s method is that referencing tables can be costly, mostly in large for or while loops. However, this should only concern you if you are someone like AxisAngle and wants to have the fastest code possible.
ROBLOX probably could change it, but I doubt there would be any point to it (since it’s not often that people use more than 200 locals and 60 upvalues) so they aren’t likely to change it soon.
Breaking things up is great for readability and organization, regardless of if you plan on sharing your code or not. It’s nice to come back to your code in a year’s time and still make quick sense of it.
Obviously this is your call, but I’m really interested in knowing what you’re making!
I guess I don’t need this many local variables, but it saves time to type out a variable than an object path that is 3 or 4 objects long. I also make my functions local. I have a ton of tiny features that make this game extra better than the original copy, and tiny bits of coding add up.
It’s more that each VM instruction is 32 bits [llimits.h], they use 6 for the opcode and need three arguments, so that’s 26 bits between 3 arguments [lopcodes.h] and arguments need to be able to refer to a local by number.
So it’d take a lot of effort for ROBLOX to change.
I’m not doing anything wrong except(perhaps in your opinion) not using modulescripts. I don’t use them because I haven’t needed to. Everything is clean and non-repetitive. There is such a thing as a game requiring lots of code - this is one.
I just don’t see the point of modulescripts client-side(for this game). I can find everything pretty good. It would be nice server-side, but nearly all functions use variables in the main scope, and I don’t know how to work with that without getting hacky.
Just do what assembly programmers do. They have a limited number of registers (which are like global variables) and if they need more than 32 (or 64, or however many the architecture supports) they push unused values into memory (which in lua would probably be represented by a big table in which things get added to and removed from the back).Then, when you need those values again, you just pop them off the “stack” of memory and into your variables.
Yeah I may just have to have a local table to hold all my variables. I can and have removed a few local variables that weren’t truly necessary, but I am going to inevitably reach the limit again.