Search Unity

Most efficient route for having a game that can be both single player and coop?

Discussion in 'Multiplayer' started by The702Guy1, Feb 5, 2023.

  1. The702Guy1

    The702Guy1

    Joined:
    Apr 24, 2022
    Posts:
    48
    So I have a game that is already pretty far in development, it is my first real project and when I started I didn't have any intention to go with multiplayer. Now that it is in Early Access, some of my players as well as prospective players have been asking about a multiplayer addition and that has made me decide to try and implement one. However I don't know which way would be more efficient to implement it.

    I have one map currently that is a single scene but plan to add more and what I'm trying to decide is should I re-write all of my code to fit both single and multiplayer in one scene, or should I make a copy of the current version of the scene and make that individual scene for multiplayer and leave the other one alone for single player? The scene data is not persistent and resets at the beginning of each game, and I don't plan on letting players join someone who hasn't actively created a lobby.

    I am very new to multiplayer and I currently have a prototype for this working and with what I've been learning along the way makes it seem like it would be a much easier solution to basically have 2 scenes of the same map, one for offline single player and one for coop play as well as having 2 sets of scripts that handle single player vs multiplayer (e.g. offline player movement script that is MonoBehaviour instead of NetworkManager and one that is the opposite).

    Thoughts?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,975
    The only solution that makes sense if you want to keep improving that game and/or add more content to it is to enable multiplayer all the way through, including the single-player experience which would be a host playing by itself.

    But if that single-player experience is "done" and you like to experiment on how to make just that scene multiplayer then you should opt for making a copy of the scene or project. Which in turn could be extended to support singleplayer, and then you'd have both in one project/scene again.
     
  3. The702Guy1

    The702Guy1

    Joined:
    Apr 24, 2022
    Posts:
    48
    The only concern I have with this is I don't want to force players to have to be online if they just want to play the single player version. If I did the single player as a host playing by itself wouldn't that require the player to maintain an online connection?
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,975
    No.

    In fact you want to add extra code to prevent client connections (during approval) in case someone tries to join without the hosting player opening the game for others to join.

    But it does get a little more complicated if you integrate a relay, matchmaking or lobby service. You need to make sure the singleplayer mode doesn't use any of these services since those will require internet connection. At the same time bypassing these services during singleplayer mode will make it harder or impossible for players to join over the Internet when the hosting singleplayer decides to "open game" like in Minecraft.

    Thus in such a situation where the game should become "open" at any point in time the singleplayer game needs to save its state (if necessary), end the network session, connect to relay etc service and start hosting through those services so others can join, and then (optionally) load the saved state if needed.