I have a function in my actor that dynamically creates new static mesh component (a Wall Mesh) next to my InitialWallMesh. Function i use is this:
void ABuildableWall::AppendWall(int32 WallIndex, ERelativeDirection RelativeDirection)
{
// Make sure we are on server
if(!HasAuthority())
{
UE_LOG(LogSecurity, Warning, TEXT("Can't append wall on client! Only called on server!"))
return;
}
if(RelativeDirection == ERelativeDirection::None)
return;
UStaticMeshComponent* Wall = GetWallByIndex(WallIndex);
if(!IsValid(Wall))
return;
// Get offsets
FVector LocationOffset{};
FRotator RotationOffset{};
GetLocalOffsetsByDirection(RelativeDirection, LocationOffset, RotationOffset);
// Convert to world space from component space
FTransform Transform = Wall->GetComponentTransform();
FVector SpawnLocation = Transform.TransformPosition(LocationOffset);
FRotator SpawnRotation = Transform.TransformRotation(RotationOffset.Quaternion()).Rotator();
// @TODO: Why replication doesn't work with DuplicateObject? Maybe some flags needs to be set or something...
UStaticMeshComponent* NewWall = DuplicateObject(InitialWallMesh, this, *FString("Wall Mesh").Append(FString::FromInt(Walls.Num())));
//UStaticMeshComponent* NewWall = NewObject<UStaticMeshComponent>(this, UStaticMeshComponent::StaticClass(), *FString("Wall Mesh").Append(FString::FromInt(Walls.Num())));
//UStaticMeshComponent* NewWall = Cast<UStaticMeshComponent>(StaticDuplicateObject(InitialWallMesh, this, *FString("Wall Mesh").Append(FString::FromInt(Walls.Num())), RF_AllFlags & ~RF_Transient));
// Attach and set transform
NewWall->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepWorldTransform);
NewWall->SetWorldLocation(SpawnLocation);
NewWall->SetWorldRotation(SpawnRotation);
// Copy properties (for NewObject)
NewWall->SetIsReplicated(InitialWallMesh->GetIsReplicated());
NewWall->SetStaticMesh(InitialWallMesh->GetStaticMesh());
// Copy Collisions (for NewObject)
NewWall->SetCollisionProfileName(InitialWallMesh->GetCollisionProfileName());
NewWall->SetCollisionEnabled(InitialWallMesh->GetCollisionEnabled());
NewWall->SetCollisionResponseToChannels(InitialWallMesh->GetCollisionResponseToChannels());
NewWall->SetNotifyRigidBodyCollision(InitialWallMesh->GetBodyInstance()->bNotifyRigidBodyCollision);
NewWall->SetGenerateOverlapEvents(InitialWallMesh->GetGenerateOverlapEvents());
NewWall->RegisterComponent();
Walls.Add(NewWall);
AddInstanceComponent(NewWall);
// Add new wall as replicating subobject
AddReplicatedSubObject(NewWall, COND_None);
}
So the problem is when i use DuplicateObject or StaticDuplicateObject component spawns only on server and never replicates to clients. Everything is ok if i use NewObject, but then i need to copy all the properties myself. What am i doing wrong here?