I am trying to spawn weapons on server and attaching it to the clients but I am not able to do so.
The weapons don’t appear visible to the clients.
Here is my code.
Character.cpp
void APSETPCCharacter::BeginPlay()
{
if (Role == ROLE_Authority)
{
SpawnDefaultWeapon();
}
}
void APSETPCCharacter::SpawnDefaultWeapon()
{
if (Role < ROLE_Authority)
{
return;
}
if (DefaultWeapon)
{
UWorld* World = GetWorld();
if (World)
{
ABaseWeapon* TempWeapon = GetWorld()->SpawnActor<ABaseWeapon>(DefaultWeapon);
TempWeapon->SetOwner(this);
TempWeapon->OnEquip();
CurrentWeapon = TempWeapon;
}
}
}
And the weapon attachment is called from OnEquip() function like this.
void ABaseWeapon::OnEquip()
{
/** Do weapon equip things bla */
OnWeaponSpawn();
}
void ABaseWeapon::OnWeaponSpawn()
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, "QWeqwe");
ItemMesh->AttachTo(OwningPawn->GetMesh(), FName(TEXT("WeaponSocket_Rifle")));
}
Some addition constructor changes I made to the weapon are
{
ItemMesh = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("Weapon"));
RootComponent = ItemMesh;
PrimaryActorTick.bCanEverTick = true;
bReplicates = true;
bNetUseOwnerRelevancy = true;
}