Download

Error casting GameState

Hi everyone,

I’m trying to do exactly the same done in the shootergame.

In myHUD.cpp I try to cast to my gamestate and it’s not working/
Can someone help me please ?

thanx.

here is my code:


void ATactical7DecHUD::DrawGameTimerAndPosition()
{
    ATacticalGameState*  const MyGameState = Cast<ATacticalGameState>(GetWorld()->GameState);
}

What is the actual error you are getting?
Or is it just a nullptr?

Try


void ATactical7DecHUD::DrawGameTimerAndPosition()
{

ATacticalGameState* const MyGameState = Cast<ATacticalGameState>(UGameplayStatics::GetGameState(GetWorld()));


} 

Also, check to make sure that your GameMode subclass has



GameStateClass = ATacticalGameState::StaticClass();

in its constructor (or it’s otherwise correctly overrode). The cast will fail if the wrong gamestate was spawned, because you can’t cast it into something it’s not.

So good, thank you very much it works very well.