EQS test not running when called using C++

Hello,

I created myself an EQS test using the EQS editor. It’s a simple GetAllActorsOfClass Generator.
It works nicely enough with my EQS testing pawn and even runs when called from a Blueprint based behavior tree task using the RunEQS node.

What I wanted to do was create the same call to the EQS test from a C++ class (since I find C++ programming easier). I found this thread which pointed me in the right direction: How to use EQS in c++ - AI - Epic Developer Community Forums

However, when I repeat the same steps, my call back function returns with zero results. When I use the Gameplay Debugger, it displays zero EQS queries being executed. Here is the relevant code:
The .h file:

    UCLASS()
    class TEST_API UBTTask_GetAllActorsInRadius : public UBTTaskNode
    {
    	GENERATED_BODY()
    	
    private:
    	ABotContr* ThisBot;
    
    public:
    	UBTTask_GetAllActorsInRadius();
    
    	UPROPERTY(BlueprintReadWrite, Category = EQS)
    		UEnvQuery* EnemySeekerQuery; // set the query in editor
    	FEnvQueryRequest EnemySeekerQueryRequest;
    
    	// The function that gets called when querry finished
    	void EnemySeekerQueryFinished(TSharedPtr<FEnvQueryResult> Result);
    
    	virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory);
    };

And my .cpp file:

UBTTask_GetAllActorsInRadius::UBTTask_GetAllActorsInRadius()
{
	static ConstructorHelpers::FObjectFinder<UEnvQuery> EQS_Query(TEXT("EnvQuery'/AI/EQS/EnemiesInArea_EQS.EnemiesInArea_EQS'"));
	EnemySeekerQuery = EQS_Query.Object;

	if (EnemySeekerQuery != nullptr)
	{
		EnemySeekerQueryRequest = FEnvQueryRequest(EnemySeekerQuery, this);
	}
}

EBTNodeResult::Type UBTTask_GetAllActorsInRadius::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
	ThisBot = Cast<ABotContr>(OwnerComp.GetAIOwner());

	EnemySeekerQueryRequest.Execute(EEnvQueryRunMode::AllMatching,
		this,
		&UBTTask_GetAllActorsInRadius::EnemySeekerQueryFinished);

	return EBTNodeResult::Succeeded;
}

void UBTTask_GetAllActorsInRadius::EnemySeekerQueryFinished(TSharedPtr<FEnvQueryResult> Result)
{
	UE_LOG(LogTemp, Warning, TEXT("C++ EQS Completed"));

	TArray<AActor*> AllDetectedActors;
	Result->GetAllAsActors(AllDetectedActors);

	UE_LOG(LogTemp, Warning, TEXT("EQS %i"), AllDetectedActors.Num());

	for (AActor* DetectedActor : AllDetectedActors)
	{
              //etc...
	}
}

While everything runs fine (no crashes etc), the AllDetectedActors.Num() line always returns 0, even though the Blueprint version works just fine, so it’s not a problem with the test.

Could someone please help me understand where I’m going wrong?
Thanks in advanced.

Hey AndyB,

I looked at your code and I started looking in the engine what actually happens and what is going wrong. I think what happens is that the EQS can not find the proper UWorld to test in, so he can never find any actors in the first place.

He can not find the right UWorld because when you make your EQS you set it up with your UBTTask as an owner ( FEnvQueryRequest(EnemySeekerQuery, this); ). To fix your problem you should use the right owner(your pawn) or call SetWorldOverride(UWorld* InWorld) on your FEnvQueryRequest( =EnemySeekerQueryRequest ) with the right UWorld.

Hope this helps and fixes the problem :slight_smile:
Elias

Thanks, Elias. :slight_smile: