- Home /
Create and Keep Track of Player LIst in Server...
Hi guys,
Ok, for a few days now Ive been trying to create a client list on a server. Now, I know this is probably not too difficult but since Im no experienced programmer its just being kind of difficult! IIve read some tutorials (Leepos and the Networking) but since most of the code is in js and part of their own project its being very complicated for me to grasp the functionality and stick it to my code... I kind of understand the concept of how it works though.
The reason Im trying manage client list is because of all the issues using network.instantiate and destroy.
So, basically the design Im using is clients control themselves (movement, firing etc.).
Each client instantiates itself (game character) and sends an RPC to all (buffered) wich instantiates a clone of that game character with respective ID locally on each other client.
What I want is to be able to create on server, a list o players, with their names and ID and have the server manage the instantiation and destroying the clients that arrive or leave (instead of using buffered RPCs).
I just dont know where to start. Heres what I have untill now:
using UnityEngine; using System.Collections;
public class Client_Start_Script : MonoBehaviour { public Transform playerPrefab; public Transform start_camera_Prefab;
public Camera client_camera; private Vector3 player_spawn_position; private Vector3 server_spawn_position;
public string player_name = null;
//Server-only playerlist public ArrayList playerList = new ArrayList();
public class PlayerNode { public string player_name; public NetworkPlayer networkPlayer; }
void Start ()
{ server_spawn_position = new Vector3(-3, 3,-20); player_spawn_position = new Vector3(-3,20,-20); Instantiate(start_camera_Prefab, server_spawn_position, transform.rotation);
}
//Server function
void OnServerInitialized() { PlayerNode newEntry = new PlayerNode(); newEntry.player_name=player_name; newEntry.networkPlayer=Network.player; playerList.Add(newEntry);
}
void OnGUI() {
if (Network.isClient)
{
if (GUILayout.Button("SpawnPlayer"))
{
//Network.Instantiate(playerPrefab, player_spawn_position, transform.rotation, 0);
NetworkViewID viewID = Network.AllocateViewID();
networkView.RPC("SpawnPlayer", RPCMode.AllBuffered, viewID, transform.position);
}
}
}
[RPC] void SpawnPlayer(NetworkViewID viewID, Vector3 location)
{ Transform clone; clone = Instantiate(playerPrefab, location, Quaternion.identity) as Transform; NetworkView nView; nView = clone.GetComponent<NetworkView>(); nView.viewID = viewID;
}
}
I'm trying to figure out pretty much the same thing. Hopefully someone will help us out, but I'll let you know if I find out anything.
I've been figuring out the same, and after 3 days of struggling, I believe that I've found a decently solid solution. I'll try to describe it a bit better soon as an answer.
Let's say for now: I managed to maintain a players array on the server that keeps track of every client by his player name and GUID. This array will then (later) be used to store more sophisticated player information (like scores etc.).
Note that my solution still relies on some RPC calling, but the server has do$$anonymous$$ance over the list. This is especiallay helpful for tracking improper client logoffs (such as crashes or users simply closing the window). If such things happen, the server will automatically unregister the player from the list.