How to properly use SpawnActor()?

So I made an actor called AMyActor and I made a root component and mesh component and it all works fine. I am attempting to spawn it from the character.cpp class. When I do this in BeginPlay(), it tells me that the arguments dont match and stuff “no instance of overloaded function…matches the argument list” So am I missing something?
Imgur

“no instance of overloaded function…matches the argument list” means you are passing arguments into a function but that function doesn’t accept those as the correct arguments.

These are the two SpawnActor functions in World.h:

	AActor* SpawnActor( UClass* InClass, FVector const* Location, FRotator const* Rotation, const FActorSpawnParameters& SpawnParameters );

	AActor* SpawnActor( UClass* Class, FTransform const* Transform, const FActorSpawnParameters& SpawnParameters));

Update your call to SpawnActor to match these argument types and it will work.

Such as:

AMyActor *MyActor = GetWorld( )->SpawnActor<AMyActor>(AMyActor::StaticClass( ), &SpawnLocation, &Rotation, Info);

Hi,
I had the same problem, and I found two possible solutions.

  1. If your actor is created in the Unreal Editor, then you can simply spawn it by code like this:

    UClass* MyItemBlueprintClass = StaticLoadClass(UObject::StaticClass(), NULL, TEXT("/Game/Weapons/axes/DoubleAxeActor.DoubleAxeActor_C"), NULL, LOAD_None, NULL);
    FActorSpawnParameters SpawnInfo;
    SpawnInfo.Owner = this;
    SpawnInfo.Instigator = Instigator;
    ApsItemActor* obj = spawnManager->currentWorld->SpawnActor(MyItemBlueprintClass, newlocation, GetActorRotation(), SpawnInfo);

The pre-requisite is that your actor is replicated. Please note that actors deriving from AActor are not replicated by default, so you need to add in their constructor:

bReplicates = true;

or within the editor you should flag your actor as “Replicates”. It is one of the properties in the details panel.

  1. If you want to create your actor completely within C++, and you have only the StaticMesh and the Material in the Editor (this was my case), you can create it like this:

    FActorSpawnParameters SpawnInfo;
    SpawnInfo.Owner = this;
    SpawnInfo.Instigator = Instigator;
    ApsItemActor* obj = GetWorld()->SpawnActor(ApsItemActor::StaticClass(), newlocation, GetActorRotation(), SpawnInfo);

    UStaticMeshComponent* MyMeshComponent = NewObject(obj, UStaticMeshComponent::StaticClass(), TEXT(“Mesh”));

    UStaticMesh* MeshAsset = Cast(StaticLoadObject(UStaticMesh::StaticClass(), NULL, TEXT(“StaticMesh’/Game/Weapons/axes/doubleaxe02abc.doubleaxe02abc’”)));
    UMaterial* MaterialAsset = Cast(StaticLoadObject(UMaterial::StaticClass(), NULL, TEXT(“Material’/Game/Weapons/axes/doubleaxe02c_Mat.doubleaxe02c_Mat’”)));

    MyMeshComponent->SetStaticMesh(MeshAsset);
    MyMeshComponent->SetMaterial(0, MaterialAsset);
    MyMeshComponent->SetWorldLocation(newlocation);
    MyMeshComponent->SetIsReplicated(true);

    MyMeshComponent->RegisterComponent();
    obj->AddOwnedComponent(MyMeshComponent);
    obj->SetRootComponent(MyMeshComponent);

Also in this case the pre-requisite is that your actor is replicated. Same case for case 1.

If your actor is invisible client side, means you didn’t replicate it, or you didn’t set the position properly. Please note the SetWorldLocation call despite the fact you are already giving the location in the spawnactor command. For me it works only if I call explicitely SetWorldLocation