How to use Game Mode in a game (nullptr exception when I use InitGameState)

Hi!

I have no idea about how to use the GameMode. I know it purposes, but I don’t know how to integrate it in my game.

To clarify me, is this the way I have to use the GameMode class?

void ATTetrisGameModeBase::InitGameState()
{
    Super::InitGameState();

    TObjectPtr<ATGameStateBase> TetrisGameState = GetGameState<ATGameStateBase>();
    TetrisGameState->Init();
}

In the Init() method I do this:

void ATGameStateBase::Init()
{
	BlockCounter = 0;
}

But I get a this is a nullptr exception in the line BlockCounter = 0;

By the way, I set a derived Blueprint class of this one in the World Settings of the level:

Thanks.

You need to set it in your game’s project settings => Maps and modes => in the game mode area choose your c++ variant

Thanks, but I’m not asking that. I’m asking how I can use their methods in a game, how I can integrate it in a game, etc.

InitGameState is called by the game mode. You don’t call it manually. You can setup game state default parameters in the function.

Not sure what more you would need to know.

I’m not calling manually.

Sorry didnt notice the rest of the code on my phone

Is BlockCounter marked as a uproperty in the gamestate header file?
Im guessing its an int32 yes?

Also does TetrisGameState pass a nullptr check? It seems unlikely that an int could be a null ptr as its a direct variable.

Sorry. This is how it is declared:


private:
	uint8 BlockCounter = 0;

Do you have your GameState class set correctly in your game mode?

Clearly the cast fails in TObjectPtr<ATGameStateBase> TetrisGameState = GetGameState<ATGameStateBase>();, which means that the game state (which has already been spawned by then) is not a ATGameStateBase

Also BeginPlay is good for initialization
And the gamestate knows about the game mode, so if you wanted to get info from the game mode you can do it in that direction