Casting GameMode returns nullptr

If been struggling to solve this for two days now.
In my gameMode called AGameMode_Cell I got a enum in which I store which of two basic control settings is currently active.
I also got a blueprint derived from it in which I just set that the cursor is visible ingame.

I want to access my gameMode in my character controller through this code AGameMode_Cell* gameMode = Cast<AGameMode_Cell>(UGameplayStatics::GetGameMode(this));
Since I’m a good little coder I check if the cast is successful before continuing but it returns a nullptr every time.
So i checked what GetGameMode() actually returns and turns out it is of the type GameMode_Cell_C which is seemingly incompatible with AGameMode_Cell.

I’ve been trying other methods of casting it and also tried doing it completly in the character controller blueprint but it also didn’t work.

It would be fantastic if someone could help me with this problem. It’s been driving me crazy for like two days now.

Hello!

  1. did you using that gamemode or that gamemode is configured as default gamemode in world settings? ( Game Mode and Game State in Unreal Engine | Unreal Engine 5.1 Documentation )
  2. is this a multiplayer game and you try maybe get gamemode on client? Because on client gamemode is not exist.
  3. you can use AGameMode_Cell* gameMode = Cast<AGameMode_Cell>(()->GetAuthGameMode());

It is my default GameMode in that world.

I didn’t change anything from the default settings of the empty project, so I assume it is single player.

I used GetAuthGameMode() with the exact same result.

and you inherited a new bp gamemode class from your gamemode?

Yes.
I created it with a right click on the c++ class and used ‘create bp class derived from this’ (or whatever it says)

Is this multiplayer game?

It isn’t.
If the default empty game isn’t then it isn’t.

ok do this command in console GetAll GameMode it should display which GameMode class is on

I tried that command and the console didn’t output anything. So I tried it with ()->GetAuthGameMode()->GetClass()->GetName(). That returned GameMode_Cell_C

!!>First of all: I changed the bleuprint name to GameMode_Cell_BP because I tried something and wanted to stick to naming conventions which I hand’t previously. It shouldn’t affect this problem.

GameMode_Cell_C means that is a blueprint class… so thats made from AGameModeCell, so basically you can cast that to AGameModeCell

Can you paste the whole code and some screenshot about your world config / gamemode bp?

So now the returned GameMode is GameMode_Cell_BP_C

I’*ll recreate the whole blueprint step by step and if it works, fine, if not I’ll find a different place to store the control settings.

oh lol wait a sec… try to do this with delay node… maybe since begin play can run “fast” and world creating is not finished in editor

Oh come on… Adding a timer in code actually worked.

well sometimes references just simply not set when beginplay happens… since when you play in editor everything happens immediatly… and actor life cycles may spawn player / controller before world set his gamemode…

I will consider this in future whem something like this happens.