- Home /
Multiplayer - sending a variable from server to client
Hello,i am pretty new in scripting and i got this problem : The problems appear when i am a client.If i am the server everything is ok.So,as you can see i am trying to send a value from a variable that only the server owns to the client.Only 1 way.Let's say that I want to send the client the current server time.Why if i print DispTemp inside SetVariable function i get the right number(25) and if i print it outside it i get something like : 100 and then 25 and then 100 and then 25 and so on...So this way when i use if(networkView.isMine && Network.isClient){ GUI.Box(new Rect(0,0,Screen.width / 16,Screen.height / 32),DispTemp + "C");} i'll always get 100 instead of 25 :S. As far as i noticed,DispTemp = 25 only when !networkView.isMine :S How can i make it to be equal with the value boadcasted by the server all of the time? Please help me and thanks in advance.An anwser in JavaScript would be welcome aswell :D
using UnityEngine; using System.Collections;
public class MonoTestVar : MonoBehaviour { int Temp = 25; int DispTemp = 100;
void Update(){ Debug.Log(DispTemp);
if(Network.isServer && networkView.isMine){ networkView.RPC("SetVariable", RPCMode.All, Temp);} }
[RPC] void SetVariable(int newTemp){ //This RPC is in this case always called by the server, // but executed on all the clients DispTemp = newTemp;
} void OnGUI(){ if(networkView.isMine && Network.isClient){ GUI.Box(new Rect(0,0,Screen.width / 16,Screen.height / 32),DispTemp + "C");} if(networkView.isMine && Network.isServer){ GUI.Box(new Rect(0,0,Screen.width / 16,Screen.height / 32),DispTemp + "C");} } }
Answer by AKAssassin · May 03, 2011 at 07:14 PM
If you want the server to change the value all of the time then take out the check networkView.isMine.
You wouldn't want to check that because even though the code is running on the server he still may not actually own the networkview, so you would just check.
if(Network.isServer){
networkView.RPC("SetVariable", RPCMode.All, Temp);}
}
That way the server will always update the value even if it doesn't own the networkView on the object.
Yeah,i tried that before but if i want Temp to increase like if(Network.isServer){ Temp += 1; networkView.RPC("SetPosition", RPC$$anonymous$$ode.All, Temp);} }
It will give me again 2 values : 1 is the one calculated by the server and the other one is calculeted by the client :S Please help me fix this :(
Baisicaly if i put a debug.log inside the SetVariable function DispTemp = Temp(the one calculated by the server) BuT if i place it in the update function for instance i'll get it's default value...Why is that happening?