- Home /
Is a global static boolean a good implementation for pausing my game?
Let's say I had a "Game Manager" script as follows:
public class GM : MonoBehaviour
{
public static bool isPaused;
...blahblahblah
}
Then, for every instance object in my scene, I had this at the top of it's update
void Update()
{
if (GM.isPaused)
return;
...All the rest of the code....
}
..In addition to that, of course I also set the timescale to 0f upon pausing.
My question is; Is there a substantial reason that using a public, static 'GM.isPaused' variable would be inferior to using a non-static, instance boolean variable? I don't want anyone's personal preference. I want to know, is there some sort of problem/consideration with doing this, however minor (such as lower performance), and how significant is the problem/consideration? You'd be helping me a lot.
Thanks!
Answer by ElementalVenom · Oct 13, 2018 at 03:26 PM
There is very, VERYYY minor time differences between the two if there are any at all(im talking like nano seconds). The only bad thing about it that i can see is that worst case senereo someone can inject IL(the language that C# is compiled into) into your game to be able to pause the game from the injected code. This is pretty useless in practice though. Ive used public static for game pause variables for the past 4 years and have never ran into an issue with it.
If your game is going to have mods that load assemblies similar to a plugin system it may be an issue. But if you dont understand what this means then your definitely not doing it.
TL;DR: Youre fine using public static for pause variables.
Answer by logan4179 · Oct 13, 2018 at 07:00 PM
Thank you for your response. You say you've been using this for the past 4 years, so I'm curious; is this your preferred way of doing it?
It depends on what im doing. But to be honest: yes, i just use public static bool 80% of the time that I do game pauses lol.