How can I tell when an actor has been replicated?
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?
asked Feb 27 '15 at 03:49 AM in C++ Programming
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: https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Engine/ENetRole/index.html. 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 :D
answered Feb 27 '15 at 08:36 AM
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.
answered Mar 03 '15 at 04:10 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here