Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How can I write code that can be used for singleplayer or with different networking solutions?

Discussion in 'Scripting' started by DCTShinobi, Oct 18, 2022.

  1. DCTShinobi

    DCTShinobi

    Joined:
    Jun 23, 2014
    Posts:
    51
    I've been working on a project to create an asset for the store. Originally, I focused on getting the project to work for offline singleplayer. However, this particular project is much more useful for a multiplayer/networked game. So I shifted focus to writing code for a specific networking solution (Mirror in this case).

    The content in the project is nearly complete, but I am now looking for the wisest way to make my asset to be more universal, so it can be used for offline singleplayer or online multiplayer using multiple networking solutions (Mirror, Photon, etc.). I have tried to stay relatively simple with what's required for networking (IsServer, IsLocalPlayer, SyncVar, etc.).

    Do I need to create a separate version of each script for each networking solution (plus another for singleplayer)? Do I need to create a separate asset/package for each networking solution? Is there a way to write a single script that would run certain lines of code with an #IF keyword for each possible network solution? Could I use IsServer only once to set a global bool that all of my other scripts could run off of?

    Even examples or tutorials of such a concept would be welcome.

    Thank you! :-D
     
  2. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    Well, what do you mean by this, do you want your game to be able to integrate a photon solution, mirror solution etc? or just 1, if it's just 1, no you don't need to create a script for online and offline, you can simply use cases, IFs etc, I only know about photon so you can use
    Code (CSharp):
    1. PhotonNetwork.OfflineMode
    To check for if the game is offline, or online, you can set this to true or false however you like, you can have a dropdown saying online or offline, whatever your game concept is.
    If your trying to make an asset which can integrate both mirror and photon, you will have to create at least create 2 scripts for each version, maybe 3, depending if mirror has offline options like pun does
     
  3. DCTShinobi

    DCTShinobi

    Joined:
    Jun 23, 2014
    Posts:
    51
    Thank you. Yes, I'm creating an asset for others to use in their games, so I don't know which networking solution they will choose to use. So I will try to make it functional for at least the most popular ones as well as for no networking.
     
  4. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    Well, if your creating an asset, using at least 2 solutions would be great for the buyer, but yes it will be complicated to be able to learn both networking solutions and make a fully functioning game using 2 or more solutions. If your new to networking, starting with 1 would be the wisest choice, let me know if you need any help
     
    DCTShinobi likes this.
  5. DCTShinobi

    DCTShinobi

    Joined:
    Jun 23, 2014
    Posts:
    51
    Thank you so much! Yes, I'm going to get it working with Mirror, then adapt it to Photon. If I have any questions, I'll definitely let you know, thank you again! :-D
     
    Johan_Liebert123 likes this.