- Home /
Pass value over network without RPC-call?
This seems such a simple problem but somehow I don't get it:
I want to instantiate a gameobject in network and pass a value to the new created object that uses this value in it's Start() function and then delete it's networkview-component because it's not needed anymore. Network.Instantiate works fine, even the value gets passed before Start() on the client, but not for the other players. Note that in my case I can't use RPC-calls on the script that the object instantiates.
So I put the script in editor as observed for the networkview component, but it doesn't helped. I don't even get something with...
function OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo) {
if (stream.isReading) {
print("onserialize: read");
}
if (stream.isWriting) {
print("onserialize: writing");
}
print("onserialize");
}
...attched in the script that is on the instantiated object. Nothing is printed when I instantiate the objects.
How does OnSerializeNetworkView work? Probably I use it wrong, but the docs are just so superficial sometimes :( Even if I don't delete the networkview-component, it throws nothing!
Another strange thing for me is that I earlier forgot to attach the networkview component to the object I want to instantiate but it instantiated correctly (position and rotation) for the other players oO why? Is it just for synch when objects live long?
Should I make a RPC-call at Start() in the script from the instantiated object to short synch the value from the client to all the other players or is there a better way for this problem?
Right well OnSerializeNetworkView is for sync after creation - as your NetworkView is gone not much will be happening.
The answer has to be RPC - there isn't another way. Build you own version of Network.Instantiate perhaps (that's what I do) with an scene component acting as the manager - then you can pass whatever you like to it.
Yeah, I solved the problem with RPC now.
$$anonymous$$issile-launcherscript from one of the players:
public class $$anonymous$$issileLauncher extends Cast{
var missile : $$anonymous$$issile;
var offsetVector : Vector3 = Vector3(0, 0.2, 1.2);
function UseCast() {
var new$$anonymous$$issile : $$anonymous$$issile = Network.Instantiate(missile, transform.position + transform.TransformDirection(offsetVector), transform.rotation, 0);
new$$anonymous$$issile.networkView.RPC("SetProjectileForce", RPC$$anonymous$$ode.AllBuffered, new$$anonymous$$issile.projectileForce + rigidbody.velocity.magnitude);
}
}
And here's the missiles script:
var projectileForce : float = 30;
var explosion : Transform;
var effectForce : float = 100;
var explodeAfterSek : float = 3;
WaitAndExplode();
function WaitAndExplode() {
yield WaitForSeconds(explodeAfterSek);
Destroy$$anonymous$$e();
}
@RPC
function SetProjectileForce (realProjectileForce : float) {
rigidbody.velocity = transform.forward * realProjectileForce;
}
function OnTriggerEnter(other : Collider) {
if (other.rigidbody) {
other.rigidbody.AddForceAtPosition(transform.forward * effectForce + Vector3(0, effectForce, 0), transform.position, Force$$anonymous$$ode.Impulse);
Destroy$$anonymous$$e();
}
}
function Destroy$$anonymous$$e() {
Instantiate(explosion, transform.position, transform.rotation);
Destroy(gameObject);
}
A networkview-component must be attched to the missile, but State Synch can be "Off" and observed "None", so I wouldn't have too much traffic.