I just discovered something rather interesting (that, in retrospect, should have occurred to me a long time ago) that I thought others might want to know.
This applies to a module which contains values which you want to share publicly but, while sharing them, you don't want them altered by the client (because it would affect the internal workings of your module). In most programming languages, this is where a contant comes in handy. The problem is that in Lua (unless you're using 5.4+), there are no contants...or are there?
Why This Trick Works
In lua, withinin a script, if there are two variables of the same name, one local and one global, lua will prefer the local.
How This Trick Works
By setting a local variable in your module then placing it in global scope with the same name, you are effetively (with regards to your script's scope) creating a constant (even though the global version of it can be altered). The global version is basically a mirror of the local variable which can be affected by the local but cannot,in turn, affect it. Even though the global value can be altered by the client, the local is unaffected. This means that if you check input values of global functions against the variable name, only the local value is checked.
Example:
myscript.lua
externalscript.lua
This applies to a module which contains values which you want to share publicly but, while sharing them, you don't want them altered by the client (because it would affect the internal workings of your module). In most programming languages, this is where a contant comes in handy. The problem is that in Lua (unless you're using 5.4+), there are no contants...or are there?
Why This Trick Works
In lua, withinin a script, if there are two variables of the same name, one local and one global, lua will prefer the local.
How This Trick Works
By setting a local variable in your module then placing it in global scope with the same name, you are effetively (with regards to your script's scope) creating a constant (even though the global version of it can be altered). The global version is basically a mirror of the local variable which can be affected by the local but cannot,in turn, affect it. Even though the global value can be altered by the client, the local is unaffected. This means that if you check input values of global functions against the variable name, only the local value is checked.
Example:
myscript.lua
PHP Code:
--set it in the global environment
DEFAULT = 0;
--create the local version of the global variable
local DEFAULT =DEFAULT;0;
--check the user's input vs the actual variable value
function checkDefault(nInput)
print("Yout Input: "..tostring(nInput).." | Original variable value: "..tostring(DEFAULT))
end
PHP Code:
--require the module
require(myscript.lua)
--first, check the values without trying to alter the DEFAULT variable
checkDefault(DEFAULT);
--now, try to alter it
DEFAULT = 456;
--check it again
checkDefault(DEFAULT);
Comment