What's the best way to have a singleton-type object instance in every gamemode?

Hey guys,

So imagine a ‘ServerHelper’ object or some-such, whose methods could be called by players when joining, leaving, etc, to update values (i.e. a server-maintained clock). How could I make sure that one of these objects always exists, and is always accessible, in any match / game world? I always want to be able to query the server’s ‘time’ value from anywhere.

Thanks.

If you’ll implement is as a singleton design pattern, then it will always be available every time from everywhere.

How about using something like GameState?

Is this going to be on a client server or is this for a dedicated server?

Best method is to subclass GameEngine and place a reference to your singleton object there. Then, it is accessible from every game mode and won’t be garbage collected. Be careful of objects that the singleton can refer to, because you can cause levels to stay in memory if you store references to them. Also make sure that any objects you store in that singleton use the smart pointers so you can check for them being still valid.

If I subclass GameEngine, how do I ensure that my version of it is the one that’s initialised/used?

Dedicated server, but with the ability for clients to get some of its information via RPCs (imagine clients that need to get the ‘server time’, so everyone can sync up, for example).

You specify the class in your DefaultEngine.ini



[/Script/Engine.Engine]
GameEngine=/Script/YourGamePackage.YourGameEngine


One other thing to consider is that in 4.4 the GameInstance feature will become available. This is a UObject with a lifetime of the Game Seassion, so in a stand alone game, there will be 1 and it will be there for the entire duration. If you use the multiplayer play in editor feature, then there could be multiples, one for each instance of the game.

New Game Instance class is awesome!

Thanks and rest of Epic!

:slight_smile:

Hi, just wanted to double-check. Do you mean to suggest sub-classing UGameEngine, and writing, for example;



[/Script/Engine.Engine]
GameEngine=/Script/MyGameName.CustomEngineClassName


If that were the case, would I be including the preceding ‘U’ added to the name? That is of course only if this is the way you meant.

Thanks, regardless.

I have refined my query now that I’ve tried implementing something in detail.

What I am after is a tick-able object that exists on the server and is replicated to the clients, and which can freely send RPCs between the server and the client.

This is for my time sync code, which currently exists in a custom player controller - it manages synchronizing the server time with all clients. Unfortunately, I need this time even when updating proxy actors, which don’t have a controller accessible on that particular machine. Additionally, I have tried GameState, but it does not Tick, even with


PrimaryActorTick.bAllowTickOnDedicatedServer = true;
	PrimaryActorTick.bCanEverTick = true;

Any ideas? Still trying to grasp how some of the larger classes interact in a networked game.

Thanks.

GameState doesn’t tick, but the GameMode does. Once per frame, update the GameState, which is then replicated to the clients.