Tkl1129 said:
Thats mean unless I'm able to share value via different groups or plugins that I will use SetVaribles in mush, if just for normal coding, I use a = 1 is more convinience
Well, sharing values between plugins is relatively rare, but if that's your aim then SetVariable and GetPluginVariable are definitely what you want. For normal coding, the choice between global and local depends on how long you need the value. If you store the result of a calculation in a variable and only Note() it immediately afterwards, you'd want a local. But if multiple aliases/triggers/times/etc. want use a single variable, a global is the simplest solution.
Tkl1129 said:
1. Do the Lua Global Variables eat memory? since I don't know how many I'd add before, is that a good practice that always "nil" to delete it? :D
If they take more room than a local, it's probably a very small amount. Locals can be used very quickly with a single Lua operation, but globals take an extra operation to load the value from the globals table. In practice it's not going to make any difference.
But yes, it's a good idea to set a global to nil when you're done using that value. It'll at least save space in the globals table. Locals are deleted automatically when they go out of scope, so you don't have to worry about them.
Tkl1129 said:
2. In same script
a = 1 -- LUA Global Var
Local a = 2 -- Lua Local Var
print(a) -- return local var
if in this case, I'm sure better won't set in same name is better but if really got same name, can I get the value at the same time?...
Yes, that's how the code will behave. This is called "shadowing", because the global of the same name is basically hidden in the local's shadow. You can't access a shadowed variable until the name shadowing it goes out of scope. However, globals have a workaround in that you can use _G["a"] to access a global by name. _G is the name given to the special table where globals are stored. Of course, if you had a local named _G as well you'd be out of luck! (Also, the 'local' keyword must be all-lowercase. ;D)
More precisely, 'local' creates a new slot for a local variable, and binds the name to that slot. Until the new binding goes out of scope, you can't access the shadowed variable.
local a = 42 -- slot 0
do
local a = 50 -- slot 1
print(a) -- prints 50
end
print(a) -- prints 42
Tkl1129 said:
I just reading the Programming in Lua ...Ch.1 to 4...-_-!..drive me crazy as I'm only know zscript before...my god~~~
Hah, it's a pretty big change, yeah. If anything I've explained here is confusing, don't worry too much about it - I'm dipping into some lower-level details in some places. The vanilla mechanisms are really all you need to worry about.