قالب وردپرس درنا توس
Home / IOS Development / Environment Query System in C ++ – Think and Build

Environment Query System in C ++ – Think and Build



I'm still working on the AI ​​system for Mirror's End, and I decided to move the entire AI kernel from Behavior Tree to Utility AI. Environment Query System (EQS) is very well integrated with Behavior Trees, and I really wouldn't lose the ability to run EQS from my custom AI system. Fortunately, the unreal engine allows us to use EQS outside Behavior Trees in a very simple way. With this short tutorial I will show you how to do it with C ++.

LET CODE

Data types we are interested in are UEnvQuery and FEnvQueryRequest . The class UEnvQuery inherits from UDataAsset which, as the UE4 documentation says, is "the basic class of a single resource containing data".
The easiest way to set up This data should include a reference to a UEnvQuery in your controller.

  class AMyController: public AAIC controller {
.
.
.
OPERATION (EditAnywhere, Category = "AI")
UEnvQuery * FindHidingSpotEQS;
.
.
} 

FEnvQueryRequest is a structure that acts as a cover to allow execution request and also to pass that information (query parameters).

You will call it directly from your class implementation (I actually prefer to keep a reference in the controller to the request request, but that is not necessary).

CAN QUERY

From the reviewer you can only run the query through FEnvQueryRequest this way:

  FEnvQueryRequest HidingSpotQueryRequest = FEnvQueryRequest (FindHidingSpotEQS, this);

HidingSpotQueryRequest.Execute (
EEnvQueryRunMode :: SingleResult,
this,
& AMyController :: HandleQueryResult); 

As we first initialize the request request sending the request we will run (the reference to the EQS assets previously defined in the controller header) and the request the owner, as in This case is the controller.
When the request is initialized, we are ready to perform it with the function Execute .
The first parameter defines how to calculate the result of the query. We obviously have the same options available from BehaviorTree (SingleResult, RandomBest5Pct, RandomBest25Pct, AllMatching). The second parameter is the delegate object that will receive query results, and the third is the delegate method used to handle the result, this method must implement FQueryFinishedSignature ]

HANDLING QUERY RESULT

The delegation function that we will designate to handle the execution of the query may be something like:

  void HandleQueryResult (TSharedPtr result) {
if (result-> IsSuccsessful ()) {
MoveToLocation (result-> GetItemAsLocation (0));
}
} 

The query result is wrapped in a FEnvQueryResult structure handled by a shared pointer (in a few words, a smart pointer that owns the object it points and deletes it when no other references to the object are available).
The FEnvQueryResult structure has some useful features to confirm the query result. I use isSuccessfull () but you can also use isFinished () and isAborted () to confirm other query requirements. Once you know that the query is complete, you can access search results using a function such as GetItemAsLocation (index) that returns a single item that comes from the query (In the example I ask that the item is on index 0) or you can request GetAllAsLocations () function. If you prefer, and if it makes sense to the query you ran, you can also get the items as actors using GetItemAsActor and GetAllAsActors . Another interesting and useful option is to retrieve all the items with the GetItemScore function:

  for (int i = 0; in Items.Num (); i ++) {
UE_LOG (LogTemp, Warning, TEXT ("Points for item% d is% f"), i, result-> GetItemScore (i));
} 

Depending on what you have defined as query mode (SingleResult, RandomBest5Pct, RandomBest25Pct, AllMatching), you can get different scores, but the items will always be ordered from highest to lowest score.

Here you find a complete code example:


 // AMyAIController.h --------------------------------- ------------
#include "CoreMinimal.h"
#include "AIController.h"
#include "EnvironmentQuery / EnvQueryTypes.h"
#include "MyAIController.generated.h"

class UEnvQuery;

UCLASS ()
class EQSTUTORIAL_API AMyAIController: public AAIC controller
{
GENERATED_BODY ()

OPERATION (EditAnywhere, Category = "AI")
UEnvQuery * FindHidingSpotEQS;

UFUNCTION (BlueprintCallable)
void FindHidingSpot ();

void MoveToQueryResult (TSharedPtr  result);
};

// AMyAIController.cpp ---------------------------------------------
#include "MyAIController.h"
#include "EnvironmentQuery / EnvQueryManager.h"

void AMyAIController :: FindHidingSpot ()
{
FEnvQueryRequest HidingSpotQueryRequest = FEnvQueryRequest (FindHidingSpotEQS, this);
HidingSpotQueryRequest.Execute (EEnvQueryRunMode :: SingleResult, this, and AMyAIController :: MoveToQueryResult);
}

void AMyAIController :: MoveToQueryResult (TSharedPtr  result)
{
if (result-> IsSuccsessful ()) {
MoveToLocation (result-> GetItemAsLocation (0));
}
} 

and you can also find a weather at GitHub.

Ciao!

  Yari D&G areglia

Yari D&G areglia

https://www.thinkandbuild.it

Senior iOS developer @ Neato Robotics during the day, game developer and wannabe artist @ Black Robot Games at night.


Source link