Understanding ownership and photonview

Options
Hi,

I have studied a lot of documentation and forum posts and still do not seem to be able to either understand the ownership and Photonview aspect, or get it to work properly.

My issue is that, even when I have created an object with PhotonNetwork.Instatiate(), the photonview.ismine() method is only ever true for the client that created the room. Is this meant to be the case, or am I missing something here? And if it is the case, why?

Any insights would be greatly appreciated.

Best Answer

  • jeanfabre
    jeanfabre mod
    Answer ✓
    Options
    uhmmm chess games development :)

    Hope you are having fun developing it!

    I am very confused with your code actually. this is likely creating a race condition.

    instead, I would suggest two things:

    1: to verify the isMine behaviour, create a simple prefab with a simple UI that shows the isMine value itself. It's very important to isolate this totally from any other logic to avoid stacking issues

    2: when changing turns, why calling "changeturn" which implies logically that it's your opponent turn, and send a boolean flag, I think this is adding confusion to the method, and I don't think it's needed actually. Don't send a boolean, and rely on the logical implication of that call that if it's not your turn and you receive it, it's now your turn.

    Does that make sense?

    Bye,

    Jean

Answers

  • jeanfabre
    Options
    Hi,

    you are getting mixed up with several concepts. Let me recap few things, maybe that will help:

    PhotonView.isMine is true for every player in front of their respective computer. When PlayerB on computerB join a room and instantiate its player, InstanceB is mine will be true on computerB, but false on computerA, and InstanceA will be true on computerA, but false on computerB.

    ismine reflects who instantiated that prefab instance, it's unrelated to the room creator.

    So it's likely your testing protocole that is faulty. how do you verify this value on each computers?

    Bye,

    Jean
  • Slevin
    Options
    Thanks for your quick reply Jean.

    This is what I thought, however the behaviour of my script does not reflect this. I have a method to switch turns in a chess game:
    //
    [PunRPC]private void ChangeTurn(bool bSwitchTurn){
    		if(photonView.IsMine){
    			bIsMyTurn = bSwitchTurn;
    		}else{
    			photonView.RPC("ChangeTurn", PhotonTargets.Others, !bSwitchTurn);
    		}
    	}
    //
    Both objects calling this function are set up correctly and instantiated by the client when they join a room. Since I can see the objects appear/disappear when a player joins, I know that the objects are being instantiated properly in the network, but this method only works on the client who created the room. In order to get it to work, I had to change to the following:
    //
    [PunRPC]private void ChangeTurn(bool bSwitchTurn){
    		if(bIsMyTurn){
    			photonView.RPC("ChangeTurn", PhotonTargets.Others, !bSwitchTurn);
    			bIsMyTurn = bSwitchTurn;
    		}else{
    			bIsMyTurn = bSwitchTurn;
    		}
    	}
    //
    The same issue is reflected elsewhere in the project in the OnPhotonSerializeView() method. Do you know how this could be occurring?
  • jeanfabre
    jeanfabre mod
    Answer ✓
    Options
    uhmmm chess games development :)

    Hope you are having fun developing it!

    I am very confused with your code actually. this is likely creating a race condition.

    instead, I would suggest two things:

    1: to verify the isMine behaviour, create a simple prefab with a simple UI that shows the isMine value itself. It's very important to isolate this totally from any other logic to avoid stacking issues

    2: when changing turns, why calling "changeturn" which implies logically that it's your opponent turn, and send a boolean flag, I think this is adding confusion to the method, and I don't think it's needed actually. Don't send a boolean, and rely on the logical implication of that call that if it's not your turn and you receive it, it's now your turn.

    Does that make sense?

    Bye,

    Jean
  • Slevin
    Options
    I am! The Chess side is easy as luckily the client only wants multiplayer so I don't have to worry about implementing AI. En Passant is the only thing left to implement.

    Hmmm okay, the fact that this code is confusing is a good sign since it means that I'm misunderstanding something but I don't entirely understand what. I have just looked into race conditions and I think what you mean is it's possible that the method is "jamming" because of the order of the method being called?

    1. Okay, I will do a small test to wrap my head around this.
    2. Sure I think I understand what you mean. I have changed the code to reflect what I think you mean, is this correct?
    if(bIsMyTurn){
    		bIsMyTurn = false;
    		photonView.RPC("ChangeTurn", PhotonTargets.Others);
    	}else{
    		bIsMyTurn = true;
    	}
    Although I haven't done a test on the ismine() yet, it may be worth noting that while this works with the above if(bIsMyTurn), if I change it to if(photonView.IsMine), I get the same problem as before. I added a debug.log and when the client who creates the room calls this function it works, but when the next player calls it it doesn't. I'll do a test and see if I can isolate the problem.
  • Slevin
    Options
    Ahhh, I understand now. Because I am using photonView.RPC(), I think that it is keeping the original photonView even when calling ChangeTurn() for the other player. So if I wanted to do it this way, I think I could use PhotonNetwork.RPC() instead and it would work. Thanks so much for your help, Jeanfabre!
  • jeanfabre
    Options
    Hi,

    Good. I am glad you sorted it out!

    Bye,

    Jean