Search code examples
unity-game-enginegame-developmentmirrorunity-networking

Unity Mirror - Command and Dedicated Servers. I dont know how to proceed


I'm in a very early state of the game, it is just a Room(1-8 players) card game. The idea is this: Have one dedicated server (One single scene) which contains the next tree:

- NetworkManager
- ServerManager 
  - Games 
    - Game 1 (Prefab 1.1)
       - RoomManager
       - InGameManager
    - Game 2 (Prefab 1.2)
      - etc.

I don't want to use multiple scenes in the server because I just want to store all the info of multiples games in the different managers, and instantiate the prefabPlayer in RoomManager and move them to InGameManager in the startGame().

The main problem im having is with the comunication between client and server cause its a little non-intuitive. The [Command] function in all the examples and codes i saw its done in the client side Player GameObject to do it on the Server side Player GameObject. But i dont have any idea about how to just create a game.

I have in my ServerManager GO a method called "CreateGame()", but is not attached to any playerPrefab or anything known by the client directly.

Being in my client side I cant do a [Command] to CreateGame cause I cant find that function (Ofc cause isnt in my playerprefabGO) but how do I call those """External Methods""" to create a game or join a game or other thing out of the proper player (ConnectionServer.CreateGame or something like that -.-'')

Tried using commands, RPC, etc. but I cant understand the logic out of the players. ChatGPT abuser here and even with that I cant realize how to do it.


Solution

  • Ok, finally solved it, not in the way I expected but finally ChatGPT give me an usefull idea: The idea is simple, just created a struct CustomMessage : NetworkMessage, then did the next method :

    On Client

        public void CallClientMethod(int idMethod, NetworkConnection conn)
        {
        CustomMessageSC message = new CustomMessageSC(idMethod);
    
        conn.Send(message);
        }
    

    This is where i build the message with all the parameters i need (here i just have the idMethod i want to call on the server, but can add more properties to the struct (Just basic types)

    On Server

         public override void OnStartServer()
         {
         base.OnStartServer();
        
         NetworkServer.RegisterHandler<CustomMessageCS>(OnCustomMessageReceived);
         }
    

    And this RegisterHandler should capture the message sent and popup OnCustomMessageReceived method.

         public void OnCustomMessageReceived(NetworkConnection conn, CustomMessageCS message)
         {
         if (message.IdMethod == 1)
             Do This Method on Server
         }
    

    And this should do the thing, in my opinion is a bit tedious and complex (lost like 5 days playing around) since in mirror definition they say its "easy to use". Its easy to use if you want to do a singleplayer tetris :(