Devlog #07 - Multiplayer Battle Mode Implemented


Introduction:

As per the next plan, we'd have to convert the code made for single-player to work in multiplayer mode.

This could be easily done by making some minor changes.

PhotonView component:

The first step of this is to add photon view and photon transform view to all the components that are required to sync across the server the game is played

Photon View and Photon Transform View Components
 

And then we'd have to move all the prefabs that are instantiated to a folder called "Resources", hence photon can access it easily.

Resources Folder
 

Converting the Scripts:

The conversion is done by changing the following:

Firstly, check if you are accessing only your own script. This can be done by using

if (!PV.IsMine) return;
if (!isAlive) return;

is alive is a bool, and is kept only for the reason to not trigger these script functions after the player is dead

Instantiate -> PhotonNetwork.Instantiate

Example:  

Projectile newProjectile = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", projectilePrefab.name), projectileStartPoint.position, projectileStartPoint.rotation).GetComponent<Projectile>();

Destroy -> PhotonNetwork.Destroy

Example:

if (PV != null && PV.IsMine) {
    PhotonNetwork.Destroy(gameObject);
}

We are checking if the function that triggers the destroy is ours because the photon network can only destroy a local player's game object, or the destroy should be done by the master client.

And whatever functions that the other client could make our client do, should be called using an RPC (Remote Procedural Call).

Defining an RPC function is done by - 

[PunRPC]
void DestroySelf() {
    if (PV != null && PV.IsMine) {
        PhotonNetwork.Destroy(gameObject);
    }
}

Function call for the respective function - 

if (PV.IsMine) {
    Debug.Log($"[{ActorId}] Player Died"); 
    PV.RPC(nameof(DestroySelf), RpcTarget.All);
}

By doing the above steps to the other function, I have successfully converted a single-player to multiplayer

Final Output:

Fixing the gun model floating by:

Since we instantiated a gun on a child, and photon networks only apply the destroy on the root model, we were required to make the gun model on the model rather than as a child that is instantiated separately. This was also completely unnecessary to instantiate it separately, since we are going to use a single gun throughout the game.

Since we removed the separate instantiation, the bug where the gun was floating after a player's death was fixed.

References:

[1] Photon PUN. Instantiation: https://doc.photonengine.com/pun/current/gameplay/instantiation

[2] Photon PUN.RPCs and RaiseEvent: https://doc.photonengine.com/pun/current/gameplay/rpcsandraiseevent

Leave a comment

Log in with itch.io to leave a comment.