Casting to Game Instances C++

I am Having problem when trying to cast to my Gamesinstance inside my player characters sprites.

void AFYP_MazeCharacter::StartSprint()
{

UFYP_GameInstance* GM = Cast<UFYP_GameInstance>(UGameplayStatics::GetGameInstance());

if (IsValid(GM))

	{
		GetCharacterMovement()->MaxWalkSpeed = SprintSpeed \* GM->MovementSpeed; 	
	}

}

With the Cast<UFYP_GameInstance>(UGameplayStatics::GetGameInstance()) with my logs saying that UGameplayStatics::GetGameInstance function doesnt take 0 argument which i dont know what it means

UGameplayStatics::GetGameInstance() takes a parameter, UObject* WorldContextObject, because of reasons related to multiplayer that aren’t particularly important. All that really matters is that you need to provide some object that knows what World/level it’s in, to get to the right GameInstance (because there could be many). You’ll see this parameter in a lot of static functions.

In your case, with an actor member function, you can go with the trivial option of passing this as the World Context. So:
UFYP_GameInstance* GM = Cast<UFYP_GameInstance>(UGameplayStatics::GetGameInstance(this));

Compiler errors are not a log. You’ll need to get comfortable reading & understanding compiler and linker errors if you’re going to write C++. Otherwise you’re progress will be deathly slow if you have to go to the internet for every one. Not a judgement, it sounds like you’re new to C++ and that’s great. Welcome to the dark side! Just something to keep in mind as you move forward.

1 Like

i think it needs a way to get a WorldContextObject.

ive not used sprites before but maybe you can pass an ‘owner’ through