How to extend functions in GameMode class?

Hi!
I’m little confused about extending functions. I want to spawn my pawn in nav mesh location so I need to extend RestartPlayer function in MyGameMode. I’ve put in this in MyGameMode.h

virtual void RestartPlayer(class AController* NewPlayer) override;

but when I put declartion in cpp file then I’ll get all UE_LOG’s with error. Also there is
“use of undefined type ‘APlayerState’” build error. So it dosen’t look like this function use GameMode variables.
I’m still thinking in US way so I’m surprised that this isn’t working :slight_smile:

I’ve just started with c++ so can someone show me the way how to do it? Thanks.

First thing you should do:
Solely have the overwritten function RestartPlayer as an empty function, add a breakpoint and see if it enters the function.

Did you set your custom GameMode class to be used in the editor’s project settings?

I’m not sure how to use breakpoints. I’ve added it in VS and started instance in Debug Game Editor but nothing happens. I’ve also added UELOG but theres is no this message in Output. My GameMode is set correctly.
So I understand that I didn’t missed something obvious and all what I should do to extend function is that what I’ve made?

EDIT: Maybe RestartPlayer function isn’t triggered anyway but generaly my problem is why compiler can’t find reference from parent function?

If you haven’t dont so yet, read this: Setting Up Your Development Environment for C++ in Unreal Engine | Unreal Engine 5.1 Documentation .

Breakpoints are points that you can set at lines of code to tell the debugger to prior to executing this line. Look here and scroll to “Debugging breakpoint” http://www.dotnetperls.com/debugging .

If you say you’ve done everything correct then I’m not sure where to help : ).

What UE_LOG channel are you using? Since not all are active. I usually use

UE_LOG(LogEngine, Error,  TEXT("bla bla"));

when I just want to quickly check.

Thanks for tips. I always wonder what’s going on with breakpoints.

I use logs very often so i’m surprised that it’s not working. This is my error log from VS builder after copy RestartPlayer function from AGameMode class to AMyGamemode.

For Example this line

UE_LOG(LogGameMode, Verbose, TEXT("RestartPlayer %s"), (NewPlayer && NewPlayer->PlayerState) ? *NewPlayer->PlayerState->PlayerName : TEXT("Unknown"));

generates this error


    1>C:\UE4\ProtoBall\Source\ProtoBall\ProtoBallGameMode.cpp(31): error C2653: 'FLogCategoryLogGameMode' : is not a class or namespace name
    1>C:\UE4\ProtoBall\Source\ProtoBall\ProtoBallGameMode.cpp(31): error C2065: 'CompileTimeVerbosity' : undeclared identifier
    1>C:\UE4\ProtoBall\Source\ProtoBall\ProtoBallGameMode.cpp(31): error C2065: 'LogGameMode' : undeclared identifier
    1>C:\UE4\ProtoBall\Source\ProtoBall\ProtoBallGameMode.cpp(31): error C2228: left of '.IsSuppressed' must have class/struct/union
    1>          type is 'unknown-type'
    1>C:\UE4\ProtoBall\Source\ProtoBall\ProtoBallGameMode.cpp(31): error C2228: left of '.GetCategoryName' must have class/struct/union
    1>          type is 'unknown-type'
    1>C:\UE4\ProtoBall\Source\ProtoBall\ProtoBallGameMode.cpp(31): error C2027: use of undefined type 'APlayerState'
    1>          C:\Program Files\Epic Games\4.8\Engine\Source\Runtime\Engine\Classes\GameFramework/Pawn.h(13) : see declaration of 'APlayerState'
    1>C:\UE4\ProtoBall\Source\ProtoBall\ProtoBallGameMode.cpp(31): error C2227: left of '->PlayerName' must point to class/struct/union/generic type
    1>C:\UE4\ProtoBall\Source\ProtoBall\ProtoBallGameMode.cpp(31): error C2660: 'FMsg::Logf_Internal' : function does not take 4 arguments

Do you have TeamPlayerController.h added in your header file?

To always be sure the right headers are included create a c++ class from the editor: Using the C++ Class Wizard in Unreal Engine | Unreal Engine 5.1 Documentation

For the specific issue of the UE_LOG errors:

  1. Open up MyGameMode.cpp
  2. Scroll to the top of the file, right after the #include directives but before any functions.
  3. Add the text: DEFINE_LOG_CATEGORY_STATIC(LogGameMode, Log, All);

This should resolve the compilation issues, at least those related to logging. This line is copied directly from GameMode.cpp in the engine.

For actually inheriting RestartPlayer, you’ll probably want to start with something like this:

void MyGameMode::RestartPlayer(AController* NewPlayer)
{
	if (NewPlayer == NULL || NewPlayer->IsPendingKillPending())
	{
		return;
	}

	if (NewPlayer->PlayerState && NewPlayer->PlayerState->bOnlySpectator)
	{
		UE_LOG(LogGameMode, Verbose, TEXT("RestartPlayer tried to restart a spectator-only player!"));
		return;
	}

	Super::RestartPlayer(NewPlayer);

	//Your code here.
}

The Super:: line is important, as that will execute the existing code in the engine for RestartPlayer. Without that, you’d need to handle getting a StartSpot and possessing a new pawn yourself.