• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by X_Magma · Jan 20, 2020 at 02:37 AM · multiplayerphotonmultiplayer networking

[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.

Comment
OG1337OG

People who like this

1 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image

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.

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image X_Magma · Jan 20, 2020 at 03:24 AM 0
Share

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 GameManager object, I would need to call it from 'master' client?

Thanks again.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

204 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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 3 Answers

How to add a component to a prefab 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges