Announcement

Collapse
No announcement yet.

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

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    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.

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

    Comment


      #3
      How about using something like GameState?

      Comment


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

        Comment


          #5
          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.

          Comment


            #6
            Originally posted by joeGraf View Post
            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?

            Originally posted by zazabar View Post
            Is this going to be on a client server or is this for a dedicated server?
            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).

            Comment


              #7
              You specify the class in your DefaultEngine.ini

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

              Comment


                #8
                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.

                Comment


                  #9
                  Originally posted by Marc Audy View Post
                  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 Marc Audy and rest of Epic!



                  Rama
                  100+ UE4 C++ Tutorials on the UE4 Code Wiki, including UE4 Multi-Threading!

                  UE4 Marketplace: Melee Weapon Plugin & Compressed Binary Save System Plugin | Rama's C++ AI Jumping Videos | Vertex Snap Editor Plugin

                  Visit www.ue4code.com to see lots of videos about my C++ Creations! ♥ Rama

                  Comment


                    #10
                    Originally posted by joeGraf View Post
                    You specify the class in your DefaultEngine.ini

                    Code:
                    [/Script/Engine.Engine]
                    GameEngine=/Script/YourGamePackage.YourGameEngine
                    Hi Joe, just wanted to double-check. Do you mean to suggest sub-classing UGameEngine, and writing, for example;

                    Code:
                    [/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.

                    Comment


                      #11
                      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
                      Code:
                      PrimaryActorTick.bAllowTickOnDedicatedServer = true;
                      	PrimaryActorTick.bCanEverTick = true;
                      Any ideas? Still trying to grasp how some of the larger classes interact in a networked game.

                      Thanks.

                      Comment


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

                        Comment

                        Working...
                        X