# this was nullptr when trying to get coordinates

**URL:** <https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838>\
**Category:** AI\
**Tags:** ai-navigation, Behavior-Tree, behavior-tree-task, unreal-engine, CPP, question\
**Created:** [October 22, 2024, 9:05am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838 "2024-10-22T09:05:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![M4yh3m.exe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@M4yh3m.exe](https://forums.unrealengine.com/u/M4yh3m.exe)\
**Post date:** [October 22, 2024, 9:05am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838/1 "2024-10-22T09:05:00Z")

</div>

Hello everyone, I’m making a multiplayer game and at the moment I’m trying to implement the movement of a character on the ceiling, I chose the simplest option to just flip and raise the mesh to the ceiling, and leave the capsule in its place and at the moment I’m creating a task for the behavior tree in which it will do all this, but I have a problem when I try to release LineTrace to the ceiling to get its coordinates: this was nullptr

---

<div class="post-metadata">

**Author:** ![M4yh3m.exe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@M4yh3m.exe](https://forums.unrealengine.com/u/M4yh3m.exe)\
**Post date:** [October 22, 2024, 9:07am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838/2 "2024-10-22T09:07:43Z")

</div>

Task\_GoToCeiling.h

```auto
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "BehaviorTree/BTTaskNode.h"
#include "ZombieEnemyCPP.h"
#include "Task_GoToCeiling.generated.h"

/**
 * 
 */
UCLASS()
class FIRSTGAME_API UTask_GoToCeiling : public UBTTaskNode
{
	GENERATED_BODY()

private:

	virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& OwnerComp,
		uint8* NodeMemory) override;
	FHitResult HitResult;

public:

	AZombieEnemyCPP* Zombie = Cast<AZombieEnemyCPP>(UGameplayStatics::GetPlayerController(GetWorld(), 0));

	
};

```

Task\_GoToCeiling.cpp

```auto
#include "Task_GoToCeiling.h"
#include "LineTrace.h"
#include "Kismet/GameplayStatics.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "BTAIController.h"
#include "ZombieEnemyCPP.h"

EBTNodeResult::Type UTask_GoToCeiling::
ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
	FVector StartCapsule = GetWorld()->GetFirstPlayerController()->
		GetCharacter()->GetCapsuleComponent()->GetComponentLocation();
	FVector EndCapsule = StartCapsule + GetWorld()->GetFirstPlayerController()->
		GetCharacter()->GetCapsuleComponent()->GetComponentLocation() * 400.0f;

	HitResult = Zombie->TraceCeiling(StartCapsule, EndCapsule, HitResult);

	/*FHitResult HitResult = GetWorld()->GetFirstPlayerController()->
		GetCharacter()->GetComponentByClass()->LineTraceSingle(StartCapsule, EndCapsule, true);*/
	if (HitResult.bBlockingHit && HitResult.GetComponent()->ComponentHasTag("Ceiling"))
	{
		FRotator CeilingRotation = (FRotator(180.0f, -90.0f, Zombie->GetControlRotation().Yaw));

		Zombie->GetMesh()->K2_SetWorldLocationAndRotation(HitResult.Location, CeilingRotation, true, HitResult, true);

	}
	else
	{
		return EBTNodeResult::Failed;
	}

	return EBTNodeResult::Succeeded;
}

```

ZombieEnemy.cpp

```auto
FHitResult AZombieEnemyCPP::TraceCeiling(FVector Start, FVector End, FHitResult HitResult)
{
	HitResult = this->LineTraceComp->LineTraceSingle(Start, End, true); //nullptr here
	
	return HitResult;
}

```

---

<div class="post-metadata">

**Author:** ![M4yh3m.exe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@M4yh3m.exe](https://forums.unrealengine.com/u/M4yh3m.exe)\
**Post date:** [October 22, 2024, 9:09am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838/3 "2024-10-22T09:09:37Z")

</div>

The code is not the most accurate due to my attempts to do it myself and fix other errors, I apologize in advance

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [October 22, 2024, 9:15am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838/4 "2024-10-22T09:15:52Z")

</div>

Hi, try replacing the “\*400.0f” with “-FVector(0,0,400.0f)”

---

<div class="post-metadata">

**Author:** ![M4yh3m.exe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@M4yh3m.exe](https://forums.unrealengine.com/u/M4yh3m.exe)\
**Post date:** [October 22, 2024, 9:26am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838/5 "2024-10-22T09:26:13Z")

</div>

same problem

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/1/3/f13adbe0e0df158dc4d087f1a3db0b6f2a09f2a7.png)

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [October 22, 2024, 9:28am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838/6 "2024-10-22T09:28:36Z")

</div>

Maybe try making the value larger, say 2000.0f.

If this still doesn’t return anything, it may help to visualize the trace with one of the two options mentioned in this post:

> [@Draw debug lines for line tracing](https://forums.unrealengine.com/t/draw-debug-lines-for-line-tracing/280307):
>
> Hi guys, I am using the GetWorld()-\>LineTraceSingle() function in CPP code to trace some things. It would be very handy for me to actually see some debug lines drawn by an engine, while a tracing happens. In Blueprints, it is easily done with DrawDebugTrace checkbox, which is present in all the tracing nodes. Is it possible to enable it in C++?

---

<div class="post-metadata">

**Author:** ![M4yh3m.exe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@M4yh3m.exe](https://forums.unrealengine.com/u/M4yh3m.exe)\
**Post date:** [October 22, 2024, 9:36am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838/7 "2024-10-22T09:36:46Z")

</div>

It seems I didn’t describe the error quite correctly, when I call the same function from the NPC class it works fine, but I need to register this function in Task and there is a problem with nullptr, it seems to me it can’t get LineTraceComp which is in my AI class

---

<div class="post-metadata">

**Author:** ![M4yh3m.exe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@M4yh3m.exe](https://forums.unrealengine.com/u/M4yh3m.exe)\
**Post date:** [October 22, 2024, 9:42am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838/8 "2024-10-22T09:42:41Z")

</div>

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/5/6/456efee63796e127255e74ddb43e9b639c2f67e4.png)

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [October 22, 2024, 9:43am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838/9 "2024-10-22T09:43:34Z")

</div>

I see - can you show the code which sets the LineTraceComp variable?

---

<div class="post-metadata">

**Author:** ![M4yh3m.exe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@M4yh3m.exe](https://forums.unrealengine.com/u/M4yh3m.exe)\
**Post date:** [October 22, 2024, 9:45am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838/10 "2024-10-22T09:45:17Z")

</div>

ZombieEnemyCPP.h

```auto
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

class ULineTrace* LineTraceComp;

```

ZombieEnemyCPP.cpp

```auto
AZombieEnemyCPP::AZombieEnemyCPP()
{
	LineTraceComp = CreateDefaultSubobject<ULineTrace>("LineTraceComponent");
	
}

```

---

<div class="post-metadata">

**Author:** ![M4yh3m.exe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@M4yh3m.exe](https://forums.unrealengine.com/u/M4yh3m.exe)\
**Post date:** [October 22, 2024, 9:46am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838/11 "2024-10-22T09:46:54Z")

</div>

LineTrace.h

```auto
#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "LineTrace.generated.h"

UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class FIRSTGAME_API ULineTrace : public UActorComponent
{
	GENERATED_BODY()

public:

	ULineTrace();

protected:
	// Called when the game starts
	virtual void BeginPlay() override;

public:
	FHitResult LineTraceSingle(FVector Start, FVector End);
	FHitResult LineTraceSingle(FVector Start, FVector End, bool ShowDebugLine);
	FHitResult SphereTrace(FVector Start, FVector End);

};

```

LineTrace.cpp

```auto
#include "LineTrace.h"
#include "Kismet/KismetSystemLibrary.h"
#include "Engine/Engine.h"
#include "DrawDebugHelpers.h"
#include "Kismet/GameplayStatics.h"
#include <Kismet/KismetMathLibrary.h>
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/Actor.h"

// Sets default values for this component's properties
ULineTrace::ULineTrace()
{

}

// Called when the game starts
void ULineTrace::BeginPlay()
{
	Super::BeginPlay();
}

FHitResult ULineTrace::LineTraceSingle(FVector Start, FVector End)
{
	FHitResult HitResult;
	FCollisionObjectQueryParams CollisionParams;
	FCollisionQueryParams CollisionQueryParams;
	CollisionQueryParams.AddIgnoredActor(GetOwner());

	if (GetWorld()->LineTraceSingleByObjectType(
		OUT HitResult,
		Start,
		End,
		CollisionParams,
		CollisionQueryParams
	))
	{
		return HitResult;
	}
	else
	{
		return HitResult;
	}

}

FHitResult ULineTrace::LineTraceSingle(FVector Start, FVector End, bool ShowDebugLine)
{
	FHitResult Actor = LineTraceSingle(Start, End);
	if (ShowDebugLine) {
		DrawDebugLine(
			GetWorld(),
			Start,
			End,
			FColor::Red,
			false,
			3.0f,
			0,
			5.0f
		);
	}

	return Actor;
}

```

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [October 22, 2024, 9:53am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838/12 "2024-10-22T09:53:35Z")

</div>

I can’t see any issues with that - what about the “Zombie” variable? it’s the “this” that is NULL

---

<div class="post-metadata">

**Author:** ![M4yh3m.exe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@M4yh3m.exe](https://forums.unrealengine.com/u/M4yh3m.exe)\
**Post date:** [October 22, 2024, 9:58am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838/13 "2024-10-22T09:58:28Z")

</div>

It seems to me the problem is that I cant get the zombie itself that is in the world, at the moment the Zombie is Cast(UGameplayStatics::GetPlayerController(GetWorld(), 0)); , but I dont know how to get an existing actor and at the same time access its LineTraceComp to check for a ceiling above it

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [October 22, 2024, 10:09am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838/14 "2024-10-22T10:09:09Z")

</div>

Sorry, I’ve been sitting here laughing at myself - it reminds me of the Gag where one person is telling another about a person named “Hue” - who? Hue! yeah, that’s what I’m asking - who? etc 😆 I think I need some sleep.

Rather than set Zombie in the header there (as it’s most probably a nullptr at that point) try setting it as the first line in the ExecuteTask function.

---

<div class="post-metadata">

**Author:** ![M4yh3m.exe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@M4yh3m.exe](https://forums.unrealengine.com/u/M4yh3m.exe)\
**Post date:** [October 22, 2024, 10:13am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838/15 "2024-10-22T10:13:21Z")

</div>

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/2/1/4213c4f756948fb46a7c006e8b77e5f73e7a71c0.png)  
Like that?

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [October 22, 2024, 10:18am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838/16 "2024-10-22T10:18:20Z")

</div>

yeah, but if you’ve still got the Zombie variable in your header you can just put

```auto
Zombie = Cast<...

```

---

<div class="post-metadata">

**Author:** ![M4yh3m.exe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@M4yh3m.exe](https://forums.unrealengine.com/u/M4yh3m.exe)\
**Post date:** [October 22, 2024, 10:26am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838/17 "2024-10-22T10:26:31Z")

</div>

it didn’t work, is there any way i can get it from GetWorld()?

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [October 22, 2024, 10:35am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838/18 "2024-10-22T10:35:05Z")

</div>

You can use:

```auto
Zombie=(AZombieEnemyCPP*)UGameplayStatics::GetActorOfClass(AZombieEnemyCPP::StaticClass());

```

if there’s only one in the level, or you can do:

```auto
TArray<AActor*> zombieList;
UGameplayStatics::GetAllActorsOfClass(AZombieEnemyCPP::StaticClass(),zombieList);

```

if there are more than one, but then you’ll need to find which one it is that you’re wanting to move to the ceiling - maybe with a proximity test.

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [October 22, 2024, 10:46am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838/19 "2024-10-22T10:46:13Z")

</div>

Anyway I’m going to have to tap out now and get some sleep - it’s been an almost 18hour work day.

I hope you manage to get it working!

---

<div class="post-metadata">

**Author:** ![M4yh3m.exe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@M4yh3m.exe](https://forums.unrealengine.com/u/M4yh3m.exe)\
**Post date:** [October 22, 2024, 10:47am UTC](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838/20 "2024-10-22T10:47:00Z")

</div>

Ok, thanks for your help, I’ll write here if I find a solution

[Next page](https://forums.unrealengine.com/t/this-was-nullptr-when-trying-to-get-coordinates/2079838.md?page=2)
