How can i replicate a TArray of AActor?

Hello,

I am trying to replicate a TArray of AActor that are spawn by an rpc sent to the server.

All this is happening inside a class inheriting from ACharacter.

Here is the header:

UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Replicated, Category = YagDice)
TArray<AActor*> D20Array;

UFUNCTION(Server, WithValidation, Reliable)
void SpawnDice20();

And here is the cpp:

void AYagPlayer::SpawnDice20_Implementation()
{
	[definition of the parameters, location, rotation, spawnparams]
	D20Array.Add((AActor*) GetWorld()->SpawnActor<AActor>(BP_d20, Location, Rotation, SpawnParams));
}

The DOREPLIFETIME macro is called in the GetLifetimeReplicatedProps function from the ACharacter inheriting class (here: AYagPlayer)

DOREPLIFETIME(AYagPlayer, D20Array);

The function is called through a key-pressed event.

With that setup, whether i press the key on the server or on the clients, the actors only appear on the server.

The clients can spawn them on the server but never see them.

So they are spawned by (and on) the server on client request but are not replicated.

I suspect that i am only replicating the pointers to the actors instead of the actors themselves, but i am a baby c++ programmer so i’m sure about nothing.

I tried to fill the array with the object instead of their adresses with that kind of changes:

TArray<AActor> D20Array;
D20Array.Add(& (AActor*) GetWorld()->SpawnActor<AActor>(BP_d20, Location, Rotation, SpawnParams));
D20Array.Add(& GetWorld()->SpawnActor<AActor>(BP_d20, Location, Rotation, SpawnParams));

but then i couldn’t compile.

Can someone help me understand what i am doing wrong ?

As always, thanks a lot in advance !

Cedric

OK Solved, i had to replicate each new actor separately:

AActor* SpawnedPlayer = (AActor*)GetWorld()->SpawnActor<AActor>(BP_d20, Location, Rotation, SpawnParams);
	SpawnedPlayer->SetReplicates(true);
	D20Array.Add(SpawnedPlayer);