- Home /
Network n00b - how to refer to synced gameObjects in a multiplayer scene?
Hi everyone, Very new to network programming, so I have a bit of a high level question. What would be the best way to go about having one class created through Network.Instantiate() aware of which other gameObject spawned it?
Say I have a SpiderQueen which is running around hatching SpiderChildren. Children need to know things about their mother, eg. colour, name, and to also store a reference to her (say for a pathfinding destination) - and of course none of these are valid RPC parameters (only accepts int, float, char etc.).
The only way that really seems possible at the moment, is to assign each SpiderQueen some unique ID, sync this with all players by adding to OnSerializeNetworkView(), pass this to children once instantiated, and have them search the scene for SpiderQueens containing this ID - but this seems pretty inefficient.
eg.
class SpiderQueen: MonoBehaviour {
public int uniqueID = 1111;
public SpiderChild spiderChildRef;
public SpiderChild spawnChild() {
if (spiderChildRef) {
SpiderChild newChild = Network.Instantiate(spiderChildRef, blah blah blah) as SpiderChild;
newChild.setMotherID(uniqueID);
}
}
}
class SpiderChild: MonoBehaviour {
public int motherID = 0;
public void setMotherID(int a_id) {
if (networkView.isMine) {
networkView.RPC("RPC_setMotherID", RPCMode.AllBuffered, a_id);
}
}
[RPC]
void RPC_setMotherID(int a_id) {
motherID = a_id;
// now I know my mother's ID, I can search the scene for SpiderQueens with this uniqueID
}
}
Pretty basic, but if I take this approach, I still have to manually sync all properties of all SpiderQueens in the scene with eachother as well. Is that just the way it is, or am I not understanding something large here.
Thanks for any help!
Your answer