- Home /
Network - Change Varible Across Clients
Hello,
I have a script in my scene that controls the scoring of my game. There is only one instance of this script, and new one IS NOT instantiated when a new player joins.
So there is always one. My question is, if the server makes a change to a variable on this script, will all the clients receive these changes too? Automatically?
So for example, there is a variable called overallScore, in which the server controls. If the server changes this value to 10, will this value change to 10 on the client scripts too?
My understanding is that, if you have one of something in the scene (this script in this case), and a client joins, you technically now have 2 instance of that script and so you need to sync the two together. Correct?
Example:
if(Network.isServer){
overallScore = 10;
}
This will only change the overallScore on the server side to 10, right? Whereas:
if(Network.isServer){
networkView.RPC("ChangeScore", RPCMode.All, 10);
}
@RPC
function ChangeScore(new : int){
overallScore = new;
}
This will be called only by the server, but apply the changes to the clients too. Right?
Do I have to do this all the time? What if I just want to share all the variables on this script, so its the 'centre' of the game?
I think your main RPC schema is correct. As above, ChangeScore() will change the overallScore value automatically on all clients receiving RPC. But this is about already connected clients. I think you have just to use RPC$$anonymous$$ode.AllBuffered ins$$anonymous$$d of RPC$$anonymous$$ode.All, as overallScore to be ready also for not yet instantiated scripts.
You need to send the information threw the network, so yes.
Just for information, in the method ChangeScore you call the integer "new", you should take another name for the variable, since "new" means something else (colored in blue).
Okay, does making a variable private, or public make a difference?
Not for RPC calls because RPC calls are made "locally".
Do I need a NetworkView on the same gameObject the script is on, and put the Script in the Observer? Thanks
Your answer
Follow this Question
Related Questions
How can I check if a particular IP is a server? 1 Answer
Proxy Server 0 Answers
Port not opening? 3 Answers
Server and Network no communication 1 Answer
Syncronization issues between server and client, using the OnStartServer() function, 0 Answers