- Home /
Photon network problem
Hey I've been working on a game which two players simultaneously play and a board hanging in the scene shows the player1 score and player2 score.
the game mechanic and coding works fine, whenever player1 scores he gains points and it shows on the board, same goes to player2 but the problem is the points for player1 change in his scene but not in player2's scene.
which means if I'm player 1 I can see my score change but player's 2 score will stay at zero the whole game while in his scene it's changing.
I've been using Photon for the networking in Unity.
The board is a gameobject which has a script that is called by a trigger of each player but I want the result to be the same in both players' scenes.
any help ?!
Answer by Firedan1176 · Jul 13, 2014 at 01:15 AM
To expand from taylank's answer, you do need to use RPC. On the object that the scoreboard script is on, place a Photon View component. Now if the scoreboard is already in the game and isn't instantiated when the game runs, you don't have to put anything in the observe blank. Now, within the scoreboard script, you're going to need to send an RPC so that the other player can receive it. This is how it works:
The scoreboard is updated or whatever and you need to send the new scores.
You use the RPC function to basically send a variable over the network to a function on the other person's computer.
Their scoreboard is updated.
In this example, I will be sending the score and the username to all other players (well just 2 in your example; you and the other player). To do this, I will grab the PhotonView on my 'scoreboard' gameobject (or whatever object has the scoreboard script on it), and call an RPC within that PhotonView, and include the username and the score.
GetComponent<PhotonView>().RPC ("UpdateScores", myUsername, myCurrentSore);
Now what this will do is call the function "UpdateScores" on every player's scoreboard script and feed in the username and the score of that player. Only problem is "UpdateScores" doesn't exist as a function (You're going to add it now).
Now, since both you and the other player need to synchronize, you'll need to make a function (void in C#) that will receive that incoming information. You'll also need to include @RPC (for JavaScript) before you declare the function or [RPC] for C#. My example will be using C#.
[RPC]
void UpdateScores(string username, float score) {
//Keep in mind that it doesn't matter that 'username' and 'score' above are the same as when you send it; they just need to both be the same Type of variable, such as string and float.
//You now have the username and the score on other clients. Do your 'update' stuff here.
}
Keep in mind since you're calling all functions on all computers, you are also calling it on your computer. Therefore, your game will also be updated. It doesn't really matter as yours will be updated anyways.
If you need more understanding, you can comment or look at the documentation for Photon Unity Networking (click here). I hope I answered your question.
Answer by taylank · Jul 13, 2014 at 12:46 AM
You should probably use a RPC function to change the players' scores, so that the change is reflected in each view. I believe there are examples of RPC use in Photon's Marco Polo tutorial:
http://doc.exitgames.com/en/pun/current/tutorials/tutorial-marco-polo
Check out the section titled Shout & Respond.
Your answer
Follow this Question
Related Questions
Matchmaking multiplayer help 0 Answers
Photon wont sync for the masterclient. 1 Answer
Unity3D Photon movement synchronization 0 Answers
Networking Sync SetActive Not Working 0 Answers
remove spawned object from my networkView when timeScale = 0 0 Answers