Is using a table to work around the 200 variable limit, considered bad practice for future code?
Example:
local var = {}
var.Player = game.Player.LocalPlayer
var.Mouse = var.Player:GetMouse()
Then just keep assigning new variables to the table var as you make progress to your code, would this then be considered bad practice? Or is there a better way for exactly this?
Going over the 200 variable limit is bad practice. This usually means you have a god module/script (something that has too many tasks, rather than one specific one) that you should really be splitting up into multiple smaller modules with specific purposes, or to encapsulate (part of) that data into objects.
You should always strive to write code that is modular, this is much easier for yourself to keep track of and maintain.
It’s hard to give specific advice without knowing your specific example. If you share some code on what kind of variables you currently have I could give more specific advice.
It’s not what I currently have or try to do right now, it’s more of some old problems I’d like to not face again in the future. In summer 2018, I wrote a framework which hit the limit of 200 variables which was all in one script. In that moment I understood how important modules and multiple scripts are, which got me to try and save the framework by splitting it into modules, but it got too difficult and I decided to restart the project and remake the framework (which I probably will in late February or early March 2019).
I’m simply wondering about how I can avoid this happening to the new framework. I will try and use multiple modules as I’ve seen top developers do. The framework I made in summer 2018 had unnecessary variables that I could’ve stored in a configuration module or similar, where one variable stored one things like one motor6D, or one rig bodypart, or a configuration value etc.
If you’re determined to have it in one script, note any instances where variables are created for a single block of code and then never used again. You can stick those blocks of code into a function, or a do block.
local variableOne = 'something required in multiple blocks'
do
local variableTwo = variableOne:sub( 1, 23 ) .. 'a single block'
print( variableTwo )
local variableThree = variableTwo:len()
print( variableThree )
end
print( variableTwo ) -- doesn't exist here
Question: If most of my variables are meant to be used in various functions/loops,
would this not count towards the limit of 200 variables if I defined them as someVariable = "someString" instead of defining them as local someVariable = "someString"?
Each individual function has its own 200 local variable limit along with a 60 out of scope (upvalue) local variable limit. There is no global variable limit because the global environment is just a masked table, while local variables are held in a special, easy to get Lua stack.
Yes, but this may make the flow of your code hard to follow in terms of where variables are used. You should also only do this for variables that aren’t used from different threads – so no using it in connections or callbacks unless it must be global. There’s also a small performance overhead but only matters where your code must be absolutely fast.
If you’re hitting 200 variables in a single ModuleScript, there is most definitely a problem in the way you are designing your codebase. You should not have a case in which this happens. Split your data table into relevant sub-data containers, don’t keep it in a super-module.
I hate global variables and would never encourage using them. I’d rather have my variables local to the current scope.
Doesn’t majorly change my response. Branch your variables and constants out to ModuleScripts. 200 or more in one script is fairly overkill. I don’t even like the thought of using 2/5ths that amount.