RPC and OnGUI issue

Options
Mikalo
edited August 2014 in DotNet
This isn't my exact code, as there is a lot more to the actual code. I'm still new to PUN and trying to make a multiplayer unity game, so forgive me if this is a simple solution.

I have code that instantiates an object for each player as they join, now the players get to setup their chars and then once they are at the end, they can challenge and be challenged. However, there seems to be an issue.
    void OnGUI()
    {
        if (photonView.isMine)
        {
            if (pageNumber == 0)
            {
                if (GUI.Button(new Rect(25, 25, 100, 20), "Next"))
                {
                    pageNumber++;
                }
            }
            if (pageNumber == 1)
            {
                if (GUI.Button(new Rect(25, 25, 100, 20), "Button to select opponent"))
                {
                    playerPhotonView.RPC("ChallengePlayer", PhotonTargets.Others);
                }
            }
            if (pageNumber == 2)
            {
                if (GUI.Button(new Rect(25, 25, 100, 20), "Decline challenge"))
                {
                    pageNumber--;
                }
            }
        }
    }

Basically each page is setup to let you pick your name, char, etc and then at the end, it would have code that lets you click a button to select who you want to battle.
    [RPC]
    void ChallengePlayer()
    {
        Debug.Log(pageNumber + " challenged page number");
        //script will include information about challenger
        if(pageNumber == 1)
            pageNumber++;
    }

The rpc checks if you are on the challenge page and then issues a challenge by moving to the next page. However, the problem I'm having is when I make the RPC call, the log shows that pagenumber = 0 and not 1 like I would have thought.

My next move was to track pageNumber in OnPhotonSerializeView, which worked, pagenumber than equaled 1 when I called the RPC and pagenumber is than updated to 2, however, the gui doesn't update to the next page like I thought it would have.

Again, still new at this. My basic desire is to let players setup for the game, than they enter the challenge screen, if they are challenged, they can accept and be moved to a room to play or decline and be sent back to the challenge screen.

Thanks for any help.

Comments

  • vadim
    Options
    Looks like 2nd player is still on page 0 and can't accept the challenge when RPC fired. Wait until 2nd players gets to proper ui state.
    Or set pageNumber = 2 without condition if visiting page 1 is not mandatory.
    Probably you want state when 1st client pageNumber = 1 and 2nd pageNumber = 2. In that case OnPhotonSerializeView synchronization of pageNumber is not an option for you since it makes variables equal on all clients.
  • Yes, I understand the variable is reading 0 for some reason, but maybe I'm not understanding what you are saying, unless there is a special reason the pageNumber variable is not holding its value.

    I'll explain what I've tried.

    Basically I use the build & run in unity to create my first player. Then I just use play within the unity editor for my 2nd player.

    At this point, both players are on pageNumber = 0;
    I click the button to bring up the next screen on both players (pageNumber = 1).
    On player one (the one outside unity) I click the challenge button which calls the RPC on player 2 and runs the debug.log on the unity console, which shows pageNumber = 0.

    Note, I have also checked the value of pageNumber before being called and it shows pageNumber = 1.

    Also note, I removed the check "if (pageNumber == 1)" and just let the RPC set pageNumber = 2, but the Gui never switches, although the pageNumber value does update (first click shows pageNumber = 0, second click shows pageNumber = 2)

    I hope that all makes sense. As I said, I'm new to the networking scene and while I've got some unity experience under my belt, there is still much I don't know, so I wasn't sure if changing values in Gui and RPC was different. It almost feels like there are two versions running. The version that I interacted with and another version that never moved through the pages.
  • vadim
    Options
    Gui never switches, although the pageNumber value does update
    I think you should resolve this issue first of all. Your code shows clearly that gui should switch. Trace all pageNumber changes and log it on every OnGUI() call. It's not RPC or Photon related.
    Btw, use 'switch(pageNumber)' instead of series of if's in OnGUI to avoid several if's blocks execution at one call.