Is storing the Game Instance in a variable, bad?

Hi, is it bad to cast to the game instance, than store the result in a variable, for later use?
Or is it better to cast to it, everytime you need it?
Thanks.

Hey @AllGamesSuck!

As far as I know, and I may be wrong on this but as far as I know…

Anything that is guaranteed to exist does not impact performance when cast to because it doesn’t need to be loaded, it’s always there. The player (if there’s only one kind of player), the Game State, Game Instance, and Game Mode should all be fine to cast to with no performance hit.

In other words, for these things, casting to it once and setting it might be easier and more convenient on your graph. Either guarantee a way to SET it every time it changes, OR cast every time. This is NOT good advice most of the time.

Hope that helps!

3 Likes

If I reference the class often I’ll set a variable. If it’s a one to five over the course of the entire session I’ll simply cast each time.

I look at it in memory terms. A variable is long term memory (unless in a function and local). How much impact will this var have on my memory usage.

2 Likes

Thank you. But in the case of the game instance, does the variable duplicate it in memory, since the game instance is already loaded? Or does the variable just point to it?

That really does not matter, until you want to cast to same class in a loop for 2000 or even more times, vs storing it as variable already casted to.

Also since game instance is already loaded (more like always loaded), there is no extra overhed while casting to it, as system does not need to load class to memory (or whatever it does when you cast to not loaded class).

2 Likes