All actors are Replicated and AlwaysRelevant.
PlayerCharacter have replicated variable (pointer)
UPROPERTY(Replicated)
AWeaponBase* CurrentWeapon;
void APlayerCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(APlayerCharacter, CurrentWeapon);
}
AWeaponBase is simple Actor.
On server, i spawn actor at some time and set result to the variable:
if (Character->HasAuthority())
{
FActorSpawnParameters SpawnParameters = FActorSpawnParameters();
SpawnParameters.Owner = Character;
Character->CurrentWeapon = Cast<AWeaponBase>(GetWorld()->SpawnActor(CurrentWeaponType, &SocketLocation, &SocketDirection, SpawnParameters));
}
And I can’t understand the results:
- Clients will recieve created AWeaponBase ONLY when Owner is set to server character (listen server playable one) and Ownership and CurrentWeapon will be set correctly too.
- If Owner is set to client character, then clients don’t recieve newly created actor (CurrentWeapon not replicated too). And much more strange behaivor is that if I run RPC (NetMulticast, Reliable) on that created weapon - it will spawn on clients correctly with correct Owner (also it will cause correct CurrentWeapon replication)
Anyone can help, why it’s like that?
Another strange thing happen if I try to set owner after creation:
FActorSpawnParameters SpawnParameters = FActorSpawnParameters();
Character->CurrentWeapon = Cast<AWeaponBase>(GetWorld()->SpawnActor(CurrentWeaponType, &SocketLocation, &SocketDirection, SpawnParameters));
Character->CurrentWeapon->SetOwner(Character);
It will spawn Weapon only on client (clients CurrentWeapon is not set) on server therefore CurrentWeapon will be valid (but actor is not visible as its not spawned)