I have a function in my AGameMode class which spawns a number of replicated actors and then calls a NetMulticast function which references those actors. If I run this function, it works fine on the server but fails on the clients as all the AActor* for the spawned actors are NULL. I can make it work by, after spawning the actors, not directly calling the NetMulticast function, but instead starting a timer to call the NetMulticast function after a few seconds. I assume this is necessary as it may take some time to replicate the actors to all the clients. However, I need this delay to be as short as possible. What is the best way to tell when an actor has been replicated? Is it better to do this on the server or client?
When an actor is replicated to the client it is getting spawned on the client side with the same NetIndex (this is a unique identifier of that Actor in your network play session). So knowing that they will all spawn sometime when that happens the BeginPlay method is called, using that method will give you the exact time when the replicated actors are entering the playground. Knowing the role of the actor is important to act accordingly, the available roles are outlined here: ENetRole | Unreal Engine Documentation. You can also use the net mode to check if the BeginPlay is called on the client side or not.
To check if you are in the server you normally check the role to match the highest one which is ROLE_Authority.
If you have more questions just add a comment ^^ I’ll be glad to help you out
I have a solution that seems to be working. I changed the AActor* from UPROPERTY(Replicated) to UPROPERTY(ReplicatedUsing = …) and in the OnRep function I set a boolean flag. As far as I can tell the AActor* are not replicated until after the actors themselves (which makes sense), so I just have the client check to see if this flag is set in the NetMulticast function and if it isn’t I start a very short timer to call the function again. Once the function actually runs, I clear the flag. Seems to be working so far.
Another solution can be check, if object’s ReplicateSubobjects was ever called that means it was sent to corresponding channel. While that does not mean the data was received by client, but I think (I hope) it is safe to call RPC’s after that. Still need some research.