C++ Get Game Mode

APacman_TestGameModeBase * GameMode= (APacman_TestGameModeBase *)GetWorld()->GetAuthGameMode();

Try this.

1 Like

Hello everyone, I am currently trying to convert a blueprint project into C++ code and got stuck trying to get the game mode and cast it as an object reference in C++.

The image below is the current blueprint implementation of casting and getting the game mode.

This image below shows my attempt in trying the get the game mode in the same way but not sure what is the exact input, syntax, or includes which I need in order to successfully obtain the game mode similar with the blueprint.

Any help and suggestions is appreciated.

Thanks, it’s working nicely.

For everybody who looks into this thread - do not cast objects like in the answer above. It’s an old C-style cast, that is wrong in many ways (google it if curious).

Use Cast<> or StaticCast<> (when you’re absolutely sure about your object’s class).

Cheers

3 Likes

An alternative method to getting the game mode is a function within the Gameplay Static Library:

#include "Kismet/GameplayStatics.h"

MyGameMode* MyMode = Cast< MyGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
4 Likes

If Possible,
Can you Explain. What UGameplayStatics::GetGameMode Dose?

Might not be the level of detail if you want.
But it pretty much does what it says! :smiley:

UGameplayStatics is basically a library which holds a load of useful methods, such as GetGameMode, and by passing in the world, it gets you the game mode of that world.

If you hover over GetGameMode in blueprint it actually says “Target is Gameplay Statics”

somehow dynamic_cast doesn’t do what I expect. My game mode is a Blueprint that inherits from AGameModeAsteroids and in C++, I want to cast to the parent class of that blueprint.

Reading on the topic, in C++ dynamic_cast appeared to be the safest option. But I always get a null pointer:

AGameModeAsteroids* UFunctionLib::GetGameModeAsteroids(const UObject* WorldContextObject)
{
    const auto GameMode = dynamic_cast<AGameModeAsteroids*>(UGameplayStatics::GetGameMode(WorldContextObject));
    if(!GameMode)
    {
        UE_LOG(LogTemp, Error, TEXT("Game mode must implement `AGameModeAsteroids`"));
        //RequestEngineExit(TEXT("Game mode must implement `AGameModeAsteroids`"));
    };
    return GameMode;
}

Any idea what’s the problem here?

EDIT:

Turns out, all of Cast, StaticCast, and dynamic_cast have the same effect: I get a null pointer.

I set the game mode in the project settings to BP_GameModeAsteroids and I verified that that one inherits from AGameModeAsteroids, which in turn inherits from AGameModeBase.

I also set the game mode override in the world settings (which shouldn’t even be necessary). Still the editor tells me: null pointer, whenever I try to get the game mode and cast it to AGameModeAsteroids.

And the question remains: are there circumstances where the additional machinery invoked by dynamic_cast is worthwhile?

Extra weirdness on top: running the game stand-alone seems to work. Simulation and editor don’t.

It’s actually well documented: GameMode is only available on the server.

I don’t know where I am in my sample project, but I suppose I am programming a client that doesn’t have access. Now I am reading up on the topic here

Very offtopic but I c style casted to my custom game instance without defining it in the DefaultEngine.ini file and got all these weird editor crashes when i stopped the game.

It is very dangerous because the cast will work even when there are errors and you just dont associate the upcoming exceptions with the previous casting.

Cast<> fixed it for me, thank you very much.

1 Like