- Home /
[Photon] How would I give each player a different variable?
So I'm new to Photon, and multiplayer in general so I'm sorry if this is a dumb question. I'm trying to make a game where each player has a different role but I'm not sure how to go about doing that.
For Example, when the game starts, I want to give a random player a roleId of '0', another one a roleId of '1', and all others a roleId of '2'.
I tried having a look at RPCs but I'm not quite sure how they work and I don't even know if it's what I need.
Thanks.
Answer by GetLitGames · Jan 20, 2020 at 03:08 AM
The Photon tutorial (link below) goes through all the steps, starting with opening a lobby and connecting the players, then instantiating the player's controllers. It uses GameManager to connect up the players via lobby etc but my instructions below should work - just adding another method on it. This assumes the GameManager doesn't get destroyed when you load the scene for the players to be in.
You should go through a tutorial what will explain the basics first, but you need to add a PhotonView to a game object like maybe call it GameManager or RoleManager and then add a new script called GameManager as well as the PhotonView component to an empty gameobject. Naturally you need to do this to a gameobject that will be around once the game has loaded - in the game scene. In your GameManager script you declare a method as [PunRPC] and only in your host code do you call the:
PhotonView photonView = PhotonView.Get(this); photonView.RPC("ChatMessage", RpcTarget.All, "jup", "and jup.");
You should understand that code in the GameManager runs on all machines, whether its a host or client. You want the host to call those two lines of code. The parameters don't have to be strings, you can set them to integers or just have one integer parameter. The point is that the clients will have their ChatMessage function called with the parameters you sent.
So you might call it SetRole instead of ChatMessage.
https://doc.photonengine.com/en-us/pun/v2/demos-and-tutorials/pun-basics-tutorial/intro
This link shows the most basic RPC described above: https://doc.photonengine.com/en-us/pun/v2/gameplay/rpcsandraiseevent#shortcuts_for_rpc_names
There isn't much to it, you define the method and decide what to send to the method as parameters. You add code to use the parameters once it is called. The little trick is understanding that all scripts will be running on all copies of all gameobjects on all clients. When you call photonView.RPC("ChatMessage", RpcTarget.All) every client individually will have their ChatMessage function run with the parameters you send.
Unfortunately your case is more complicated, you need a way to send messages to individual clients. Hopefully someone can answer that but you should understand the basics first.
Thank's for pointing me in the right direction. Just to clarify, is the 'host' the same as the 'master'? I'm thinking of just having the RPC method require two parameters (int clientId, int roleId) and sending that method to all roles. Then the clients will check if their photon view ID is the same as the first parameter, if so then set their role to the second parameter. So if I was to do that on the Game$$anonymous$$anager object, I would need to call it from 'master' client?
Thanks again.
Your answer
Follow this Question
Related Questions
Photon PUN 2 RTS one time information sharing 0 Answers
PUN - Choose random Hero for player 0 Answers
VR Mixed Reality Cilbration issues over the Network? 0 Answers
Photon IK sync 2 Answers