How do I Spawn Actors?

I am trying to spawn an actor from within my C++ code but can’t seem to get it right without crashing Unreal when loading my project. I setup an actor class called Ball (ABall in C++) and have created a call to the SpawnActor function (from here) and it just doesn’t seem to work.

My current call is:

ABall* SpawnedBall1 = (ABall*)GWorld->SpawnActor(ABall::StaticClass());

which I thought would be substantial in creating a spawn of the object, but clearly not. Any pointers or help would be appreciated.

Thanks

It looks like you probably need to check that the world exists before you spawn your actor. Here’s an example from the FPS Tutorial:

UWorld* const World = GetWorld();
            if (World)
            {
                FActorSpawnParameters SpawnParams;
                SpawnParams.Owner = this;
                SpawnParams.Instigator = Instigator;
                // spawn the projectile at the muzzle
                AFPSProjectile* const Projectile = World->SpawnActor<AFPSProjectile>(ProjectileClass, MuzzleLocation, MuzzleRotation, SpawnParams);
                if (Projectile)
                {
                    // find launch direction
                    FVector const LaunchDir = MuzzleRotation.Vector();
                    Projectile->InitVelocity(LaunchDir);
                }
            }

Thanks NTG, this has allowed my editor to open with the spawn actor function compiled, however I am still not seeing any type of object appear in the game/hierarchy. I’ll take a look through the FPS tutorial and see if that can give me any pointers when it spawns the projectile object.

Try:
ABall* SpawnedBall1 = GWorld()->SpawnActor(ABall::StaticClass());

Maybe it will help:
Spawning:
So you need a game Mode as C++ class and then in the Editor under Edit->Project Settings->Maps & Modes: Default Modes you need to set your created GameMode as default game mode. So that this game mode is launched when you start your game.

Or :
open class Viewer from editor and drag your object inside the scene. But i think you want to spawn it.

GameMode cpp:

void AKawummGameMode::BeginPlay()
{
Super::BeginPlay();
UWorld* const World = GetWorld();
if (World)
{
	World->SpawnActor<ASolidBlock>(ASolidBlock::StaticClass());
}
}

SolidBlock cpp:

ASolidBlock::ASolidBlock(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	MyBlock = PCIP.CreateAbstractDefaultSubobject<UStaticMeshComponent>(this, TEXT("SolidBlock"));

	static ConstructorHelpers::FObjectFinder <UStaticMesh>StaticMesh(TEXT("StaticMesh'/Game/Blocks/SolidBlock.SolidBlock'"));
	static ConstructorHelpers::FObjectFinder<UMaterial> Material(TEXT("MaterialInstanceConstant'/Game/Materials/M_Basic_Floor.M_Basic_Floor'"));

	MyBlock->SetStaticMesh(StaticMesh.Object);
	MyBlock->SetMaterial(0, Material.Object);
}

and SolidBlock .h

UCLASS()
class ASolidBlock : public AActor
{
	GENERATED_UCLASS_BODY()
	virtual void BeginPlay() OVERRIDE;

	// static mesh
	TSubobjectPtr<UStaticMeshComponent> MyBlock;
};

ok. i hope it helps a bit when spawning :slight_smile:

I think this shouldn’t be an answer, please instead write a comment as this “answer” doesn’t try to solve the question.