PunRPC only on one client.

Hi there!


Repost because about only a few people even saw my last one.


Thanks in advance for reading. I am trying to implement a global chat system. The code is quite self-explanatory, but the main idea is just that every time a client enters some text and hits enter, that new text is sent to all the clients, which then add the new text to the previous text.

After countless documentation readings, forum scrawlings, and head bangings, I have no idea why it doesn't work. I am here to answer and assist as much as I can. Likely, it's something incredibly obvious that I missed.


Help please?

using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;


public class GlobalChatScript : MonoBehaviour
{
    [SerializeField] private InputField InputText;
    [SerializeField] private GameObject GlobalChatText;
    [SerializeField] private GameObject PauseMenu;
    [SerializeField] private GameObject NameMenu;
    [SerializeField] private PlayerMovement3 PlayerMover;


    private bool inputMenuActive = false;


    [SerializeField] private int maxTextLength;


    void Update()
    {
        if (Input.GetKeyDown(KeyCode.T) && !inputMenuActive && !PauseMenu.activeInHierarchy && !NameMenu.activeInHierarchy)
        {
            //Open chat input, lock player movement, free cursor, select chat input
            inputMenuActive = true;
            InputText.gameObject.SetActive(true);
            PlayerMover.freeMovement = false;
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible = true;
            InputText.Select();
        }


        if (inputMenuActive && this.GetComponent<PhotonView>().IsMine && Input.GetKeyDown(KeyCode.Return))
        {
            //Immediately close this conditional, update the chat
            inputMenuActive = false;
            UpdateMyChat();
        }
    }


    public void UpdateMyChat()
    {
        //Grab new text, wipe text
        string setText = this.GetComponent<PhotonView>().Owner.NickName + ": " + InputText.text;
        InputText.text = "";


        //Close chat input, resume player movement, lock mouse
        InputText.gameObject.SetActive(false);
        PlayerMover.freeMovement = true;
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    
        Debug.Log(this.GetComponent<PhotonView>().ViewID + " | SentMessage | " + setText);


        //Send PunRPC
        this.GetComponent<PhotonView>().RPC("SetGlobalChatText", RpcTarget.All, setText);
    }


    [PunRPC]
    public void SetGlobalChatText(string textSet)
    {
        //Put new text in front
        string set = textSet + "\n" + this.GlobalChatText.GetComponent<Text>().text;
        //Limit string size
        if (set.Length > maxTextLength)
            set = set.Substring(0, maxTextLength);


        //Update my chat
        Debug.Log(this.GetComponent<PhotonView>().ViewID + " | RecievedMessage | " + textSet);
        this.GlobalChatText.GetComponent<Text>().text = set;
    }
}


Answers

  • You use RpcTarget.All, so that's good. The RPC itself doesn't look as if it's only running on a specific client. Fine.

    The Debug.Log doesn't show up either?

  • Yeah, all that is correct. Thank you so much for responding, but I managed to fix it. Turns out I didn't understand a key part of the PunRPC method, which can be interpreted in two ways on the documentation. It isn't anything important, just an error on my end.


    Also, I don't know how often you get this, but good job. I see your face everywhere on these forums, and your long history and helpful nature is incredibly impressive. I believe you deserve some appreciation.

  • First of all: Glad you found the error and were able to fix it. Cool.

    Thanks a lot for your nice words of appreciation! Made my day already. 😊