xpc234
August 5, 2013, 12:59pm
1
Hi
i’m creating a network game
whenever i enter the client and press Connect it sends a request to the server and the server creates a gameobject with a random color.
the problem is that in the client the game object appears in different color and i need to sync the colors.
i tried to use rpc but i can’t pass gameobjects as parameters and passing the NetworkPlayer doesn’t help me.
How can i sync the color of the gameobjects?
Thx
Server side:
if(randomcolor == Color.Red){
//send string to client “c=red” for example
}
Client side:
If(serverInfo == “c=red”)
GameObject.renderer.material.color = Color.Red
and if you’re using a random red/blue/green format then send 3 random integer values.
And then make the server side never call the random function again, so the color stays the same for everyone.
xpc234
August 5, 2013, 9:36pm
3
Server side:
if(randomcolor == Color.Red){
//send string to client “c=red” for example
}
Client side:
If(serverInfo == “c=red”)
GameObject.renderer.material.color = Color.Red
and if you’re using a random red/blue/green format then send 3 random integer values.
And then make the server side never call the random function again, so the color stays the same for everyone.
Thx for the response
but in the game there are multiple number of players and when i’ll write
ill have to specify which gameobject i want to access and since i can’t pass gameobjects as parameters in rpc i don’t have a gameobject to refer to in the client.
xpc234:
Thx for the response
but in the game there are multiple number of players and when i’ll write
ill have to specify which gameobject i want to access and since i can’t pass gameobjects as parameters in rpc i don’t have a gameobject to refer to in the client.
You could try sending the name of the gameobject and then use GameObject.Find(nameString).renderer.material.color = Color.Red;
But that probably wouldn’t work if there are gameobjects with the same name.
Maybe u could go into further detail about your project, so i am able to help better c:
xpc234
August 6, 2013, 7:15am
5
I’m building a cell(living cell) simulator for a project.
the simulator consists of 2 project, the server and the client
this is the main script in the client:
using UnityEngine;
using System.Collections;
public class ClientMain : MonoBehaviour {
public string serverIP = “127.0.0.1”;
public int serverPort = 25000;
private bool responseToMouse;
private GameObject selectedObj;
void Start() {
responseToMouse = false;
selectedObj = null;
}
void connectToServer() {
Network.Connect(serverIP, serverPort);
}
void disconnectFromServer() {
Network.Disconnect();
}
void OnGUI() {
if(Network.peerType == NetworkPeerType.Disconnected) {
GUILayout.Label (“Not connected”);
if(GUILayout.Button (“Connect to server”)) connectToServer();
}
else {
if(Network.peerType == NetworkPeerType.Connecting) {
GUILayout.Label (“Connecting…”);
}
else {
GUILayout.Label(“Connected to server”);
GUILayout.Label("IP/Port: " + Network.player.ipAddress + “/” + Network.player.port);
if(GUILayout.Button (“Disconnect”)) disconnectFromServer();
}
}
}
void OnDisconnectedFromServer(NetworkDisconnection info) {
GameObject[ ] gos = GameObject.FindGameObjectsWithTag(“Player”);
foreach(GameObject go in gos) {
Destroy(go);
}
}
void Update () {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Physics.Raycast(ray, out hit, 2000);
if(hit.transform.gameObject.tag == “Player”) {
//if(hit.transform.gameObject.networkView.isMine) {
if(Input.GetMouseButton (0)) {
if(responseToMouse) {
selectedObj = hit.transform.gameObject;
responseToMouse = false;
}
}
else {
responseToMouse = true;
}
//}
}
else {
if(Input.GetMouseButton(0)) {
if(hit.transform.gameObject.name == “gamePlane” selectedObj != null) {
networkView.RPC(“handlePlayerInput”, RPCMode.All, Network.player, hit.point.x, hit.point.y);
selectedObj = null;
}
}
}
}
[RPC]
public void handlePlayerInput(NetworkPlayer player, float x, float y) {
}
}
and these are the 2 main scripts in the server
using UnityEngine;
using System.Collections;
public class ServerPlayerManager : MonoBehaviour {
public Hashtable players;
PlayerInfo ply;
public void Start() {
players = new Hashtable();
ply = gameObject.GetComponent(“PlayerInfo”) as PlayerInfo;
}
public void spawnPlayer(NetworkPlayer player) {
float x = Random.Range (-50, 50);
float y = Random.Range (-50, 50);
Color[ ] colors = new Color[ ] {Color.black, Color.blue, Color.cyan, Color.gray, Color.green, Color.grey, Color.magenta, Color.red, Color.yellow };
int rc = Random.Range (0, colors.Length - 1);
spawnPlayer(player, x, y, colors[rc]);
}
public void spawnPlayer(NetworkPlayer player, float x, float y, Color color) {
//ply.name = “Player”+player.ipAddress.ToString();
GameObject go = Network.Instantiate(ply.cellPrefab, new Vector3(x, y, 80), Quaternion.AngleAxis(90, Vector3.right), 0) as GameObject;
CellHandler ch = go.GetComponent(“CellHandler”) as CellHandler;
//ch.ResetCell (true, 0);
ch.SetColor(color);
players[player] = go;
ply.players[player] = go;
}
public void deletePlayer(NetworkPlayer player) {
GameObject go = players[player] as GameObject;
Network.RemoveRPCs(go.networkView.viewID);
Network.Destroy(go);
players.Remove (player);
}
[RPC]
public void handlePlayerInput(NetworkPlayer player, float x, float y) {
GameObject go = players[player] as GameObject;
CellHandler ch = go.GetComponent(“CellHandler”) as CellHandler;
ch.originalPosition.x = x;
ch.originalPosition.y = y;
ch.haveToMove = true;
}
}
using UnityEngine;
using System.Collections;
public class ServerMain : MonoBehaviour {
public int listenPort = 25000;
public int maxClients = 15;
private ServerPlayerManager spm;
void Awake() {
spm = gameObject.GetComponent(“ServerPlayerManager”) as ServerPlayerManager;
}
void OnPlayerConnected(NetworkPlayer player) {
spm.spawnPlayer(player);
}
[RPC]
public void SyncColor(NetworkPlayer player) {
}
void OnPlayerDisconnected(NetworkPlayer player) {
spm.deletePlayer(player);
}
void startServer() {
Network.InitializeServer(maxClients, listenPort, false);
}
void stopServer() {
Network.Disconnect();
}
void OnGUI() {
if(Network.peerType == NetworkPeerType.Disconnected) {
GUILayout.Label(“Server is not running”);
if(GUILayout.Button (“Start Server”)) startServer();
}
else {
if(Network.peerType == NetworkPeerType.Connecting) {
GUILayout.Label (“Server is starting up…”);
}
else {
GUILayout.Label(“Server is running.”);
showServerInformation();
showClientInformation();
}
if(GUILayout.Button(“Stop Server”)) stopServer();
}
}
void showClientInformation() {
GUILayout.Label ("Clients: " + Network.connections.Length + “/” + maxClients);
foreach(NetworkPlayer p in Network.connections)
GUILayout.Label("Player from ip/port: " + p.ipAddress + “/” + p.port);
}
void showServerInformation() {
GUILayout.Label ("IP: " + Network.player.ipAddress + " Port: " + Network.player.port);
}
}
(there’s also a script for handling the cell behavior but i guess it’s not important for the problem)
btw, i’m using this tutorial: 3dgep.com/?p=4609
thx for the help
appels
August 6, 2013, 9:04am
6
The owner of the object needs to update the clients which color the object has. This can be done using RPC’s.
An rpc gets called on the gameobject it is exececuted.
You can’t send a color through RPC so you will have to find another way. Convert it to int’s or serialize the color and send it.
So on the server you can do something like :
Color CubeColor = Color.red;
renderer.material.color = CubeColor;
string xmlString = SerializeToXmlString(CubeColor);
networkView.RPC("SetColor", RPCMode.AllBuffered, xmlString);
To serialize to an xml string:
string SerializeToXmlString(Color thiscolor)
{
XmlSerializer serializer = new XmlSerializer(typeof(Color));
StringWriter sw = new StringWriter();
serializer.Serialize(sw, thiscolor);
return sw.ToString();
}
Then on the client just deserialize:
[RPC]
void SetColor(string playercolor)
{
Color c = DeserializeXmlString(playercolor);
renderer.material.color = c;
}
Color DeserializeXmlString(string xmlString)
{
XmlSerializer serializer = new XmlSerializer(typeof(Color));
StringReader sr = new StringReader(xmlString);
return (Color)serializer.Deserialize(sr);
}
xpc234
August 6, 2013, 2:00pm
7
appels:
The owner of the object needs to update the clients which color the object has. This can be done using RPC’s.
An rpc gets called on the gameobject it is exececuted.
You can’t send a color through RPC so you will have to find another way. Convert it to int’s or serialize the color and send it.
So on the server you can do something like :
Color CubeColor = Color.red;
renderer.material.color = CubeColor;
string xmlString = SerializeToXmlString(CubeColor);
networkView.RPC("SetColor", RPCMode.AllBuffered, xmlString);
To serialize to an xml string:
string SerializeToXmlString(Color thiscolor)
{
XmlSerializer serializer = new XmlSerializer(typeof(Color));
StringWriter sw = new StringWriter();
serializer.Serialize(sw, thiscolor);
return sw.ToString();
}
Then on the client just deserialize:
[RPC]
void SetColor(string playercolor)
{
Color c = DeserializeXmlString(playercolor);
renderer.material.color = c;
}
Color DeserializeXmlString(string xmlString)
{
XmlSerializer serializer = new XmlSerializer(typeof(Color));
StringReader sr = new StringReader(xmlString);
return (Color)serializer.Deserialize(sr);
}
that’s not the problem
i don’t have any problem passing the color as a parameter
my problem is to what gameobject i should assign the color in the client? i can’t pass gameobjects through rpc and all the gameobjects in my game have the same name so i can’t know…
appels
August 6, 2013, 2:18pm
8
You don’t have to know…
As I stated before, the command is executed on the GameObject it was called on.
xpc234
August 6, 2013, 5:27pm
9
Oh… so i can execute and recieve a rpc command from a script that’s attached to the game object?
Ok, it works
thank you for the help