PhotonNetwork.Instantiate() vs RPC calls

Hello,

Everywhere I have researched states PhotonNetwork.Instantiate() should only be used for major player controlled items such as avatars, and that bullets and attacks, etc. should be made using RPC calls for efficiency.

I tested both ways, and for some reason the RPC method is much slower performance wise, and has lag and framerate impacts for high speed projectile creation, such as machine guns.

Below are the two pieces of code I tested for firing bullets.

RPC Method Call:

[PunRPC]
void FireBulletViaRPC(int ownerPVID, Vector3 bulletLoc, Quaternion bulletRotation, Vector3 ownerVelocity) {

            //Right way, inside an RPC call

            GameObject tmp = Instantiate (bulletPrefab, bulletLoc, bulletRotation);

            //We need to add our velocity to bullet so we dont fly past it at full speed.

            //Connect with bullet first to tel it about creator
            MultiplayerRPCBullet tmpBlt = tmp.GetComponent<MultiplayerRPCBullet> ();
            tmpBlt.ownerPVID = ownerPVID; //who's yur daddy
            tmpBlt.myOwnerVelocity = ownerVelocity; //start at his speed



 }

And I compare that to my original code:

void FireBulletViaInstantiate(int ownerPVID, Vector3 bulletLoc, Quaternion bulletRotation, Vector3 ownerVelocity) {


            GameObject tmp = PhotonNetwork.Instantiate (pathToBullet, bulletLoc, bulletRotation, 0);

            //We need to add our velocity to bullet so we dont fly past it at full speed.

            //Connect with bullet first to tel it about creator
            MultiplayerRPCBullet tmpBlt = tmp.GetComponent<MultiplayerRPCBullet> ();
            tmpBlt.ownerPVID = ownerPVID; //who's yur daddy
            tmpBlt.myOwnerVelocity = ownerVelocity; // start at his speed



}

For both methods they are simply called with mouse0 down event handler with a cooldown, and encapuslated with PV.ismine properly. Since that code is shared by both tests, I felt it is not relevant to share it.

There is significant performance increase of after effects, such as bullet impacts etc. showing up much more reliably using RPC method calls. With this method all projectiles fired are visible, never lost, and always produce their destruction effects, but collision with players is very unreliable.

However, using the network instatiate produces much better collision detection, at the cost of having more complications when deleting a network instantiate projectile with a PV component.

I have seen other people new to network programming state they used PhotonNetwork.Instantiate() within the RPC method. That makes no sense to me, as you would be broadcasting a messgae to all players, and they in turn would broadcast a room-wide instantiation, causing a cascade of more projectiles than necessary, is that incorrect?

I though the whole purpose of using RPC method, was each machine would just locally create their own projectiles, which have no PhotonView component and just operate locally.

If anyone can clarify what I am doing wrong, it would be appreciated, thanks.

If you are Instantiating at runtime without an ObjectPool there will be lag. Any instantiation at runtime will create lag and cause the garbage collector to trigger here and there without some sort of pooler.

Search for an object pooler in Unity and preload a decent amount of bullets for your game and it should speed up that part. The RPC call will still have some lag on the network a bit and bullets are probably best done with rays/timed or simulated but you should at least get a faster repeated call.

There is a PUN ObjectPool see here in the ‘Using The PrefabPool’ section: Pun 2 Instantiation | Photon Engine
Photon Unity Networking 2: DefaultPool Class Reference

Or make your own
Stop wasting memory: recycle your objects! (best one)

Uses these two files:

Or
https://github.com/prime31/RecyclerKit

More info on the problem, runtime instantiation

The Photon Asteroids sample will probably help on the shooting part: Pun 2 Asteroids Demo | Photon Engine

There is a big about solving synchronization issues by adding lag compensation.

As far as your second point about spawning on their own and controlling it, yes that is correct. When you spawn using an RPC it will create on all players connected screens but not be networked any longer, it will have to match speed/direction/time etc and if it hits something that is a PhotonView the collision will also have to be RPC’d to all if it hits or when the player health runs out, major events. Ultimately you want the RPC to only go when they are in the same area or space otherwise it is not necessary. Fast real-time games use a server simulation or rays/time to make it faster but for small amounts of players RPCs are ok.

1 Like