2
\$\begingroup\$

In Unity I converted my single player game to a multiplayer game. Now, the single player game does not work anymore because the objects are disappearing on start, due to the network identity.

What is the best practice for still using the same objects in a single player game?

My question is about a general implementation, and not about something in my game specific.

Example:

We create a simple single player game:

  • Make a sphere
  • Make a script to move it with arrows

We have a single player game!!

  • Change the script to be a networkbehaviour
  • Check for islocalplayer in the script
  • Make the sphere a network identity
  • Make a network controller

Now we have a multiplayer "game"!

HOWEVER

If we now want to play this game singleplayer, it will not work. Just try adding the sphere to a scene. It will disappear on start.

What can I do to make the object usable in both single and multiplayer scenes?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ We're going to need a lot more details about how you're managing your objects & networking in single-player to be able to help you debug this. Can you include some relevant code? Ideally a minimal sample that demonstrates the problem. \$\endgroup\$
    – DMGregory
    Commented Feb 24, 2016 at 13:14
  • \$\begingroup\$ There is not enough information to provide you with any type of answer that would be useful to you. What is wrong with the network identity? Are you providing a unique ID for each object? Can you provide some code/screenshots of what you have tried? \$\endgroup\$
    – jgallant
    Commented Feb 24, 2016 at 13:24
  • \$\begingroup\$ Thank you for taking an interest! I have edited my question. I hope it is a bit more clear. If it is not, just ask :) \$\endgroup\$
    – Jasper
    Commented Feb 24, 2016 at 14:17

2 Answers 2

1
\$\begingroup\$

You may be overthinking it. Perhaps you can simply start your game as a host and not allow any additional connected players.

The NetworkManager enables your game to have multiple clients. It does not require you to. Trying to build a second version of your game for single players is often needless effort.

\$\endgroup\$
2
  • \$\begingroup\$ This is actually the solution I went with. Added a script that does "GetComponent<NetworkManager>().StartHost();" on start() method. \$\endgroup\$
    – Jasper
    Commented Mar 2, 2016 at 9:21
  • \$\begingroup\$ Do you know if it will still popup the windows firewall notification, and if there is a way to disable this for singleplayer games that i host like this? I don't want to spook the players \$\endgroup\$
    – Jasper
    Commented Mar 2, 2016 at 9:21
0
\$\begingroup\$

From Philipp's answer here:

For an online Multi player game, we take pong game as example.

The MVC pattern is a good solution here. You would need three different controllers:

  • The LocalPlayerController which is sending commands to a paddle by listening to the local input devices. If you have multiple input devices (keyboard, mouse, gamepad...) it might be a good idea to further subclass it into the one it listens to. In any case, these class(es) should be used in both singleplayer and multiplayer, because it makes no difference for the control who you are playing against.
  • The AIController which analyzes the current game state and sends commands to an ai-controlled paddle.
  • The RemotePlayerController which listens to a network socket and interprets the incomming messages as commands to a paddle.

Regarding network output: This would not be another Controller, this would be another View. A view is not just something you see on the screen. It is any form of output representation of the model (the model in the context of game development would be the game state). It can be a graphical output on the screen or a data stream sent over the network. In this case the NetworkView would observe any paddles controlled by LocalPlayerControllers or AIControllers and translate their movement to the network.

\$\endgroup\$
5
  • \$\begingroup\$ Link-only answers are discouraged here. This answer would be better if you included a summary of the key points found at the link, so that it remains a useful signpost even if the linked resource later becomes unavailable. \$\endgroup\$
    – DMGregory
    Commented Feb 25, 2016 at 12:50
  • \$\begingroup\$ Thank you for your answer Manoj. I think I understand what you mean, but I am not sure how this will translate to a practical Unity example. Could you maybe explain that? \$\endgroup\$
    – Jasper
    Commented Feb 26, 2016 at 8:45
  • \$\begingroup\$ Also: what do you mean with paddle? \$\endgroup\$
    – Jasper
    Commented Feb 26, 2016 at 8:45
  • \$\begingroup\$ @Tsuyosa paddle means your player. assume pong game as example. For an online multiplayer game you have to manage 3 things mainly. 1. Local Player, 2. AI, 3. Remote Player. the thing explained above is based on this only \$\endgroup\$ Commented Feb 26, 2016 at 10:00
  • \$\begingroup\$ Thank you for your answer, but it does not really answer my question. Unity already handles things like this for me. My question is about using the same object for both single player and multiplayer games, inside the Unity framework. This does not answer that. \$\endgroup\$
    – Jasper
    Commented Feb 27, 2016 at 13:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .