How do you replicate a TArray of UObjects

I created an Actor Component with an array of objects derived from UObject. I’ve setup the UObject to be replicatable on it’s own when within the ActorComponent but when I put it into a TArray the game crashes.

My Setup is as follows:

class UMyActorComponent
{
    UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Replicated)
    TArray<UMyObject> myObjects
}

void UMyActorComponent::GetLifetimeReplicatedProps(...)
{
    DOREPLIFETIME(UMyActorComponent, myObjects)
}

bool UMyActorComponent::ReplicateSubobjects(ActorChannel, Bunch, RepFlags)
{
    bool wroteSomething = Super::(ActorChannel, Bunch, RepFlags);
        
    for(int32 i = 0; i < myObjects.Num(); ++i)
    {
        if(myObjects[i]->IsReplicationNeeded()) // custom flag
        {
            wroteSomething |= ActorChannel->ReplicateSubobject(myObjects[i], *Bunch, *RepFlags)
        }
    }
}

while writing this I just had a thought… I had been looking at the documentation on replicating Dynamic Arrays… Do I absolutely positively HAVE to have each element in the array if I am not using the ReplicationKey function…??.. that may be it…

I’m having a hard time thinking of why you would need to do this - you can just add actors with replication set to true to your scene, then you can access them using GetAllActors - aka your map/world IS your array of actors

Can you give a higher level example of what you are trying to accomplish?

Am I really the only person whose ever tried this???

Can you elaborate on how the game is crashing? Have you stepped through with the debugger? It’s probably a null/invalid pointer in myObjects[i] that’s crashing when you access it. Either that or the IsReplicationNeeded() function is crashing (can’t see if this is possible without seeing the code).

The major reason is to replicate generic data to the server about the player. The server is master of the data but the player can see his or her own stats. Specifically there is a rather complicated trait and status effect system of objects that don’t need to be actors, but do need to be replicated. I found that UActorComponent allows replication from the parent actor so I started there.

(I wrote a comment earlier… but it’s not showing up so… pardon me if this is a duplicate response.)

Unfortunately all I can say is that the error occurred at the moment of replication. I setup an Interface on the client and server to display bit of replicated information. The moment I called a command to change the client’s data the game crashed. I sort of gave up and moved on to something else… then came back in all my stubbornness and actually got it working.

However what was causing the crash I am not sure about as I have completely destroyed the previous code that was leading to the crash. But I will be making a post on how to replicate a TArray of UObjects to help others who may need this later.

My guess is that the error occurred because I was inadvertently creating the array on BOTH the client and the server thus leading to similar but different objects. That or maybe a TArray must be duplicated in it’s entity… but I don’t think that was the problem as my new experiences with replicating UObjects in a TArray don’t seem to care much about the Array itself, but of the object… This is a hypothesis I had intended to actually test. My new objects are constructed only on the server and replicated down to the client.

I replicate tarrays, but typically I use enums or structs inside of them - I do sometimes put references to objects in those structs/enums/arrays but I never have to do the “ReplicateSubobjects” part and it works fine - i.e if those objects in your scene are set to replciate, it takes care of it on its own

I think that is because those are “natively” supported because they are simple types. I know UStructs replicate without issue… In retrospect because struct and classes are basically the same thing now in C++ i probably could have just used them… but stubbornness.

I have since solved it though, and will be posting the answer (with code) soon as it was a solid week of banging head against wall, scraping everything and starting small while searching any information I could find about replication, subobjects, and TArrays. I swear I read the same documentation 10-30 times looking for subtle clues and hints…

For the answer to this question I wrote out an explaination of Replicating a TArray of Subobjects in the forums