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
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.
local This = true
local That = false
local Two = 2
local Foo = "Bar"
local This = "That"
local Bar = "Foo"
local Lenny = "( ͡° ͜ʖ ͡°)"
local Happy = ":)"
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.
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.