I am trying to spawn the characters equipment in my multiplayer game. From what I understand actor can only be spawned on the server or else the client-spawned actor will think it has ROLE_Authority.
So a little background, my character has a UActorComponent called “ArenaCharacterEquipment” which I want to contain all the characters gear.
In PostInitializeComponents on my character I check for Role == ROLE_Authority, so if its the server I want to spawn all the equipment. This works.
“ArenaCharacter.cpp”
void AArenaCharacter::PostInitializeComponents()
{
Super::PostInitializeComponents();
if (Role == ROLE_Authority)
{
CharacterEquipment->SpawnDefaultEquipment();
}
}
“ArenaCharacterEquipment.cpp”
void UArenaCharacterEquipment::SpawnDefaultEquipment()
{
FActorSpawnParameters SpawnInfo;
SpawnInfo.bNoCollisionFail = true;
PrimaryWeapon = GetWorld()->SpawnActor<AArenaWeapon>(PrimaryWeaponBP, SpawnInfo);
PrimaryWeapon->SetOwningPawn(Cast<AArenaCharacter>(GetOwner()));
PrimaryWeapon->SetPrimary(true);
PrimaryWeapon->UnEquip();
SecondaryWeapon = GetWorld()->SpawnActor<AArenaWeapon>(SecondaryWeaponBP, SpawnInfo);
SecondaryWeapon->SetOwningPawn(Cast<AArenaCharacter>(GetOwner()));
SecondaryWeapon->SetPrimary(false);
SecondaryWeapon->UnEquip();
}
I set ReplicatedUsing on the Primary and Secondary weapon. However these are never called. When I play the game, the server spawns both his and the clients gear fine. However on the clients side no gear is spawned for him or the server.
“ArenaCharacterEquipment.h”
UPROPERTY(Transient, ReplicatedUsing = OnRep_PrimaryWeapon)
class AArenaWeapon* PrimaryWeapon;
UPROPERTY(EditAnywhere, Category = Weapons)
TSubclassOf<class AArenaWeapon> PrimaryWeaponBP;
UPROPERTY(Transient, ReplicatedUsing = OnRep_SecondaryWeapon)
class AArenaWeapon* SecondaryWeapon;
UPROPERTY(EditAnywhere, Category = Weapons)
TSubclassOf<class AArenaWeapon> SecondaryWeaponBP;
“ArenaCharacterEquipment.cpp”
void UArenaCharacterEquipment::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(UArenaCharacterEquipment, PrimaryWeapon);
DOREPLIFETIME(UArenaCharacterEquipment, SecondaryWeapon);
}
void UArenaCharacterEquipment::OnRep_PrimaryWeapon()
{
FActorSpawnParameters SpawnInfo;
SpawnInfo.bNoCollisionFail = true;
PrimaryWeapon = GetWorld()->SpawnActor<AArenaWeapon>(PrimaryWeaponBP, SpawnInfo);
PrimaryWeapon->SetOwningPawn(Cast<AArenaCharacter>(GetOwner()));
PrimaryWeapon->SetPrimary(true);
PrimaryWeapon->UnEquip();
}
void UArenaCharacterEquipment::OnRep_SecondaryWeapon()
{
FActorSpawnParameters SpawnInfo;
SpawnInfo.bNoCollisionFail = true;
SecondaryWeapon = GetWorld()->SpawnActor<AArenaWeapon>(SecondaryWeaponBP, SpawnInfo);
SecondaryWeapon->SetOwningPawn(Cast<AArenaCharacter>(GetOwner()));
SecondaryWeapon->SetPrimary(false);
SecondaryWeapon->UnEquip();
}