C++ GameMode / PlayerController questions

Hi,

Bit of background:

  • I have a custom game mode and player controller classes which get constructed as the application starts (building in DebugGame Editor).
  • Most, if not all of the applications content will be procedurally created and code driven (i.e. I will barely be using the Editor except to view the app and deploy etc).
  • I am using the procedural mesh generation code from the Wiki to generate custom meshes from some external-to-application data

I am new at this and struggling at the moment to find a good ‘starting point’ at which I can add spawn actors (and thus meshes) to the world in code. I have tried overriding some of the functions from the base ‘AGameMode’ class my game mode inherits from but they do not seem to ever get called. I will show the code I’m using below:


// Header for my custom GameMode
class ACustomGameMode : public AGameMode
{
	GENERATED_UCLASS_BODY()

	void StartPlay();  // This is a virtual function from AGameMode that doesn't get called?
};


// Cpp for my custom GameMode
ACustomGameMode::ACustomGameMode(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	PlayerControllerClass = ACustomPlayerController::StaticClass(); // This gets called
}

void ACustomGameMode::StartPlay() // This doesn't get called, I tried other functions like the 'Tick' override also, same result
{
       ((ACustomPlayerController*)PlayerControllerClass.GetDefaultObject())->MyFunction();
}


// Header for my custom PlayerController
UCLASS()
class ACustomPlayerController : public APlayerController
{
	GENERATED_UCLASS_BODY()

	void MyFunction();
};


// Cpp for my custom PlayerController
ACustomPlayerController::ACustomPlayerController(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{ // This gets called
}

void ACustomPlayerController::MyFunction() // This never gets reached
{
	FPostConstructInitializeProperties properties;
	ATerrainActor *anActor = new ATerrainActor(properties);
	anActor->PrimaryActorTick.bCanEverTick = true;

	FActorSpawnParameters sp;
	sp.Name = "SomeName";
	sp.bNoCollisionFail = true;
	FVector *location = new FVector(0.0f, 0.0f, 0.0f);
	FRotator *rotation = new FRotator(0.0f, 0.0f, 0.0f);

	UWorld *world = GetWorld();

	ATerrainActor *actor = (ATerrainActor*)world->SpawnActor(ATerrainActor::StaticClass(), location, rotation, sp);
	actor->PrimaryActorTick.bCanEverTick = true;
}

I guess you can sort of see what I’m trying to achieve: Find a function in which I can execute custom code to 1) create a spawn actor, 2) get that actor into the default ‘world’, 3) attach / detach procedurally created meshes to that actor on a constantly updating basis at different positions.

I have found it quite hard to find any tutorials dealing with what I am trying to do but if you know any, or have any direct advice it would be very very welcome.

Thanks.

You need to use

Virtual void StartPlay() override;

In the header.

Are you sure you’ve set up your GameMode to be the one used by your game? That could explain why neither StartPlay() nor Tick() are getting called.

+1 to this - make sure it’s configured to use this. I’ve made this mistake a lot.

To add to the current answers:

For begininning of play, I use these functions:

Header:
virtual void BeginPlay() override;

CPP:
void YourClassName::BeginPlay()
{
Super::BeginPlay();
}

And in the editor, go to Edit->Project Settings->Maps & Modes->Default Modes and check to make sure your custom game mode is being used here.