Simple Network Example

I’m just starting out (still using the trial) and I need networking (no single player mode planned), but the docs are somewhat lacking.

Does anyone have a really simple network multiplayer example? Two spheres in a box would do, as long as it shows the networking code on both server and client.

Well, make that an empty box and a sphere spawned for every client that connects. Really, extremely primitive, no FPS code, no explosions, just something that shows a working example of how to init a server, how to init a client and make it connect.

Please?

Now, not in three weeks when my trial period is up?

Please with sugar? :slight_smile:

It’s coming. UT has in the past been pretty generous with trial extensions if you have a decent reason to need one… when the example does come out you’ll probably be able to get an extension by emailing them explaining your situation.

bump I want that too !! very much

I’ll bump this as well.

I’m new, still in the trial and attempting to absorb as much as possible. I’ve found the documentation to be excellent but a networking tutorial would be very helpful.

Thanks.

Network tutorial is coming soon as I understand.

Within a week or so… Stay tuned! :slight_smile:

That’s great news, thanks.

Not sure if this is useful or not, but here’s a very short script to do some basic networking including NAT punching. It’s a super simple RPC broadcast to get P2P chat working. Sorry JS guys its in C#.

  1. Paste it into a C# Script called NetworkTest.cs and add it to a GameObject.

  2. Change the P2PGameType and P2PGameName.

EDIT:
3. Add a Network View to the GameObject, turn of state sync and observed to Nothing.

To be a server - click Start Server, then Register with Master Facilitator - wait 3 to 5 seconds and your game should appear below.

To be a client, just click Get Host List. This will connect to the Master Facilitator and get a list of games of the GameType you specified. At the bottom of the list (you may need to scroll) - there is a connect button - click it.

Start typing in the Chat window, and if everything is working OK, all members of session should get the text.

using UnityEngine;
using System.Collections;
using System;

public class NetworkTest : MonoBehaviour
{
    private Rect NTWindowPos;
    private Rect ChatWindowPos;
    private ConnectionTesterStatus natCapable;
    private HostData[] testHD;
    private Vector2 HostListScrollWindowPos;
    private Vector2 ChatHistoryScrollWindowPos;
    private bool PollforHostList = false;
    private string MessageToSend = "";
    private ArrayList ChatHistoryArray;
    private string P2PGameType = "CHANGEME";
    private string P2PGameName = "CHANGEMETOO";

    // Use this for initialization
    void Start()
    {
        NTWindowPos = new Rect(20, 100, 320, 400);
        ChatWindowPos = new Rect(360, 100, 320, 400);
        ChatHistoryArray = new ArrayList();
        ChatHistoryArray.Add("Chat Started at " + DateTime.Now.ToString());
        natCapable = ConnectionTesterStatus.Undetermined;
    }
    IEnumerator PollForHostListUpdates()
    {
        Debug.Log("Polling HostList" + Time.realtimeSinceStartup.ToString());
        MasterServer.RequestHostList(P2PGameType);
        testHD = MasterServer.PollHostList();
        yield return new WaitForSeconds(3);
        PollforHostList = true;
    }
    [RPC]
    void ApplyGlobalChatText(string MessageToAdd)
    {
        ChatHistoryArray.Add(MessageToAdd);
    }
    void OnGUI()
    {

        NTWindowPos = GUI.Window(5, NTWindowPos, DoNetworkTester, "P2P Network Connecter");
        ChatWindowPos = GUI.Window(6, ChatWindowPos, DoChatWindow, "P2P Chat");
    }
    void DoChatWindow(int WindowID)
    {
        GUILayout.BeginVertical();
        GUILayout.Space(25);
        GUILayout.Label("Chat History");
        ChatHistoryScrollWindowPos = GUILayout.BeginScrollView(ChatHistoryScrollWindowPos);
        foreach (object itemStr in ChatHistoryArray)
        {
            GUILayout.Label((string)itemStr);
        }
        GUILayout.EndScrollView();
        GUILayout.BeginHorizontal();
        MessageToSend = GUILayout.TextArea(MessageToSend);
        if (GUILayout.Button("Send", GUILayout.Width(64), GUILayout.Height(64)) || (Event.current.character == char.Parse("\n")  Event.current.type == EventType.keyUp))
        {
            networkView.RPC("ApplyGlobalChatText", RPCMode.All, MessageToSend);
            MessageToSend = "";
        }
        GUILayout.EndHorizontal();
        GUILayout.EndVertical();
        GUI.DragWindow();
    }

    void DoNetworkTester(int WindowID)
    {
        GUILayout.BeginVertical();
        GUILayout.Space(25);
        GUILayout.BeginHorizontal();
        GUILayout.Label("NAT Available: " + natCapable.ToString());
        if (GUILayout.Button("Test NAT")) natCapable = Network.TestConnection();
        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();
        GUILayout.Label("PeerType: " + Network.peerType.ToString());
        GUILayout.EndHorizontal();
        
        if (GUILayout.Button("Start Server"))
        {
            Network.useNat = true;
            Network.InitializeServer(32, 25002);
        }
        if (GUILayout.Button("Disconnect"))
        {
            Network.Disconnect(200);
        }
        
        
        if (GUILayout.Button("Register with Master Facilitator"))
        {
            MasterServer.RegisterHost(P2PGameType, P2PGameName);
            PollforHostList = true;
        }
        
        
        if (GUILayout.Button("Check Host List") || PollforHostList)
        {
            StartCoroutine(PollForHostListUpdates());
            PollforHostList = false;
        }
        
        HostListScrollWindowPos = GUILayout.BeginScrollView(HostListScrollWindowPos);
        if (testHD != null)
        {
            foreach (HostData itemHD in testHD)
            {
                GUILayout.BeginVertical();
                GUILayout.Label("Type: " + itemHD.gameType);
                GUILayout.Label("Name: " + itemHD.gameName);
                foreach (string thisIP in itemHD.ip)
                {
                    GUILayout.Label("IP Route: " + thisIP.ToString());
                }
                GUILayout.Label("Port: " + itemHD.port.ToString());
                GUILayout.Label("NAT Enabled: " + itemHD.useNat.ToString());
                GUILayout.Label("Players: " + itemHD.connectedPlayers.ToString() + "/" + itemHD.playerLimit.ToString());
                GUILayout.Label("Password: " + itemHD.passwordProtected.ToString());
                GUILayout.Label("Comment: " + itemHD.comment);
                if (GUILayout.Button("Connect"))
                {
                    //Network.useNat = true;
                    Network.Connect(itemHD.ip, itemHD.port);
                }
                GUILayout.EndVertical();
            }
        }
        GUILayout.EndScrollView();
        GUILayout.EndVertical();
        GUI.DragWindow();
    }
}

Wow shaun thank you !!! – this is what I’ve been looking for.

I can get two machines to almost talk to each other, but not quite –

I can start a server, and register it, and connect with the other machine – the display shows two machines on the server – but nothing happens on either machine when I type.

is this a network view problem I wonder???

EDIT: All I needed to do was attach a network view component to the gameobject with the script.

Thanks again shaun… some of you guys can put this stuff together without working examples, but I’m just a dumb right-brainer. I need a working example to take apart and learn from…

HiggyB =-

within a week or so? w00t w00t

Merry Christmas EvErYoNe!!!

Unclet: you’re absolutely right. thanks :slight_smile:
I’ve updated the post with the additional step.

EDIT:
After getting some further infomation, I realize that Network.TestConnection is an asynchronous function, which means you must keep calling it until it returns a value. Once it returns a value, it wont test again, unless you set the forceTest paramter to true. So, keep clicking that Test NAT button, or copy the connection testing code into OnGUI and set forceTest=true in the button code :wink: