Simply spawning an actor


I’m trying to spawn an actor in the world. I’m new to Unreal Engine and I’m unsure what to use as my main class. I assume the GameMode, so I’ve written the following:

    : Super()
    // set default pawn class to our Blueprinted character
    static ConstructorHelpers::FClassFinder<APawn> PlayerPawnClassFinder(TEXT("/Game/FirstPersonCPP/Blueprints/FirstPersonCharacter"));
    DefaultPawnClass = PlayerPawnClassFinder.Class;

    // use our custom HUD class
    HUDClass = AMyGameHUD::StaticClass();

    NSDebug nsdebug;


// Fill out your copyright notice in the Description page of Project Settings.

#include "NSDebug.h"

#include "Items/FuelTank.h"
#include "Engine.h"



void NSDebug::init(AMyGameGameMode* gamemode)
    // something here is causing a crash
    /*FVector Location(0.0f, 0.0f, 0.0f);
    FRotator Rotation(0.0f, 0.0f, 0.0f);
    FActorSpawnParameters SpawnInfo;
    AFuelTank* tank = gamemode->GetWorld()->SpawnActor<AFuelTank>(Location, Rotation, SpawnInfo);
    debug("Spawned FuelTank");

void NSDebug::debug(const char* msg)
    GEngine->AddOnScreenDebugMessage(-1, 9999, FColor::Cyan, FString(msg));

However, this is causing the entire engine to crash. If you could let me know what the problem is, I’d really appreciate it. I can’t continue my project until this is solved.

It’s crashing because GetWorld is likely returning NULL.

You normally want to do things like spawning actors and such in “BeginPlay”, which is guaranteed to happen after the world is created.

See the flow chart here:

Thanks for your quick response, but on BeginPlay of what should I put my code?

There is no one place where you can do that. You could create a “manager” object that you manually drop in your levels that does all the spawning, or you could use “StartMatch/StartPlay” in AGameMode (as shown in that flow chart).

You should not only check if GetWorld() is valid, like ExtraLifeMatt said, but also the spawned object, tank, before using them.

Serious question though, doesn’t it make more sense NOT to do so much defensive coding, and instead fix the crashes as they come? Say you’re unable to spawn an actor on beginplay of the GameMode, what good does it do NOT to crash? Besides, does it not hide where the issue is by not crashing? Unless you would also recommend adding an error log.

Lots of resources out there do nullchecks absolutely everywhere, which bloats the code like crazy, that’s really puzzling…

It depends. If it’s safe to continue execution, then checking for null or whatever and handling it gently is fine. However, if you really think there should never be null values (or whatever case you are searching for), you can use ASSERT which will crash but give you a nice message/callstack and gets compiled out in release.

I always tough nullchecks and error logs went hand in hand during development, and therefore unnecessary to say it. After all, that’s programming 101. Since the OP is using C++, I just assumed he knew that.