- Home /
Photon Unity - Only receiving one value.
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
if(playerNumber != 1)
{
stream.SendNext(Input.GetAxisRaw("Vertical"));
}
if(playerNumber == 1)
{
GameObject[] listOfPlayers = GameObject.FindGameObjectsWithTag("Player");
foreach(GameObject player in listOfPlayers)
{
stream.SendNext((Vector2)player.transform.position);
}
}
}
else
{
vertAxis = (float)stream.ReceiveNext();
if(playerNumber != 1)
{
GameObject[] listOfPlayers = GameObject.FindGameObjectsWithTag("Player");
if(listOfPlayers.Length != 0)
{
foreach(GameObject player in listOfPlayers)
{
player.transform.position = (Vector2)stream.ReceiveNext();
}
}
}
}
}
If the code is like this then the values for player.transform.position won't be received. But if I remove:
vertAxis = (float)stream.ReceiveNext();
Then the values for player.transform.position will be received.
Can anyone explain what's going on or come up with a fix? Thanks.
Do you have a PhotonView component attached to every player?
Thanks for the comment!
Yes I definitely do have a PhotonViem on every player. If I didn't I don't think I would be able to send even one value.
In general, using for loops in OnPhotonSerializeView is not a good idea, because you can create functionality via networking so that you don't have to send data in a n^2 format. This will optimize your performance results greatly.
Answer by fuego_see_money · Jul 02, 2015 at 02:28 AM
Okay awesome. I don't know what script that you posted is attached to, but what I would do is, attach this script to every single player:
public class NetworkCharacter : Photon.MonoBehaviour
{
Vector3 realPosition;
void Update ()
{
if (photonView.isMine)
{
//do nothing
}
else
{
//lerping position
transform.position = Vector3.Lerp (transform.position, realPosition, .15f);
}
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(transform.position);
}
else
{
realPosition = (Vector3)stream.ReceiveNext ();
}
}
}
If you attach this to every player, than what this will do is have each player keep track of each other's positions. Dont use your other script anymore.
Let me know if you have questions.
-Will
Answer by UFO_505 · Jul 08, 2015 at 09:01 AM
Thanks for your help, Will.
I don't quite see the problem with for statements because from my testing I haven't found them causing any issues.
Anyway, thank you for the code you provided. However it doesn't quite do what I want because I want to send all players input to one 'host' player to create a kind of authoritative server.
I have finally managed to find a solution though:
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
if(photonView.isMine && playerNumber != 1)
{
stream.SendNext(Input.GetAxisRaw("Vertical"));
}
if(photonView.isMine && playerNumber == 1)
{
GameObject[] listOfPlayers = GameObject.FindGameObjectsWithTag("Player");
foreach(GameObject player in listOfPlayers)
{
stream.SendNext(player.transform.position);
}
}
}
else
{
GameObject[] listOfPlayers = GameObject.FindGameObjectsWithTag("Player");
for(int i=0; i < listOfPlayers.Length; i++)
{
if(listOfPlayers[i].GetPhotonView().isMine && listOfPlayers[i].GetComponent<Player>().playerNumber == 1)
{
vertAxis = (float)stream.ReceiveNext();
}
}
for(int i=0; i < listOfPlayers.Length; i++)
{
if(listOfPlayers[i].GetPhotonView().isMine && listOfPlayers[i].GetComponent<Player>().playerNumber != 1)//listOfPlayers.Length != 0)
{
foreach(GameObject player in listOfPlayers)
{
player.transform.position = (Vector3)stream.ReceiveNext();
}
}
}
}
}
Answer by tobiass · Jul 10, 2015 at 09:31 AM
I think your code clashes with the idea that each individual PhotonView is controlled by it's owner. This makes sure each player can affect it's own objects and then send the new position/state to the others.
Your code looks like you run the script on all clients but only have one client send updates for every player object. That can't work. How do you get the other client's input?
Take a look at the Marco Polo Tutorial. It shows how to send and receive values like you try to.
If that doesn't apply to your case, you need to post what you want to achieve.
basically what im trying to do is create a kind of authoritative server without actually having the server execute any code (photon servers can't).
one player acts as a kind of server
every player sends their input to that one player and then that one player runs the game and tells all the other player's what is happening
i have managed to get this to work to an extent - the players can all move and rotate their characters and they're all in sync because one player is telling everyone else what to do
im about to start work on the shooting mechanic which will probably work in a similar way.
do you see any serious problem with what im trying to do? i realise that there will be about double the lag of what i would otherwise have. however the game is quite simple so i am hoping that each client will be able to send updates more often than is usual
Yes, there is a huge problem with what you are doing. The problem is, Photon is not meant for doing that. At all. Photon is made so that it works well with unity, including object oriented framework.
This is why you should do what I posted earlier, because that will have each object tell everyone where it is. Photon Unity Networking already has a server - this is how PhotonNetwork.Instantiate
is used. It talks to the main server and tells everyone to spawn something on their local computers.
You should see this video - it explains how Photon works. It will help you greatly. Watch the whole thing, you wont regret it.
I understand what you are saying and I realise that what I am doing is not normal. However, the game is working well so far and it has a great advantage over the usual model that you are suggesting because everybody's game is always completely in sync.
You should listen to the people you asked on this forum - both of whom told you not to do it the way you are doing it. One of whom (not me, Tobias) is a lead engineer for Photon. If you continue to program your game in this way, which is fine by me, just note two things:
1) You are not using Photon the way it's meant to be used, when its relatively easy to switch over to how it should be used now. If you wait until too long, you might find that it's impossible to implement things into your game that you need.
2) If you have no intentions of listening to the people who answer your questions, why bother asking the question at all? I understand you think the way you are doing it will work out, but this forum is not just meant for individuals getting answers to their questions, but it's also a permanent reference for googlers across the world after the questions have been answered. I've used answers to past questions on this site countless times, as I'm sure you have too. This question will simply point other users who have similar problems to the wrong way of approaching it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613201436im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Photon Networking Spawn player 2 0 Answers
Sync game objects across network Photon C# 1 Answer
Photon synchronization 1 Answer
Unable to network remove object 0 Answers
Photon OnPhotonSerializeView - sending a custom struct - "not serializable" 0 Answers