Calling RPC after OnPlayerJoined

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on Fusion.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Calling RPC after OnPlayerJoined

InvalidCola
2022-05-21 16:40:11

The project I am working on uses mostly RPC calls to communicate between clients. That works fine and the network communication runs smoothly.

When a player joins the host attempts to send an RPC message to the joining player (initiated in the OnPlayerJoined method), with some basic information about the game.

For some strange reason this message is not received by the joining player UNLESS a 2 second delay is added between OnPlayerJoined being called and the RPC being invoked on the host.

Here is the OnPlayerJoined code:

public void OnPlayerJoined(NetworkRunner runner, PlayerRef player)

{

Debug.Log("OnPlayerJoined");

NetworkObject networkPlayerObject = runner.Spawn(_playerPrefab, null, null, player);

_spawnedPlayers.Add(player, networkPlayerObject);

if (player != runner.LocalPlayer && _isHost == true)

{

// A message is added to a send queue and is sent with RPC in FixedUpdateNetwork

AddNetCommand(CommandType.ClientJoined, player);

}

}

And here is the RPC which is called from FixedUpdateNetwork:

[Rpc(RpcSources.InputAuthority, RpcTargets.All, InvokeLocal = false, InvokeResim = true)]

public RpcInvokeInfo RPC_SendDataTargeted([RpcTarget] PlayerRef player, byte[] data, RpcInfo info = default)

{

Debug.Log("GOT TARGETED MSG!);

PhotonConnectionManager.instance.DataReceived(data, false, info.Source);

return default;

}

With the above code the RPC is never received by the joining player. But if I add a 2 second delay before running AddNetCommand (e.g. by using a Coroutine), so the RPC is delayed, then everything works fine. I tried with a shorter delay (e.g. 0.5 seconds) but that does not seem to be enough. Note that messages are set to always be sent reliably, and during the gameplay that also seems to be the case.

Is there currently a delay between a player joining and the time where RPC commands can be sent to that player? Or am I doing something wrong?

Please let me know if I need to provide more information, and thanks in advance for any help <3

Comments

ramonmelo
2022-06-20 13:15:50

Hi @InvalidCola ,

First of all, you should not rely on RPCs, as they should be used for rare and short communication.

Is there currently a delay between a player joining and the time where RPC commands can be sent to that player?

There should be no delay necessary to send RPCs, not that we are aware of.

Does this also happen on the latest SDK build?

Keep in mind that the "OnPlayerJoined" will be invoked on all clients, so there is no particular reason to invoke the RPC there to inform other clients.

Another point about your implementation is that you are filtering by Host (_isHost == true) but is sending the RPC from the Input Authority, is that the right logic?

Meaning, is the Host the InputAuthority over the NetworkObject you are invoking the RPC?

--

Ramon Melo

Photon Fusion Team

Back to top