Getting access to the GameMode

How do I get access to the GameMode class I have created from other classes?

For example, imagine you have this:


class AMyGameGameMode : public AGameMode
	FPointSystem* PointSystem;


AMyGameGameMode::AMyGameGameMode(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
	// set default pawn class to our character
	DefaultPawnClass = AMyGameCharacter::StaticClass();
	HUDClass = AMainHUD::StaticClass();
	PointSystem = new FPointSystem();

How do you then go and access the AMyGameGameMode object…

I had a total guessage at it (Obviously it wrong):

AMyGameGameMode* GameMode = Cast<AMyGameGameMode>(AMyGameGameMode::StaticClass());

I see that they seem to be referenced as if they are static classes… How does this work, I find the lack of static declaration confusing if so…

I also tried this:

TSubobjectPtr<AMyGameGameMode> GameMode;
GameMode = PCIP.CreateDefaultSubobject<AMyGameGameMode>(this, TEXT("GameMode"));

But that crashes the loader :smiley:

of course that is wrong, StaticClass is to get the Class for comparaisons and spawning, to get the gamemode, you do this:

AMyGameMode * mymode= Cast<AMyGameMode>(GetWorld()->GetAuthGameMode());

the GetWorld()->GetAuthGameMode(); function gets the current gamemode in any AActor, keep in mind, this wont work on clients on multiplayer game, as clients in a multiplayer game cant access the gamemode becouse the only instance is in the server.

I just tried this before you posted (I thought I was on the right wave length!) but it throws an error:

	AMyGameGameMode* mode = Cast<AMyGameGameMode>(GetWorld()->GetAuthGameMode());

        // Unhandled exception at 0x00007FFCFEB08FC0 (UE4Editor-Engine.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0x0000000000000120.

I’ll keep at it… Most likely something silly i’m doing now… however, if you do know… deffo tell me :smiley: Thanks buddoss!

Might be worth noting i’m trying to access it from within the HUD class I created


Related posts:

Looks like those objects are not in the current scope. Perhaps add a member to your HUD to hold a pointer to (or copy of) the game mode and fill it before entering the current state so you have data to look at.

Isn’t this what I have?.. You can see GameMode is of Type ATofuGameMode… and it’s a property of HUD:

class TOFUGAME_API AMainHUD : public AHUD
	TSubobjectPtr<UHUDElementComponent> TofuPoints;
	virtual void DrawHUD() override;
	ATofuGameGameMode* GameMode;

Then somehow it’s not being set. Look at your screenshot - it’s NULL…

I see in your HUD constructor you try to get the game mode and set it - my guess is that it doesn’t exist at that point.

Yes, that’s correct. It’s not there to grab yet… Hopefully someone knows why :smiley: Thanks for your time though matey :slight_smile:

Okay, I hadn’t quite grasped what vblanco was saying as it’s a new part for me… but apparently it’s not working because I am working on client… which I assume means… my pc? rather than a server… I’ll take a look at these PlayerState and GameState thingies… Any other parent declaration place?

Is your HUD object global? If so, perhaps you can have the game mode register itself with your HUD when it comes up.

You should never try to access objects in the constructor because, as VegasRich pointed out, there is a chance that they haven’t been constructed yet. What you should do is override the BeginPlay function and set the pointer there.

Kumppi… The Hud doesn’t have a BeginPlay, are you thinking of StartPlay with the GameMode? If so, I have moved into this now:

Doesn’t make a difference unfortunately… but thanks for the warning.

ATofuGameGameMode::ATofuGameGameMode(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
	// set default pawn class to our character

void ATofuGameGameMode::StartPlay()
	DefaultPawnClass = ATofuGameCharacter::StaticClass();
	HUDClass = AMainHUD::StaticClass();

void ATofuGameGameMode::StartPlay()
	DefaultPawnClass = ATofuGameCharacter::StaticClass();
	HUDClass = AMainHUD::StaticClass();
        HUDClass->GameMode = this;

maybe? Hopefully?

Unfortunately that will not work bro… Once again though, cheers for having a try!!!

Isn’t AHUD derived from AActor? If so it should have the BeginPlay function. Weird. Well game mode definitely has it.

Kumppi, you’re right!!! It does end up deriving from AActor… Thanks for slapping me down like the noob I am, hangon lemme see if this works now…

Well looooooky here… It no longer crashes… Now I’ve got to get this GameState working instead which I didn’t realise I was supposed to use instead.

hey jimmy, GameMode only exists on the server, so first of all make sure you only call it on the server :slight_smile: and use the command that vblanko showed you. Don’t use it in the constructor like you’re doing since it might not exist yet there, use it in the BeginPlay. I hope it works out for you

My GameState is empty… Any ideas what might be failing?

I am setting the class up in the gamemode:

GameStateClass = AGameState::StaticClass();

and then inside my huds BeginPlay I am calling

UWorld* world = GetWorld();
	GameState = world->GetGameState<AMainGameState>();

GameState = Cast<AMainGameState>(world->GetGameState()); // ?

Yeah I had a problem doing it this way.


So you have to declare the template type:

	GameState = Cast<AMainGameState>(world->GetGameState<AMainGameState>());

and then you may aswell get rid of the Cast…

However, even with the Cast it doesn’t seem to return the GameState set up in the GameMode

Related: ?

errr… I had a typo… YEHAAA IT WORKs… THANKS ALL… Beers for ALL