[FUSION] Sync text between clients

Options
Ritarirane
edited July 2022 in Fusion

Hey,

absolute photon noob here, how do i sync text between 2 clients?

here is my code:

my string:

  [Networked] string networkedtext { get; set; }


my rpc:

[Rpc(RpcSources.All, RpcTargets.All)]

  void RPC_SendMessage()

  {

    networkedtext = new System.Random().Next(1, 11).ToString();

    TextField.text = networkedtext;

    Debug.Log("RPC executed");

  }


i call the rpc every 3 secs but clients have different numbers

RPC_SendMessage();

Answers

  • Isaac_Augusto
    edited July 2022
    Options

    Hi,

    Please use the type [Networked] NetworkString<_x> networkedtext {get;set;} where x is the size of your string in memory.

    e.g: NetworkString<_2>, NetworkString<_4> ...

    I'm not sure if you're using Shared or Host/Server mode, but in either case you need to sent the RPC only to the State authority over the NetworkObject. , each client is getting a different number because each one is running the random function. So it should be something like that:

    [Rpc(RpcSources.All, RpcTargets.StateAuthority)]
    
      void RPC_SendMessage()
    
      {
    
        networkedtext = new System.Random().Next(1, 11).ToString();
    
        Debug.Log("RPC executed");
    
      }
    

    And you need to have a OnChanged for assig the text field, instead of the RPC, because the RPC will run only on the state authority now. Please take a look at the samples and documentations to understand better, but for now the networkedtext should be sync between the clients, you can Debug.log it on the FixedUpdateNetwork or the unity Update, but only use networked values for logic on the FixedUpdateNetwork.


    Hope this was helpful.

    -----

    Isaac Augusto

    Photon Fusion Team