Search Unity

Question Need help adapting local multiplayer to online multiplayer

Discussion in 'Multiplayer' started by verdecillo66, Nov 13, 2021.

  1. verdecillo66

    verdecillo66

    Joined:
    Sep 15, 2017
    Posts:
    5
    Hello.
    I'm working on a small 2D game. It has both a single-player mode and a local-multiplayer mode. Of course, in both modes the code and prefabs for the players are the same and there are no problems with that. The thing is, I want to add online-multiplayer. I'll probably use Mirror and I'll follow some tutorials online to adapt the game.

    However, in all of those tutorials, it is asumed that the game is started from the ground-up, but mine isn't. My question is, how do I adapt the code for online play? Should I duplicate the already existing scripts and prefabs and change them to fit Mirror Networking? That seems logical, but I'd rather not have two identical scripts when I can just have one. Would the game break if I used Mirror functions even when playing on single-player modes?

    Thank you.
     
    Last edited: Nov 13, 2021
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    A trick, to run single player in a multiplayer game is to actually run a host locally, even for a one player multiplayer session. Many networking solutions will support this approach and as you build the UI, you can hide this fact entirely. I guess this is a simple approach, when the singleplayer campaign is very similar to what the multiplayer is.
    I would not "copy" all scripts but instead refactor them to multiplayer. You need to know how the networking solution works to apply the changes and you may have to refactor some systems to fit multiplayer in.
     
    verdecillo66 likes this.