Hi, you don’t need to cast it like that, remember that only one GameMode class exists at a time , but there can be several characters and casting wouldn’t really help you would it?, so how can you get a reference to your character?
Well in general when a game is playing you can simply “GetGameMode” from any other c++ class (or blueprint class)…
So for example you would go to your characer.cpp class and add :
void AMyCharacter::BeginPlay()
{
Super::BeginPlay();
GameModeReference = (AMyGameMode*)GetWorld()->GetAuthGameMode();
}
Of course you also need to add a variable to character.h class:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Gameplay)
class AMyGameMode* GameModeReference;
If your character is a blueprint you would do the same thing in your blueprint,
you can simply GetGameMode and store reference to it into a GameModeVariable (object type).
Or you can Cast it like this (Read my Answer - by “Jurif” on the bottom):
Ok, now that you have a reference to your GameMode, you could simply do this, in your character BeginPlay function you would add a line to store reference of self(this) to a GameMode variable :
void AMyCharacter::BeginPlay()
{
Super::BeginPlay();
GameModeReference = (AMyGameMode*)GetWorld()->GetAuthGameMode();
GameModeReference->ReferenceToMyCharacter->this;
}
In order for that to work we first need an appropriate variable in our GameMode.h class:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Gameplay)
class AMyCharacter* ReferenceToMyCharacter;
So there you go , this way at the Begin Play your character will get a reference to gameMode and store it into a variable, and then also send reference to self back and store it to GameMode->ReferenceToMyCharacter variable.
Now you have references on both ends, and you can use this method on all classes.
Now to wrap this up , in GameMode.cpp you are spawning a blueprint, it should look like this:
static ConstructorHelpers::FObjectFinder <UBlueprint> MyCharObject(TEXT("/Game/MyChar/Blueprints/MyCharacterBP"));
if ( MyCharObject.Object != NULL)
{
MyCharacterBP = (UClass*) MyCharObject.Object->GeneratedClass;
}
And of course you need a variable in the .h class :
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category =MyAwesomeVariables)
UClass* MyCharacterBP;
There you go, good luck