Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Multiplayer Basics

Discussion in 'Multiplayer' started by ddeo, May 27, 2016.

  1. ddeo

    ddeo

    Joined:
    Feb 17, 2013
    Posts:
    12
    I give up, I am asking questions! Been at it for a whole day already and I can't get it around my head.

    How do make a multiplayer turn based tactical game?

    (Just so people don't say "do a pong game", Although I am NOT a proficient programmer especially with OOP, I can build this game if its offline, actually half the hard stuff is done already and I was starting to adjust my code for the networking stuff but then I found out the old system is being depreciated so here I am pulling my hair out on the new system).

    My issue #1:
    I can't figure out how to identify player1 from player2 then set a non synchronized property for each
    (I want to set a bool true/false for each but can't figure out how)I feel I am cheating the system saying NetworkServer.connections[1] as one of the players and sending it true while the other defaults to false.

    My issue #2:
    How to load a set of units from a save file on the local computer so that they are on their respective sides?
    (I have the save read/write ready to go)
    I want the server to own everything and people just "request" their moves on the host but when I try to call Commands that load my units i get the non-local player error. Note: Yes they are marked Command and yes they start with Cmd.

    My issue #3:
    How should I organize things? The samples on the docs are built for an FPS or MMO...It's almost like I am going against the grain here or is it just my weakness in coding department?
    So far I have an object with the networkmanager and hud, then another gameobject handles the logic. I dont know if the second gameobject is suppose to be local authority or even have a network identity (although I did something that says my script depends on it existing so i cant even remove it) but thats where the calls to command are located at the moment. Below is the exerpt of relevant code from the gameobject.

    Code (csharp):
    1.  
    2. void Start()
    3.    {
    4.      NetworkManager.singleton.client.RegisterHandler(101, OnChoose1stPlayer);
    5.      NetworkManager.singleton.client.RegisterHandler(102, OnLoadUnits);
    6.    }
    7.  
    8.  
    9.    public void StartGame(){
    10.    
    11.      if(isServer)
    12.      {
    13.        playerOne = NetworkServer.connections[1];
    14.      
    15.        foreach(NetworkConnection n in NetworkServer.connections)
    16.        {
    17.          if (n == null) continue;
    18.          Debug.Log(n.connectionId.ToString());
    19.        }
    20.        MyMessage msg = new MyMessage();
    21.        msg.boolean = true;
    22.        playerOne.Send(101, msg);
    23.        print.text += "/n" + firstPlayer;
    24.      
    25.        //loadUnits();
    26.        Generic loadMessage = new Generic();
    27.        playerOne.Send(102, loadMessage);
    28.        Debug.Log(playerOne);
    29.      }
    30.    
    31.    
    32.    
    33.    }
    34. public void OnLoadUnits(NetworkMessage netMsg)
    35.    {
    36.      //print.text = "\n" + "made it here";
    37.      Debug.Log("made it here");
    38.      if(File.Exists(Application.persistentDataPath + "/playerdata.dat"))
    39.      {
    40.        Debug.Log("Load");
    41.        BinaryFormatter bf = new BinaryFormatter();
    42.        FileStream file = File.Open(Application.persistentDataPath + "/playerdata.dat", FileMode.Open);
    43.        flatMap saveMap = (flatMap)bf.Deserialize(file);
    44.        file.Close();
    45.        if(GameOnline.firstPlayer)
    46.        {
    47.          for(int x = 0; x < Grid.MAPSIZE; x++)
    48.          {
    49.            for(int y = 0; y < Grid.MAPSIZE/2; y++)
    50.            {
    51.              if(Grid.Map[x,y].container != null)
    52.              {
    53.                Destroy(Grid.Map[x,y].container);
    54.              }
    55.            
    56.              if(saveMap.Map[y*Grid.MAPSIZE + x] != null && saveMap.Map[y*Grid.MAPSIZE + x] != "")
    57.              {
    58.                CmdloadUnit(x,y, saveMap.Map[y*Grid.MAPSIZE + x]);
    59.              }
    60.            }
    61.          }
    62.        }
    63.        else
    64.        {
    65.            for(int x = 0; x < Grid.MAPSIZE; x++)
    66.          {
    67.            for(int y = 0; y < Grid.MAPSIZE/2; y++)
    68.            {
    69.              if(Grid.Map[Grid.MAPSIZE-x-1,Grid.MAPSIZE-y-1].container != null)
    70.              {
    71.                Destroy(Grid.Map[Grid.MAPSIZE-x-1,Grid.MAPSIZE-y-1].container);
    72.              }
    73.            
    74.              if(saveMap.Map[y*Grid.MAPSIZE + x] != null && saveMap.Map[y*Grid.MAPSIZE + x] != "")
    75.              {
    76.                CmdloadUnit(Grid.MAPSIZE-x-1, Grid.MAPSIZE-y-1, saveMap.Map[y*Grid.MAPSIZE + x]);
    77.              }
    78.            }
    79.          }
    80.        }
    81.      }
    82.    }
    83.    [Command]
    84.    public void CmdloadUnit(int x, int y, string s)
    85.    {
    86.      GameObject newUnit = (GameObject)Instantiate(Resources.Load(s) as GameObject, new Vector3(x*Grid.tileSize,0,y*Grid.tileSize), Quaternion.identity);
    87.      NetworkServer.Spawn(newUnit);
    88.    }
    89. void Update()
    90.    {
    91.      if(Input.GetKeyDown(KeyCode.Space))
    92.      {
    93.        StartGame();
    94.      }
    95.      
    96.    }
    97.  
    Hope my problem is clear enough...though I doubt it.
     
    Last edited: May 27, 2016