Hi guys, this is my first tutorial on Core so please go easy on me!
Today, I'm going to briefly go over the implications of _G. Lua's _G is an incredibly powerful tool and is often overlooked by newer developers due to it not really being complex enough to be talked about frequently. _G is a global table, with a reference built in & accessible to any script executed within a specific VM. If you're not sure what that means, let me explain.
Using _G
To start with, you should know that _G is an empty table to start with. When the first lua script runs, _G will have no information. That means that any script that needs to read global variable should wait until it is certain it is there instead of immediately trying to access it.
To start, we are going to create an "_G Manager". This script will set up our _G table and expose some variables and functions for our other scripts to use.
_G Manager:
_G.gameState = "Menu"
_G.SomeRandomVariableName = 6
_G.functionOne = function()
-- this is one way to define a function inside of _G, with the name of functionOne
end
function _G.functionTwo()
-- this is a different way to define a function inside of _G. Both ways work fine.
end
_G.isLoaded = true -- we put this here so that our other scripts can listen for this to be set to know that all of the functions in the _G table have been properly loaded.
-- This code *would* do nothing, but because we assigned the variables in _G, we will see that they, in fact, are accessible from other scripts now.
Now it's time to access our data. In order to do this, we're going to make a new script. At the top of that script, we are going to add a wait. This is only necessary if you are accessing the data immediately. If it's being accessed when a trigger is pressed or something, don't worry about adding the wait because the chances are the table has already been loaded.
while _G.isLoaded ~= true do -- If the _G manager was executed first, we shouldn't wait at all, so by using a while loop we can skip the whole thing if the initial case is false. Side note: ~= is the "not equal" operator in Lua.
Task.Wait()
end
print(_G.gameState) -- prints("Menu")
_G.gameState = "Game" -- sets the global variable equal to "Game". This will also update it for any other script.
_G.functionOne() -- this will call functionOne from the _G table.
Use Cases
While this isn't really a useful tool for building assets for community content (assigning the same name in _G as something the developer is setting will cause an overwrite, so you should use something local for that like modules or events) it is incredibly useful as a game developer who wants to keep everything in one place.
Some examples of what I've kept in _G on my projects are:
- Text manipulation libraries (put commas in the thousands, print tables, scan for a text match, etc)
- Caching network data (player progression stuff for UI, etc to save bandwidth)
- Custom Notification functions (the long code for tweening UI and flashing colors can clutter up other scripts and is more difficult to change in the long run)
- Code that is expected to be reusable
So there you have it guys, that's _G. It's a fairly simple concept but when used properly it can really change the way you structure your games. I hope this has helped! A quick note before you go: There is another global table called "shared". It works identically to _G but I'm not sure it exists in Core, it might exist in other versions / forks of Lua, but I thought I'd include it here just as another tool to add to the toolbox in case any of you are working on non-core Lua projects as well!
If you liked the post, please leave an upvote / like! I appreciate it!