Game crashes after implementation of Tick function

Can you run this by playing from Visual Studio, so when the exception is thrown it shows you exactly where it crashes?

FollowCamera could be null.

The problem is that, on the last line, you are using

Controller->CurrentInteractable = nullptr;

But you never check for a not null variable (the check is on an if, but this is outside the scope of that), so what happens is that you are doing this

Controller is null => nullptr->CurrentInteractable = nullptr //ACCESS VIOLATION

The solution is to simply use this

if(Controller)
    Controller->CurrentInteractable = nullptr;

or simply, after you CAST the controller, return if is not the type you need (because you cannot do anything else). On the very first line of code put this:

//Early out
AGameplayController* Controller = Cast<AGameplayController>(GetController());
if(!Controller)
    return;

Cheers!

Hi !

I’m following a tutorial in YouTube to create an inventory system ( here, it’s the tutorial : Crafting/Inventory System Tutorial in Unreal Engine - #1 - YouTube).

I’m in the second video and after implementing the function to detect if we have an object in front of us and put it in Tick, my game crash. I do not really understand what I have done wrong since I am the tutorial step by step (twice same) and I am always confronted with this problem. So I ask for your help because I am a bit overwhelmed.

Here is the code of the functions in question :

void AElyosaCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	CheckForInteractables();
}

void AElyosaCharacter::CheckForInteractables()
{

	FHitResult HitResult;

	FVector StartTrace = FollowCamera->GetComponentLocation();
	FVector EndTrace = (FollowCamera->GetForwardVector() * 300) + StartTrace;

	FCollisionQueryParams QueryParams;
	QueryParams.AddIgnoredActor(this);

	AGameplayController* Controller = Cast<AGameplayController>(GetController());


	if (GetWorld()->LineTraceSingleByChannel(HitResult, StartTrace, EndTrace, ECC_Visibility, QueryParams) && Controller)
	{

		//Check if the item we hit was a interactable item
		if (AInteractable* Interactable = Cast<AInteractable>(HitResult.GetActor()))
		{
			Controller->CurrentInteractable = Interactable;
			return;
		}

	}

	//If we didn't hit anything, or the thing we hit was not a interactable, set the currentinteractable to nullptr
	Controller->CurrentInteractable = nullptr;

}

I would like to make it work but more important for me, to understand why there is this problem ! I really want to understand after all the hours I spent on it.

Maybe you need more information. If so, ask me what you need and I will reply as soon as possible !

Thank you for taking the time to read this message! You are awesome and I hope you can help me!

Hi,

Thank’s for your fast reply and your clear explication !

So I did what you told me to do and the program stop at this line Controller->CurrentInteractable = nullptr; with the exception : Access violation writing (It is a translation may be bad, I forgot to change the language of my IDE in English). I don’t really understand why because this variable is public…

Thank you for your time and attention, that’s it! I can continue my tutorial, our explanations are genial !

Have a good day and thank you again!