Turn based networking in Unity

Madmenyo

I want to create a turn based network game so somewhere I have to tell the player to wait for there turn. This looks like a simple problem but I am struggling with it for many days now. First I talk you trough what I currently have, please tell me if I am doing something wrong at any stage.

Before the game starts a player enters a lobby and can join games (or possibly create them). Currently my lobby has just 2 buttons where a server can be created and a server list retrieved. Servers show up in buttons once retrieved.

When a player starts a server this script runs:

private void StartServer()
{
    Network.InitializeServer(2, 25000, false);
    MasterServer.RegisterHost(gameName, "Test game 2", "Welcome!");
    GameObject canvas = GameObject.Find("Canvas");
    canvas.SetActive(false);
}

As far as I know this player is now currently the server.

Another player can refresh the list now and join this server with the HostData presented by the MasterServer by clicking the button:

private void HostButton(HostData hostData)
{
    Debug.Log(hostData.gameName);
    Network.Connect(hostData);
    Application.LoadLevel("GameScene");
}

As you can see, a new scene has been loaded after connecting. When the connection is made I do the same for the server:

private void OnPlayerConnected(NetworkPlayer player)
{
    Debug.Log(player.externalIP + " Has connected!");       
    Application.LoadLevel("GameScene");
}

Now both players (client and server) are in the game scene and they are still connected. How would I give the turn to the server and let the client wait? Just feedback from pressing a button would be enough for me to understand. Like only allow Input Feedback for a single player. This is what I tried in the new scene:

void Start () 
{
    players = Network.connections;
    foreach (NetworkPlayer player in players)
    {
        Debug.Log(player.externalIP + ":" + player.externalPort + " entered the game!");
    }
    Debug.Log(players.GetLength(0)); //returns 1. Obviously because server to client is one conncetion. But what is the NetworkPlayer of the server?
}

So obviously the following code to check which player can act does not work.

void Update () 
{
    if (players[currentPlayer] == Network.player)
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("Player " + (currentPlayer + 1) + " fired mouse!");
            currentPlayer++;
            if (currentPlayer > 1) currentPlayer = 0;
        }
    }
}

I am completely stuck for days, all tutorials about networking show two floating cubes but do not show how to distinct players at all. How can I restrict a single player whether he created the server or joined as a client from moving until the current player has acted?

So I am asking for hints on how to implement a very basic turn based system between the server player and the connected client(s).

Venkat at Axiom Studios

While I've never used MasterServer myself, I believe that NetworkView.RPC calls should work. RPC or Remote Procedure Calls are calls sent over the network, and are executed on all peers, provided a matching public method is found.

I won't go into too many details about RPC here, it's well detailed on Unity's NetworkView.RPC docs here

To your problem, here's some code you can try

Create a new C# script, and name it RPCExample

RPCExample.cs

using UnityEngine;
using System.Collections;

public class RPCExample : MonoBehaviour {

    public static RPCExample instance;

    //Assign this NetworkView in the inspector
    public NetworkView networkView;

    //A boolean that states whether this player is the server
    //or the client
    public bool thisIsTheServer = false;

    //This is a boolean that will control whether it is THIS
    //player's turn or not
    public bool isMyTurn = false;


    void Awake () {
        instance = this;
    }

    //This method will set whether this connected player is the server or not
    [RPC]
    public void SetAsServer (bool isServer) {
        thisIsTheServer = isServer;
    }

//
    [RPC]
    public void SetTurn (bool turn) {
        isMyTurn = turn;
    }

    [RPC]
    public void FinishTurn () {
        networkView.RPC("SetTurn", RPCMode.Others, new object[] { true });
        SetTurn(false);
    }


    public void Update () {

        //It is not this player's turn, so early exit
        if(!isMyTurn)
            return;

        //If execution reaches here, it is this player's turn
        Debug.Log (string.Format("It is {0}'s turn!", 
                             new object[] { (thisIsTheServer) ? "Server" : "Client" }));

    }
}

Next, attach this script to your player GameObjects, and assign the NetworkView variable in the inspector.

Now, change your StartServer method to look like

private void StartServer() {
    Network.InitializeServer(2, 25000, false);
    MasterServer.RegisterHost(gameName, "Test game 2", "Welcome!");
    GameObject canvas = GameObject.Find("Canvas");
    canvas.SetActive(false);

    //Set this player as server
    RPCExample.instance.SetAsServer(true);
}

Change your HostButton method as follows

private void HostButton(HostData hostData) {
    Debug.Log(hostData.gameName);
    Network.Connect(hostData);
    Application.LoadLevel("GameScene");

    //This should only be executed on the client obviously.
    //Ensure you handle that scenario.
    RPCExample.instance.SetAsServer(false);
}

Now, for this example, lets assume that the server ALWAYS goes first (you can easily change that). So when your game scene loads (the one you've loaded with Application.LoadLevel("GameScene")), call the following method ON THE CLIENT

void Start () {
    //By calling FinishTurn on the client, we pass the turn onto the server
    //You can always rewrite this to use a random value, like a coin flip
    RPCExample.instance.FinishTurn();
}

And that's it!. When you're done with the current players' turn, just call

RPCExample.instance.FinishTurn();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot turn wireless networking off

From Dev

Cannot turn wireless networking off

From Dev

Custom UI for Google Games Services turn based multiplayer in Unity

From Dev

Networking pattern based on NSURLSession

From Dev

Subnetting based on networking needs

From Dev

"Failed to send big message" in Unity networking

From Dev

What is the equivalent of RPC in new Unity Networking?

From Dev

Unity 3D 4 Networking issues

From Dev

"Failed to send big message" in Unity networking

From Dev

Unity Networking [UNET]: Syncronizing static scene objects

From Dev

Unity3d LLAPI Networking: custom networking class not being used (NetworkConnection.TransportRecieve)

From Dev

Unity 5.1 Networking - Spawn an object as a child for the host and all clients

From Dev

Unity Networking: Flipping sprites in Multiplayer doesn't flip host

From Dev

Unity - Networking. Host can't see client changes

From Dev

Instantiate new "players" in a sub folder in Hierarchy view | Unity networking with Vuforia

From Dev

Storyboard - Workflow for turn based game

From Dev

Unity3d - Online multiplayer Turn

From Dev

How do I turn unity back on?

From Dev

PodSixNet multiplayer networking library for Python game based on Tkinter

From Dev

Networking, monitor and limits by data usage based on IP or user

From Dev

PodSixNet multiplayer networking library for Python game based on Tkinter

From Dev

Set networking so as to turn Wi-Fi on/off when X process is started/interrupted (Linux)

From Dev

Can I use the Unity networking HLAPI without paying for the Unity Multiplayer service?

From Dev

Two-player - turn-based game

From Dev

Game Center - turn based game issue

From Dev

API for turn-based multiplayer game

From Dev

Multiplayer turn-based game with Rails 4

From Dev

Creating user turn based game in Javascript

From Dev

Two-player - turn-based game

Related Related

  1. 1

    Cannot turn wireless networking off

  2. 2

    Cannot turn wireless networking off

  3. 3

    Custom UI for Google Games Services turn based multiplayer in Unity

  4. 4

    Networking pattern based on NSURLSession

  5. 5

    Subnetting based on networking needs

  6. 6

    "Failed to send big message" in Unity networking

  7. 7

    What is the equivalent of RPC in new Unity Networking?

  8. 8

    Unity 3D 4 Networking issues

  9. 9

    "Failed to send big message" in Unity networking

  10. 10

    Unity Networking [UNET]: Syncronizing static scene objects

  11. 11

    Unity3d LLAPI Networking: custom networking class not being used (NetworkConnection.TransportRecieve)

  12. 12

    Unity 5.1 Networking - Spawn an object as a child for the host and all clients

  13. 13

    Unity Networking: Flipping sprites in Multiplayer doesn't flip host

  14. 14

    Unity - Networking. Host can't see client changes

  15. 15

    Instantiate new "players" in a sub folder in Hierarchy view | Unity networking with Vuforia

  16. 16

    Storyboard - Workflow for turn based game

  17. 17

    Unity3d - Online multiplayer Turn

  18. 18

    How do I turn unity back on?

  19. 19

    PodSixNet multiplayer networking library for Python game based on Tkinter

  20. 20

    Networking, monitor and limits by data usage based on IP or user

  21. 21

    PodSixNet multiplayer networking library for Python game based on Tkinter

  22. 22

    Set networking so as to turn Wi-Fi on/off when X process is started/interrupted (Linux)

  23. 23

    Can I use the Unity networking HLAPI without paying for the Unity Multiplayer service?

  24. 24

    Two-player - turn-based game

  25. 25

    Game Center - turn based game issue

  26. 26

    API for turn-based multiplayer game

  27. 27

    Multiplayer turn-based game with Rails 4

  28. 28

    Creating user turn based game in Javascript

  29. 29

    Two-player - turn-based game

HotTag

Archive