Hi
Thanks for the great tutorial series ! I learned a lot from it.
I am extending the code to enable spawning objects into the world and then carrying them. Just as into your Pickup() function I have a SpawnNewActor() function hooked up to the B key.
void USCarryObjectComponent::SpawnNewActor()
{
if (GetIsCarryingActor())
{
return;
}
if (GetOwner()->Role < ROLE_Authority)
{
ServerSpawnNewActor();
return;
}
UWorld* const World = GetWorld();
if (World){
//ConstructorHelpers::FObjectFinder<UBlueprint> MyBlueprint(TEXT("/Game/Test/Stairs/StairsBP.StairsBP"));
auto Object = StaticLoadObject(UObject::StaticClass(), nullptr, TEXT("/Game/Test/Stairs/StairsBP"));
UBlueprint *BP = Cast<UBlueprint>(Object);
APawn* OwningPawn = Cast<APawn>(GetOwner());
FActorSpawnParameters SpawnParams;
SpawnParams.Instigator = OwningPawn;
SpawnParams.Instigator = OwningPawn->Instigator;
FVector Location = OwningPawn->GetActorLocation() + OwningPawn->GetActorForwardVector() * 600;
AActor *SpawnedActor = World->SpawnActor<AActor>((UClass*)BP->GeneratedClass, Location, FVector::ZeroVector.Rotation(), SpawnParams);
SpawnedActor->SetReplicates(true);
SpawnedActor->SetReplicateMovement(true);
OnSpawnNewActorMulticast(SpawnedActor);
}
}
void USCarryObjectComponent::ServerSpawnNewActor_Implementation()
{
SpawnNewActor();
}
bool USCarryObjectComponent::ServerSpawnNewActor_Validate()
{
return true;
}
void USCarryObjectComponent::OnSpawnNewActorMulticast_Implementation(AActor* SpawnedActor)
{
if (SpawnedActor)
.......
.......
}
It works fine in singleplayer, the object is spawned when the B key is hit, attached to the end of the boom and I can move it around and drop it when I press the middle mouse button. The problem is in multiplayer. On the server its all fine but on the clients the SpawnedActor parameter in the OnSpawnNewActorMulticast_Implementation function is always null. When I drop the object it appears on both client and server but on spawning it fails to attach to the boom in the client as it is null.
Any thoughts on what might be wrong ?
Thanks !
Sanjit