What I am trying is: but I am failed to achieve this.
.h
//Defining the refference to My Character
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Refference Related")
AMyCharacter* MainCharacterRef;
//Defining the refference to My Controller
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Refference Related")
AMyPlayerController* MainControllerRef;
// .h
// Forward declare custom character class
class AGameAIByExampleCharacter* CharacterRef = nullptr;
// .cpp
// Include custom character class
#include "GameAIByExample/GameAIByExampleCharacter.h"
// GameplayStatics
#include "Kismet/GameplayStatics.h"
void ARefTest::BeginPlay()
{
Super::BeginPlay();
// Get pointer to player character.
CharacterRef = Cast<AGameAIByExampleCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
// Check if cast succeeded and is not nullptr.
checkf(CharacterRef, TEXT("RefTest: CharacterRef = nullptr.")); // This will crash the program and display the text message in the error if CharacterRef == nullptr.
}
I glanced over it too quickly, my bad. Anyhow, I don’t see anything particularly wrong, in that even though the syntax for the pointer declarations is something i’ve yet to see in unreal, it shouldn’t be a problem.
I don’t know which class in question this is but you’re not using the forward declaration as pointed out.
If you haven’t, try compiling the code with the editor closed.
CharacterRef = Cast<AMyCharacter>(UGameplayStatics::GetPlayerCharacter(this, 0));
if (IsValid(CharacterRef))
{
LogScreen("CharacterRef is casted to My Character");
}
it looks like this, that bp character class is deriving from the C++ AMyCharacter Class
AMyGameModeBase::AMyGameModeBase()
{
// set default pawn class to our Blueprinted character
static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/Blueprints/Characters/MaleCharacter"));
if (PlayerPawnBPClass.Class != NULL)
{
DefaultPawnClass = PlayerPawnBPClass.Class;
}
}