- Home /
Photon Int sync not working
I feel very stupid for asking this but I've turned despite. So im trying to get a int to show how many people escaped something. Whenever a player collides with the escape place it shows up you escaped and "int + out of 4 people escaped. But it won't sync the int. By the way im using photon Pun 2. Here is the script.`public class c : MonoBehaviour, Photon.Pun.IPunObservable { public Text text; private int escaped; public PhotonView photonView;
void Start () {
}
public void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "bobbobbob(Clone)")
{
escaped++;
}
}
// Update is called once per frame
void Update()
{
text.text = escaped + " out of 4 escaped alive";
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
stream.SendNext(escaped);
}
if (stream.IsReading)
{
escaped = (int)stream.ReceiveNext();
}
}`
Answer by Captain_Pineapple · May 15, 2019 at 02:08 PM
Yo,
this is the same problem as in your last 2 questions... Please learn 2 things:
1: Do not sync things that do not change every frame by using the stream! Use an RPC for that. This will also solve your problem on it's own when you change to using an rpc.
2: When you use the stream. There will be one "true" variable that everyone else will follow... When A owns the "escape" thing then only the collision on player A's instance will do anything. For some player B,C,... they will change the variable and then recieve the synchronized value form player A who owns the object and overwrite the value.
Think about ownership of object instances. Let me know if something was unclear, you basically had this same problem the third time now. Look into this question you posted. Its exaclty the same issue.
It worked thanks. But for some reason the other players can't see the master client.
Can you elaborate what you mean by "can't see"? They do not get the "escape" of the master client?
What im trying to say is that when they spawn they literally can't see the master client player on their screen they can just go right through where the master client player is.
Did you make sure that the master clients player is created by PhotonNetwork.Instantiate and did you make sure that you are connected to a room before you do that?
Yes everything is working the master client can see all the other players but they can't see him here is my spawn script for when they spawn.
private void OnLevelWasLoaded(int level)
{
if (level == 1)
{
PhotonNetwork.Instantiate(Path.Combine("player", "bobbobbob"), new Vector3(0, 0, 0), Quaternion.identity, 0);
}
}
2 things: OnLevelWasLoaded is deprecated and this does not mean that you are connected to a room. Only instatiate netowrked objects if PhotonNetwork.InRoom is true. This way you can make sure that photon will recognize the instantiation of your object.
The LevelLoad can be completed before the room was joined. There is also a callback named OnJoinedRoom
that could be used for you here.
So every player could instatiate his own player char when the room was joined.
You can check out the documentation on callbacks here. (assu$$anonymous$$g you use photon2)
A few things. In my game. There is a lobby scene and a game scene. The problem with OnJoinedRoom is that they join the room in the lobby scene. In the lobby scene, when it reaches four players it starts a countdown which transfers all the players to the game scene. I tried photonNetwork.InRoom in my void start but I still got the same results.
Answer by cooltrophygameing · May 16, 2019 at 08:19 AM
Hey,
First I just want to say sorry for asking a lot of questions. Second, before I asked this question, I used a RPC but it didn't work here is the script where I used the RPC.
I double checked everything but it still said 0 thats why I went back to using the stream.
Did you try to debug your rpc code? i guess not.
If you add a Debug.Log in your ecapefun()
you will realize that it is never called.
First: Add the [PunRPC]
description to the ecapefun()
Secondly and most important: Do not blindly copy code without thinking about it. Think about what the parameter false
in your rpc call does. It had some "use" in the case of the door but here it's useless. If you look at the ecapefun
there is no parameter to pass.
So either add a boolean parameter to the function if you need that or remove the parameter from the rpc call.