Replicate multiple actors in order or at once?

Say a vehicle has two weapons. That is a total of three actors. Vehicle actor spawns its weapons during roleAuthority PreInitializeComponents. The weapon during spawn wants to GetOwner()->NotifyWeaponSpawned during beginplay but it can’t because it was replicated BEFORE the vehicle. This would make owner null until vehicle finishes replicating.

I can only think of one system that fixes this problem and thats actors + actorComponents. With actor components they are guaranteed to all be replicated along side its owning actor, always, everytime. But actors spawned from other actors have no such system. For christ’s sake the GameState and PlayerState is sometimes is the LAST thing to replicate vs other actors in the map.

Is there any way to enforce some actors to replicate before others? I just want to call some functions via GetOwner()-> that’s all.

It’s not possible - Actor replication order is not deterministic and and enforcing an order is not possible (doing so would also be very fragile, when you consider packet loss in a real-world scenario etc.)

You will want/need to design the code to be resilient to actors arriving in different orders, as this is paramount to achieving game stability and reliability. Always assume that actors can arrive in any order and code around that. This part of Multiplayer programming isn’t often talked about, but a lot of the time you are going to be coding around race conditions.

This often involves breaking your “initialization” functions out into their own functions instead of say, BeginPlay() - then make use of RepNotifies to attempt initialization when properties such as the owner are updated (many engine RepNotifies are virtual and can be overridden for this reason). Psuedo-Code Example:



void Actor::BeginPlay()
{
Super::BeginPlay();

AttemptInitialization();
}

void Actor::AttemptInitialization()
{
if (GetOwner())
{
// Do Init etc..
}
}

void Actor::OnRep_Owner()
{
Super::OnRep_Owner();

AttemptInitialization();
}


2 Likes

i restructured things so weapons only need to call stuff on owner, and not vice versa. OnRep_Owner is the solution, thank you.