I'm experimenting with a type of variable/function that is new to me (_G.)
I tried defining a function like this:
_G.function MyFunction() --code here end)
However the script editor gave me a red line, so I changed it to this:
_G.MyFunction = function() --code here end)
For my future reference, is the second way the correct way to define _G. functions?
When defining functions in any table (_G is a table too), you can do it in two main ways; either like this:
_G.MyFunction = function() --code here end)
or like this:
function _G.MyFunction() -- code here end)
While this is not related to your specific case of _G, you can also do this:
function MyTable:MyFunction() -- code here end)
This will define the function in such a way that the first argument will always be refered to as self
, and if you later call the function like MyTable:MyFunction()
then the first argument will be the table itself. This can be used for OOP in Lua.