Search Unity

Syncing buttons?

Discussion in 'Multiplayer' started by xsharkox, Oct 16, 2014.

  1. xsharkox

    xsharkox

    Joined:
    Aug 24, 2014
    Posts:
    10
    Hey, i've been trying to set up a network for a simple tictactoe game i've create as a small practice task. i've set up GUIbuttons and everytime a button is clicked, it sets the text in the box to X or O. i've added the photon plugin and i've been messing around with PUN for 2 days, reading up on documentation and looking at tutorials but i don't know how to sync GUI boxes or is this even possible? any kind of help would be most appreciated :)
    What i'm trying to do is create a two player TicTacToe game just incase anyone was wondering :p
    Thanks in advance!
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    You basically want to send "i pressed a button" to the others, whenever a button got clicked, right?
    Sending actions like that is best done by "RPC".
    Work through the Marco Polo Tutorial to learn how to use those:
    http://doc.exitgames.com/en/pun/current/tutorials/tutorial-marco-polo

    Once you know how to send the click, you only need to figure out how to change the values in your GUI and logic.
    Good luck.
     
    xsharkox likes this.
  3. xsharkox

    xsharkox

    Joined:
    Aug 24, 2014
    Posts:
    10
    Hey, so i've ended up using OnPhotonSerializeView instead of using the RPC method although i did try it out and do find it easier to get this kind of job done. thought my employer does not want me to do it using the RPC method. At the moment, i've got a button which both players can click to update the text and position of a GUIText, for the player who hosts the room, everything works fine such as sending data using
    stream.SendNext(onScreenTextString.text);
    the client receives the data and their screen gets updated but when the client tries to send data, nothing is sent, only the clients screen is updated. i'm a student in learning so any help would be massively appreciated! Thanks
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    OnPhotonSerializeView is called on an Object with PhotonView. Only the owner of that PhotonView can update the others, which is why only one of your players can affect the button.
    As said, use RPCs. I can't really help in-detail and teach you more than giving you the Tutorial. Sorry.
     
  5. xsharkox

    xsharkox

    Joined:
    Aug 24, 2014
    Posts:
    10
    is there a way to give all players ownership of that photonview or is that just not possible? will this work if i instantiated a new object creating a button for each player and having that button updated the single GUI Text on the scene? Sorry for asking so many questions and Thanks for all the help.

    Theres another thing i forgot to say, the host is affecting the GUI Text for both players but the client(2nd player) is affecting the GUI Text but only for himself, is this what you meant by owner of that photonview? i've got debugs in my code and all the client is doing is receiving data but not sending any out and i have no idea why. Sorry again
     
    Last edited: Oct 20, 2014
  6. xsharkox

    xsharkox

    Joined:
    Aug 24, 2014
    Posts:
    10
    Also using RPC's, would I be able to send an announcement across the server to all players for e.g. so like arguments.
     
    Last edited: Oct 20, 2014
  7. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    It is not possible to give everyone control of a PhotonView. In PUN 1.28.x it's not even possible to change who is in control of a PhotonView.
    Please experiment with RPCs or Custom Properties. OnPhotonSerializeView is simply not the best choice for your problem.

    RPCs are limited to the room the players are in. If you need game wide announcements, you could do this with Photon server or you simply use WWW to fetch a message from a web server (which you have to create and update with messages).
     
  8. xsharkox

    xsharkox

    Joined:
    Aug 24, 2014
    Posts:
    10
    Thanks for the info tobiass, i have another question sorry. what about if you want to update/synchronise things like seeing the other players for e.g. such as if you're playing a multiplayer puzzle game such as Tetris or checkers etc. I appreciate all the help and info you've given me thanks!
     
  9. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    Sorry for the late reply. Wasn't in the office since Friday.
    Seeing the other player? What do you mean?
    Use RPCs if you want to sync something infrequently. Use OnSerializePhotonView if you have to sync something more frequently. You can pause the updates sent by that easily by not putting anything in the stream if needed.
     
  10. xsharkox

    xsharkox

    Joined:
    Aug 24, 2014
    Posts:
    10
    Like the screens for e.g. so like if you're playing multiplayer tetris, you should be able to see the other players boards and see their progress as well as your own, which one would you use to achieve this? Thanks.
     
  11. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    If you use OnPhotonSerializeView on an empty GameObject per player, you could send updates of the current brick 10 times a second easily. That should be enough to follow a player in a Tetris like game.
     
  12. xsharkox

    xsharkox

    Joined:
    Aug 24, 2014
    Posts:
    10
    If you could, would you be able to take a quick look and tell me why this isn't working, Thanks in advance! Sorry for the repetitive questions.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GTHandler : MonoBehaviour {
    5.  
    6.     private int num = 0;
    7.     private GameObject onScreenText;
    8.     private GUIText onScreenTextString;
    9.  
    10.     void Start(){
    11.         onScreenText = GameObject.Find ("GUI Text");
    12.         onScreenTextString = onScreenText.GetComponent<GUIText> ();
    13.     }
    14.  
    15.     void Update(){
    16.  
    17.     }
    18.  
    19.     void OnGUI(){
    20.         if (GUI.Button (new Rect (300, 300, 100, 50), "button++")) {          
    21.             Vector3 pos = onScreenText.transform.position;
    22.             onScreenText.transform.position = new Vector3(pos.x + 0.01f, pos.y, pos.z);
    23.             num++;
    24.             onScreenTextString.text = num + "";
    25.         }//end if  
    26.     }//end OnGUI
    27.  
    28.     public void OnPhotonSerializeView (PhotonStream stream, PhotonMessageInfo info){
    29.         Debug.Log ("OnPhotonSerializeView called");
    30.         if (stream.isWriting) {
    31.             Debug.Log ("sending data");
    32.             stream.SendNext(onScreenTextString.text);
    33.             stream.SendNext(onScreenText.transform.position);
    34.         } else {
    35.             Debug.Log ("receiving data");
    36.             onScreenTextString.text = (string) stream.ReceiveNext();
    37.             onScreenText.transform.position = (Vector3) stream.ReceiveNext();
    38.         }//end if
    39.     }//end OnPhotonSerializeView
    40. }
    41.  

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class network : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.         PhotonNetwork.ConnectUsingSettings ("v1");
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.         if (Input.GetKey("escape")) {
    14.             Application.Quit ();
    15.         }
    16.     }
    17.    
    18.     void OnJoinedLobby(){
    19.         PhotonNetwork.JoinRandomRoom ();
    20.     }
    21.  
    22.     void OnPhotonRandomJoinFailed(){
    23.         Debug.Log ("FAILED TO JOIN RANDOM ROOM");
    24.         PhotonNetwork.CreateRoom ("Test");
    25.     }
    26.  
    27.     void OnCreatedRoom(){
    28.         Debug.Log (PhotonNetwork.room +" "+"CREATED");
    29.     }
    30.  
    31.     void OnJoinedRoom(){
    32.         Debug.Log (PhotonNetwork.room +" "+"JOINED");
    33.     }
    34.    
    35.  
    36. }
    37.  
    These are the two classes i have. Only the host sends data but the client does not. im sure you've already answered this but i just can't get my head around it. Sorry.
     
  13. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    I assume you have a PhotonView on some object in the scene?
    Then that object is owned by the Master Client (whoever that is). Only one user can send (!) updates for some PhotonView. If all would send updates, how would you know which one is correct?
    Using OnPhotonSerializeView forces you to accept that one user is the owner of the object. RPCs don't.
    Maybe use RPCs or Properties for the state. Check out Room.SetCustomProperties(), too.