From what I know, nothing is really wrong with using it, but its good practice to use ModuleScripts. People don’t use _G
for a reason, not sure what that is, but it just makes things look ugly.
Roblox has built in globals for convenience purposes… IT ISN’T UGLY
That last part was my opinion, in my opinion _G
looks ugly, probably should’ve clarified that first.
please read:
TLDR: you cant ensure what script is going to run first because Luau is single-threaded so the state of _G cant be guaranteed.
EDIT: this is a wrong reply, @e4r54yt64rtq26
From how it was explained to me, race conditions is when data is being changed from two or more different pieces of code at the same time. How is this related to reading it?
The reason im doing a while wait is because the value may already be there and it’d be pointless to wait in that case
repeat until
will always run once.
Nvm
Also @e4r54yt64rtq26 , I don’t think it’s ugly, I mean require is longer than _G so if your gonna say it’s ugly than modulescripts would also be ugly.
Please, for your own sanity, do not do this.
You’re only setting yourself up for organizational hell down the line. You’re going to have dozens upon dozens of functions all listed under _G with nothing to organize groups of similar functions, which can cause a truckload of problems with navigating your code and debugging.
And you’re doing this all for what? Avoiding spending 5-10 seconds typing out a single line of code? Having to require modulescripts in every script that you use them is entirely beneficial for you, because it lets you more clearly keep track of what modules you’re using and where.
You can read up on the Single Responsibility Principle of software design to get a better idea of why to avoid throwing all your functionality into one place.
I mean you could nest tables in it like:
_G.SomeFunctions = {...}
_G.OtherFunctions = {...}
What point is there for that still? If you’re going through the effort to do that then you may as well create separate modulescripts for that.
Your example already creates modules via the nested tables, only instead of storing those modules in their own scripts, you store them all in _G. Again, to what benefit?
There’s not necessarily a benefit to using _G over modules, but cached modules are stored in the same place so how is this any different than my example?
You could, but simply just stating this provides no reason as to why you should other than pure opinion. I am aware of issues that come with using _G such as different script loading times and scripts modifying it at the same time.
If someone knows what they are doing I see no issue with them wanting using _G. It’s a bit silly to spread the idea that you should never use something without giving genuine concerns with their use case.
Just to go over this in more detail as I do not think a complete answer has been given to your questions.
_G is slower than modulescripts
I would say there is realy no measurable difference. You may find a difference in the scope resolution which is why you may see code like this to make a local variable of a global
local print = print
-- other global function
_G could make you do minor mistakes, such as another script using _G and overwrites the important value
This is difficult to answer as it depends on the extent you use _G. I can say using _G will make your code less user friendly. Meaning you would have to dig into the codebase to find out what _G values had been set, where and why.
So is it still safe/good to do or not?
Safe? yes considered good No. Why?
At run time you have no control over _G other than hoping what you expect to be there is there. _G is mutable (it may be possible to use meta methods to limit access but thats going off topic).
Globals may lead to spaghetti code and ultimatly leads to high coupling.
Should you use _G?
Generally speaking there are no good use cases to _G as module script are a very powerful and diverse tool for coding. An example often overlooked by roblox developers is lazy loading. Do you need to load everything at once often the answer is no.
But thats just my view. As long as you understand how _G works and are aware of the pitfalls then it is really up to you on if you wish to use _G.
so i guess ill have to make a script template plugin to get my laziness out, thanks to everyone who pointed out that _G sucks
i think it is still allowable to store a couple of small references to your fundamental game manager class instances in order to make all other classes quickly access them without the need to store long hierarchies inside and long argument lists for their constructors, such as
_G.MainManager = MainManager.new()
what do you think?