Download

Should I use a Game State/Instance or any other way out

In my game, there are certain world conditions such as Time of the day, Temperature, Wind etc.

Mostly all actors in my scene need to be able to access these values. I am thinking what is the best place to store them.

I think the best would be either to use a game state or game instance… But, I want to use them for some other purpose. Can a game have more than 1 game state or instance?
Is there any other way like if I can place and actor in the scene and have an easy way to access it like using iterator or something similar?

As far as I know you can have more than one Game Instance, but I’m not sure if all of them can be persistent across levels.

I personally store them in my sky BP then I add functionality to my GameMode to fetch the SkyBP.

I wanted something similar, so I created a simple “ADayNightManager” (honestly could have been just a UObject, but whatever…) which is the “one source of truth” for those values, and all other entities either have references to it (i.e. SkyBP queries it for the sun position based on the time of day), or could use iterators to find it.

You could do something similar, it’s pretty trivial to write a simple entity that just contains a few values.

It being persistent across all is not an issue. But we use GetGameInstance() to get pointer to the game instance. If there are 2 different game instances then how to get them each?

Seems like a good idea. Do you use Iterator to get the sky?

I can also do something like the game mode or the game state spawns an actor and keeps the pointer to it. That actor can be accessed either through GameMode or GameState.

Much simpler actually. My Sky BP gets my GameMode at BeginPlay and registers itself with it.

Well the game instance is mostly for Local Player stuff, Gamestate is the friend you are looking for it gets replicated for the server and all clients. As for the Gamemode its a server thingy

GameInstance is a singleton object, you can only have one at a time. It stays persistent across map travels and is the best place for persistent data. GameState and GameMode may change from map to map, depending on the settings.

GameInstance/GameState exists on both client and server and GameMode only runs on server.