A problem with pointer?

Hello,

I’ll describe my project in few words. I got GameManager class (which derives from AActor) and custom player controller with input component setup and pointer to GameManager. Pointer is set by GameManager with custom method inside player controller (and it exists - I’ve double checked).

After key press I’m invoking method on GameManager and here is a problem. If there is anything inside method in GameManager (besides UE_LOG macro) my editor always crashes (even with compile success).

Do You know what could cause a problem?

It sounds like you’ve got a null reference or similar, but it’s hard to say without seeing the code. Can you post it?

Add UPROPERTY to your pointer, or garbage collector will not know about your class storing and actor reference and will remove it.

UPROPERTY()
AGameManager* GameManager;

Here is GameManager’s method responsible for setting pointer in PlayerController:

void AGameManager::SetPointerInPlayerController()
{
	ADiamondsPlayerController* controller = Cast<ADiamondsPlayerController>(GetWorld()->GetFirstPlayerController());

	controller->SetGameManager(this);
}

It’s invoked by timer set to 0.1 sec to be sure that PlayerController is already created by the engine (GameManager is placed by me in scene).

Here is header of PlayerController:

UCLASS()
class DIAMONDS_API ADiamondsPlayerController : public APlayerController
{
	GENERATED_BODY()

	AGameManager* GameManager;

public:
	ADiamondsPlayerController();

	virtual void SetupInputComponent() override;

	void RotateLeft();
	void RotateRight();
	
	void SetGameManager(AGameManager* gameManager);
};

And source:

ADiamondsPlayerController::ADiamondsPlayerController()
{
	bEnableTouchEvents = true;
	bShowMouseCursor = true;
}

void ADiamondsPlayerController::SetupInputComponent()
{
	Super::SetupInputComponent();

	check(InputComponent);

	InputComponent->BindAction("RotateLeft", IE_Pressed, this, &ADiamondsPlayerController::RotateLeft);
	InputComponent->BindAction("RotateRight", IE_Pressed, this, &ADiamondsPlayerController::RotateRight);
}

void ADiamondsPlayerController::RotateLeft()
{
	GameManager->RotateLeft();
}

void ADiamondsPlayerController::RotateRight()
{
	GameManager->RotateRight();
}

void ADiamondsPlayerController::SetGameManager(AGameManager* gameManager)
{
	GameManager = gameManager;
}

Here are methods invoked through pointer in PlayerController:

void AGameManager::RotateLeft()
{
	UE_LOG(LogTemp, Warning, TEXT("Left"));
}

void AGameManager::RotateRight()
{
	UE_LOG(LogTemp, Warning, TEXT("Right"));
}

If I start now it works and prints proper texts so pointer works. If I put anything else in above two methods it compiles properly but crashes all the time.

Apparently it helped !!! Thank You very much !!!