- Home /
When using Network.instantiate, how to set an additional setting for all clients (such as a script variable) ?
Hi,
When using Network.instantiate, how would one define an additional setting?
For instance, if I were to spawn a gun that has 4 bullets inside, I would call:
GameObject tempGun = ((Transform)Network.Instantiate(Gun, _targetSpawnLocation, Quaternion.identity, 0)).gameObject;
On the local code I write: (The gun has a script attached called "GunInfo")
tempGun.GetComponent<GunInfo>().Bullets = 4;
However, not all clients know how many bullets the gun has. How do I synchronize this with them?
An extra note: It would be possible using OnSerializeNetworkView(), but this runs every Update() and is completely unnecessary. It only needs to run once initially so all clients know how many bullets the gun has.
I suppose it's possible using an RPC, but how do I identify the object on all clients? How do I get the network-instantiated gun object on all clients code-wise?
Thanks, Nick
Answer by Bunny83 · Feb 08, 2011 at 03:40 PM
I guess the object you instantiate have a NetworkView attached? If so it's quite easy. RPC calls are send to the objects with a NetworkView with the same ViewID.
public class GunInfo : MonoBehaviour { private int m_Bullets = 0; public int Bullets { get { return m_Bullets; } set { m_Bullets = value; if (networkView.isMine) networkView.RPC("RPC_SetBullets",RPCMode.Others,m_Bullets); } }
[RPC]
void RPC_SetBullets(int aBullets)
{
m_Bullets = aBullets;
}
}
Thanks to the property you don't have to worry about syncing.
If you don't have a NetworkView on your object you have to implement some kind of object management that keeps a list of all objects and using own IDs to find the corresponding objects.
Do you use an authoritative server setup? Or a "normal mixed" setup where clients can create objects? The only important thing regarding my solution is to keep in mind that only the player that created the object can sync the bullet count.
Bunny's got the right solution here. $$anonymous$$y question is, why do clients care how many bullets other clients have?
Thanks Bunny, I'll try it out!
Derek, the gun was just an simple example I thought about, so I wouldn't have to explain my own complex situation ;)
What is the networkView.is$$anonymous$$ine for? Isn't the networkView always a component of the current gameobject it is attached to?
is$$anonymous$$ine is just a easier way to check the owner (creator) of that NetworkViewID. It's the same as (networkView.viewID.is$$anonymous$$ine) or (networkView.viewID.owner == Network.player)
What bunny said. The owner part is not referring to the object that owns it, but rather the network player that owns it (if you're using network.instantiate, the network player that called network.instantiate).
Your answer
Follow this Question
Related Questions
How can I spawn a GameObject on all players in my multiplayer game? 0 Answers
Network.Instantiate and the Trusted Client problem 0 Answers
Network.instantiate for specified clients? 1 Answer
Network.Instantiate fired from client AND server 1 Answer
Network.Instantiate only Instantiates on one client. 0 Answers