For this recipe, you'll need to have an Actor
subclass ready to instantiate. You can use a built-in class such as StaticMeshActor
, but it would help to practice with the custom Actor
you made in the previous recipe.
Create a new C++ class, like in the previous recipe. This time, select
GameMode
as your base class, giving it a name such asUE4CookbookGameMode
.Declare a function override in your new
GameMode
class:virtual void BeginPlay() override;
Implement
BeginPlay
in the.cpp
file:void AUE4CookbookGameMode::BeginPlay() { Super::BeginPlay(); GEngine->AddOnScreenDebugMessage(-1, -1, FColor::Red, TEXT("Actor Spawning")); FTransform SpawnLocation; GetWorld()->SpawnActor<AMyFirstActor>( AMyFirstActor::StaticClass(), &SpawnLocation); }
Compile your code, either through Visual Studio or by clicking on the Compile button in Unreal Editor.
Open the World Settings panel for the current level by clicking on the Settings toolbar...