How to Initialize the object references in C++?

Hi, I want to Initialize the object references in C++ , how to do it?
The blueprint you see is working fine… I want to do the same using C++

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;

.cpp BeginPlay

    ACharacter* CharacterRef{};
	AMyCharacter* MyCharacterRef{};
	APlayerController* ControllerRef{};
	AMyPlayerController* MyControllerRef{};

	CharacterRef = UGameplayStatics::GetPlayerCharacter(this, 0);
	MyCharacterRef = Cast<AMyCharacter>(CharacterRef);
	if (MyCharacterRef != nullptr)
	{
		MainCharacterRef = MyCharacterRef;
	}
	else
	{
		return;
	}

	ControllerRef = UGameplayStatics::GetPlayerController(this, 0);
	MyControllerRef = Cast<AMyPlayerController>(ControllerRef);
	if (MyControllerRef != nullptr)
	{
		MainControllerRef = MyControllerRef;
	}
	else
	{
		return;
	}

What happens when you call return? :stuck_out_tongue:

2 Likes

if the cast failed, it should call return before trying the next block of code… I don’t want to continue the program after cast failed.

Try this:

// .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.
}

More info:
https://nerivec.github.io/old-ue4-wiki/pages/forward-declarations.html

1 Like

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.

yes the CharacterRef is nulled and the engine crashed

the code compiles fine with editor closed , the problem is the cast is failed and the MyCharacterRef is always nullptr

this cause this issue too: Issue Caused by this Nullptr

…try AMyCharacter* CharacterRef{} instead of ACharacter* CharacterRef{}. Or rather, do the Cast and the UGameplayStatics at the same time.

1 Like

It was my first try, it was failed too.

	CharacterRef = Cast<AMyCharacter>(UGameplayStatics::GetPlayerCharacter(this, 0));
	if (IsValid(CharacterRef))
	{
		LogScreen("CharacterRef is casted to My Character");
	}

Which class are you calling the BeginPlay in?

AMyCharacter The same class in which the whole functionality exits.

Try double checking if your character set in the game mode really is AMyCharacter or a derived class.

2 Likes

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;
	}
}

Ensure that the GameMode is set correctly, then ensure that the default pawn class is set properly, and then start checking the character class.

already ensured everything is set up correctly, everything is working correctly writing the same logic in blueprints working fine

I can’t replicate any of the issues trying it out myself, so I’m afraid I can’t help you at this point.

Its Ok, but write if you will have a new solution or idea :slight_smile:

Thank You Sir for the working solution, You just solved the issue :slight_smile:

try this on reverse method:

	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Default")
	AYourCharacter* CharcterRef;
ACharacter* GetPlayerCharacterNodeReturn{};
AYourCharacter* As_My_Character{};

GetPlayerCharacterNodeReturn= UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);
As_My_Character= Cast<AYourCharacter>(GetPlayerCharacterNodeReturn);
if (As_My_Character == nullptr) { return; }
else { CharcterRef = As_My_Character; }

the same method should work for casting controller as wel. Cheers! Hope it helps.

2 Likes

How to Initialize the object references in C++? - #20 by c0r37py

Thank You Sir for the working solution, You just solved the issue :slightly_smiling_face:

2 Likes