Spawn Actor does not spawn an actor from a widget in C++

Hi, I have a problem. I’m trying to spawn an actor from a widget for preview inside the widget. If I do it with the Spawn Actor From Class node, everything is created perfectly. But in C++ there are problems: when I try to call spawn, the console says that the actor is created, but in fact there is nothing in the outliner. I can’t understand where it is being created. I’m a big beginner in C++, so it’s kind of like that. I read that you need to disable replication; in this situation it’s indeed not needed, but that still didn’t help.

I am using Play As Client and Play As Listen Server.

Here it is in text form:

APlayerController* PlayerController = GetOwningPlayer();

FActorSpawnParameters SpawnInfo;
SpawnInfo.Owner = PlayerController;
SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
SpawnInfo.bDeferConstruction = false;

if (PlayerController->IsLocalPlayerController())
{
    PreviewActor = GetOwningPlayerPawn()->GetWorld()->SpawnActor(
    APreviewItemActor::StaticClass(),
    FTransform::Identity,
    SpawnInfo
    );
    PreviewActor->SetReplicates(false);
}

I found a solution for my case: I need to create an additional variable that will act as the class when spawning the actor.

UPROPERTY(EditAnywhere, BlueprintReadWrite)
TSubclassOf PreviewActorClass;

It needs to be set in the Blueprint. After that, it is simply used as the class when spawning the actor:

PreviewActor = GetOwningPlayerPawn()
->GetWorld()
->SpawnActor(
PreviewActorClass,
FTransform::Identity,
SpawnInfo
);

image