Spawn actor in c++

Hi there. I am trying to spawn Actor from c++ class, but unreal editor always crush, when i do this.
Here is my code:

for (int i = 0; i < 5; i++){
    for (int j = 0; j < 5; j++){
        FVector SpawnLoc = FVector(40.0f + 80.0f*i, 40.0f + 80.0f*j, 32.0f);
        Aunit* unit = GetWorld()->SpawnActor<Aunit>(GetClass(), SpawnLoc, FRotator::ZeroRotator);
        gameField[i][j] = unit;
    }
}

Two more things:

  1. I am using ue4 on mac

  2. I am really newbie in using c++ in ue4

Yep, I have used SetNum() function for this.
Log file:
link text

Has your array already been initialized? Any error window/stack trace of the crash?

To spawn an actor in C++ you simply do this…

FVector location = FVector(*insert location here*);
		FRotator rotate = FRotator(*insert rotation here*);
		FActorSpawnParameters SpawnInfo;
		spawnedActor = World->SpawnActor<AMyActor>(AMyActor::StaticClass(), location, rotate, SpawnInfo);

Note the fourth line

spawnedActor = World->SpawnActor<AMyActor>(AMyActor::StaticClass(), location, rotate, SpawnInfo);

This saves the newly spawned actor in a variable called spawnedActor. You don’t have to save the actor into a variable if you don’t think you’ll need to access it later on. Hope this helps!

So, now i have this

void ATicTacToe5x5GameMode::spawnGameField() {

for (int i = 0; i < 5; i++){
    TArray<Aunit*> line;
    for (int j = 0; j < 5; j++){
        FVector SpawnLoc = FVector(40.0f + 80.0f*i, 40.0f + 80.0f*j, 32.0f);
        FActorSpawnParameters SpawnInfo;
        Aunit* unit = GetWorld()->SpawnActor<Aunit>(Aunit::StaticClass(), SpawnLoc, FRotator::ZeroRotator, SpawnInfo);
        line.Add(unit);
    }
    gameField.Add(line);
}

}

And it also crashes.
Log:
link text

I simulated what you’re trying to do within my own project and I don’t seem to get a crash.

I’m guessing that ‘line’ and ‘gameField’ are TArrays. Did you declare those arrays in the header (TicTacToe5x5GameMode.h). If you didn’t, try doing that as declaring an array within your spawning method exclusively won’t stick around after the method executes. At least, that’s what I think. Also try to comment out lines to see where the crash occurs.

You may also want to add:

SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn

To make sure the actor always spawns

the code looks fine so there is a problem outside that snippet, like everybody says its either the gameField not being initialized or the problem could be within the Aunit class?

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