How to use EQS in c++

Hi. I was wondering what the ‘correct’ way to use EQS from C++ is. I saw the topic https://answers.unrealengine.com/questions/438304/eqs-via-c.html but this didn’t help me very much.

If we’re meant to do it by creating an instance of FEnvQueryRequest, how do we pass in which query we wanted to do to it?

Going further than that, is there a sample of how to create queries in c++, and can we get the queries made in blueprint for c++, like how you do it with ConstructorHelpers for bp classes?

Thanks!

1 Like

I would also like to see a sample of this.

Hey Xavice,

I’ll give you an example how to use the EQS system in c++, MyActor is the class:
In MyActor.h

UPROPERTY()
UEnvQuery* MyQuery; // set the query in editor
FEnvQueryRequest * MyQueryRequest;
// The function that gets called when querry finished
void MyQueryFinished(TSharedPtr<FEnvQueryResult> Result);

In MyActor.cpp

void MyActor::BeginPlay()
{
	MyQueryRequest = FEnvQueryRequest(MyQuery, this);
}

void MyActor::RunEQS()
{
	MyQueryRequest.Execute(EEnvQueryRunMode::SingleResult, this, MyActor::MyQueryFinished);
}

void MyActor::MyQueryFinished(TSharedPtr<FEnvQueryResult> Result)
{
	AActor* result = Result->GetItemAsActor(0);
	//...
}

If you need any more information you can ask!

Hope this helps :slight_smile:
Elias

3 Likes

Thanks, excuse my ignorance I am new to EQS, but if MyQuery refers to a Test, where is the Generator set/created?

You create your EQS in the editor like any other EQS you would run in the behavior tree, and then set it in Blueprints :slight_smile:

Thanks that helped. I was trying to find a way to make or get the queries from c++ rather than just feeding them in through BP, but this’ll work just fine too.

I think there is an “&” missing in line 8 of the code for function address. So the line would be:

MyQueryRequest.Execute(EEnvQueryRunMode::SingleResult, this, &MyActor::MyQueryFinished);

I’ve just written an article about the topic :slight_smile:

1 Like

PasteDog - Have you ever tried to create Async EQS generators/tests?

The syntax here is wrong. Please edit your solution. @PasteDog

Hello,
Sorry for sending message in an old topic.
I need some help to send a vector value into my EQS from C++
I have a custom AI logic and I just need to some check from EQS, I made my EQS and it works properly with test data but I need a way to pass some game data into EQS from the C++.
Thank you