Search Unity

Got an offline game - trying to make it multiplayer, but how?

Discussion in 'Multiplayer' started by Zythus, May 5, 2017.

  1. Zythus

    Zythus

    Joined:
    Jun 28, 2014
    Posts:
    30
    Hello lads, since I am completely green and even after watching tutorials I run into a lot of problems I thought I might as well ask here.
    I've got my game, which is mobile airplane controlled by virtual joystick, trying to get through checkpoints as fast as possible and then collecting the ,,goal".

    I've got offline setup working - method with adding checkpoints, upgrade system, loadJson and saveJson and bunch of other scripts. In my scene there are gameObjects like ScoreControl, UI (which has GameManager and other scripts), and of course the Player with its scripts for controlling the plane, camera and boost.

    I tried to make them work with NetworkingBehaviour and stuff, but I am not sure where to put these [command], [rpc] and [syncvar] stuff. For example if I have float speed for my player, void WinLevel() after collecting a goal, loading player's plane attributes at the start of the game from Json, coroutine at the start of the level and a bunch of GameObject.Find() - I am not sure which of these things should I mark appropriately. Like, all of the methods and all of the variables should be synced?

    Could someone enlighten me, even by a little?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You need to decide on what goals you have for making this a multiplayer game, what data you need to sync between all clients to accomplish that, what data doesn't need to be synced (every time you're going through the trouble of syncing a piece of data, understand what you're accomplishing with that data everywhere else you're syncing it, otherwise why are you trying to sync it?). Only sync the data needed to be synced. Also you need to figure out how you want players to join your games, if you want game histories recorded, etc.

    Commands are on scripts attached to either the player object or objects under that client's authority, and are used to call a method on the server's copy of that object. Rpc's are used for the reverse, and syncvars are used for syncing values from the server to the clients.

    Also, stop using GameObject.Find
     
    Last edited: May 5, 2017
    Zythus likes this.
  3. Zythus

    Zythus

    Joined:
    Jun 28, 2014
    Posts:
    30
    Hey, thanks for hints.
    Why wouldn't you recommend using GameObject.Find? I know it's processing heavy, but I use it only at the start and only a few times.
    Also, what about coroutines? Can I use them?
     
  4. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    GameObject.Find() is fine at the start, I use it a ton. Shouldn't use it unless sparingly beyond that though.
     
    Zythus likes this.
  5. Zythus

    Zythus

    Joined:
    Jun 28, 2014
    Posts:
    30
    Yeah, I thought so too.
    Right now the thing that most annoys me is loadJson. In SinglePlayer I just used to save and load all necessary stuff from Json file using this:
    Code (CSharp):
    1.         string jsonString = Application.persistentDataPath + "/Player.json";    
    2.         if (File.Exists(jsonString))                                        
    3.         {
    4.             using (FileStream file = File.Open(jsonString, FileMode.Open))  
    5.             {
    6.                 string content;                                              
    7.                 using (StreamReader reader = new StreamReader(file))        
    8.                 {
    9.                     content = reader.ReadToEnd();                            
    10.                 }
    11.                 Character player = JsonUtility.FromJson<Character>(content);
    But now it gives me null reference exceptions saying it doesnt know what loadJson is. This multiplayer thing is tricky compared to singleplayer ;/
     
  6. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    For online games (assuming its going to have online and offline modes) you could save "multiplayer related" stuff online (either a database, or some master server type thing) and have it track everyones online stuff there, and when playing locally, just use the old save to text file method...