I have a variable in the GameMode which the PlayerController Gets and the Character can get this variable from the PlayerController but i keep getting crashes
the crash log is
Unhandled Exception: EXCEPTION_ACCESS_VIOLATION writing address 0x000006b4
GameMode .h
UPROPERTY(EditAnywhere)
int32 GMVariable;
int32 GetGamemodeVariable();
GameMode cpp
int32 AShooterGameMode::GetGamemodeVariable()
{
return GMVariable;
}
PlayerController .h
class AShooterGameMode* GetGameMode;
UPROPERTY()
int32 SelectedVariable;
UFUNCTION()
int32 PCVariable();
PlayerController cpp
int32 AShooterPlayerController::PCVariable()
{
SelectedVariable = GetGamemode->GetGamemodeVariable();
return SelectedVariable;
}
Character .h
class AShooterPlayerController* ShooterPC;
UPROPERTY()
int32 CharacterVariable;
Character cpp
void AShooterCharacter::GetPCVariable()
{
CharacterVariable = ShooterPC->PCVariable();
}
this is all the related code
how would i set GetGamemode and ShooterPC and how can i protect the pointers
How are you setting GetGamemode and ShooterPC? Are you protecting your pointers?
this is all the related code
No, it’s not all the related code.
What is ShooterPC? I assume is the player controller. So, where are you setting its value? The same for the game mode.
how would i set GetGamemode and ShooterPC
One of the ways to get the player controller:
AShooterPlayerController* ShooterPC = Cast<AShooterPlayerController>(UGameplayStatics::GetPlayerController(GetWorld(), 0));
how can i protect the pointers
Example:
if (ShooterPC)
{
CharacterVariable = ShooterPC->PCVariable();
}
Thanks this helped heaps
This is how i am currently doing it and its working
if (AShooterGameMode* GM = Cast<AShooterGameMode>(UGameplayStatics::GetGameMode(GetWorld())))
{
if (GM)
{
CurrentVariable = GMM->GetVariable();
FString IntAsString = FString::FromInt(CurrentVariable);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, IntAsString);
}
}
Remove the first if. It’s useless. Just:
AShooterGameMode* GM = Cast<AShooterGameMode>(UGameplayStatics::GetGameMode(GetWorld());
if (GM) ...
thanks for that optimization advise