Download

Issues with OnRep callback with TArray of AActor

I’ll spare a code dump and give the high level overview.

There is one function, executed on the server, it does the following:

  1. Spawn AActor on server, only relevant to owner
  2. Set Owner to the appropriate client’s Player Controller, so it will replicate to them
  3. Adds the item to a replicated TArray, which has a OnRep callback executed on the client

The TArray OnRep seems to work great most of the time, but it does have a race condition. I can trigger this race condition 100% of the time with a debugger. In PIE, it’s pretty rare but does happen.

The race condition is that TArray will replicate with the proper .Num() of Items (in my test case, 2), however these items will be nullptr. My only guess is that the TArray is being replicated before the spawned AActors get sent to the client.

How can I be sure the client gets the AActors first before the OnRep of the TArray?

This still happens with Actor->ForceNetUpdate() before adding it to the TArray.