How to RPC a Singleton Function?

Hi guys,

I'm trying to RPC a function in a separate script, attached to a non-networked mesh.

TL;DR - Have a Frame Mesh that downloads an image and applies it as a texture - Want to update this for everyone in the room when a player changes it.

What would be the correct way of executing a RPC call for a function on a different script?


Big thanks!

Answers

  • Tobias
    Tobias admin
    edited October 2022

    This sounds like a case for Custom Properties (this could tell everyone which texture to load).

    You can't call RPCs on non-networked objects. In doubt, have a reference to whatever object you want to affect and have the RPC method call that other object's method. That's plain C#...

  • Thank you for getting back to me Tobias,

    I've had a go at Properties but that's way over my head. I'm still learning, needless to say.

    I'm actually having a really hard time with RPCs don't really know what I'm doing wrong. I'm simply trying to update a string at this point, but it never syncs across the entire room, each player updates their own string.

    Here is the code:


    public TextMeshPro NonNetworked;

    public string num;


    private void Update()

    {

    this.photonView.RPC("randomNum", RpcTarget.All);

    }


    [PunRPC]

    public void randomNum ()

    {

    if (Input.GetKeyDown(KeyCode.Alpha8))

    {

    num = "8";

    NonNetworked.text = num;

    }

    if (Input.GetKeyDown(KeyCode.Alpha9))

    {

    NonNetworked.text = num;

    num = "9";

    }

    if (Input.GetKeyDown(KeyCode.Alpha0))

    {

    num = "10";

    NonNetworked.text = num;

    }


    THE SCRIPT IS ATTACHED TO AN OBJECT WITH A PHOTONVIEW.

  • Your Update() calls another method via network. Every frame.

    There is no parameter when you call the RPC, so the value you would set, is not networked.

    This will explode when you have multiple players in the network.

    As you said you are a beginner .. please begin at the beginning. You don't know how to use Properties? Learn it. It is the right tool. Yes, you could technically use RPCs but you wouldn't use a hammer for everything, just because you only understand hammering...

  • Thank you for the guidance and time Tobias,

    I took the time to learn more about properties, Observables and other concepts and my test project now works + I've gained experience to improve on all my other mechanics!

    Thanks again! 😁