Announcement

Collapse
No announcement yet.

pleas check this code and tell me what do i wrong.(reference added)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    pleas check this code and tell me what do i wrong.(reference added)

    Hi, im a 3D artist and I self-study C++ in the past few months.

    I want to run my EQS from C++ and use multi-threads method for this one, I search solution for multi-threads on google but every one of them was for the stuff like as some function need heavy looping or something like that (at least I think that), I couldn't find a solution for running engine functions or events through the multi-threads, recently with looking in UE4 codes I notice there is a class name UAITask_RunEQS for doing what I want it seems running EQS with multi-threads but I don't know why QueryResult was protected so I creat a class and inherent it from UAITask so i can access to QueryResult, but no matter what i do I cant run the event, pleas see what part I doing wrong.
    I provide a reference :

    https://drive.google.com/drive/folde...d-?usp=sharing




    this code :

    .h:

    #pragma once

    #include "CoreMinimal.h"
    #include "UObject/ObjectMacros.h"
    #include "Engine/EngineTypes.h"
    #include "Tasks/AITask.h"
    #include "EnvironmentQuery/EnvQueryTypes.h"
    #include "My_AITask_RunEQS.generated.h"

    /**
    *
    */
    UCLASS()
    class SHOOTEMUP_API UMy_AITask_RunEQS : public UAITask
    {
    GENERATED_BODY()
    public:
    UMy_AITask_RunEQS(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get());


    UMy_AITask_RunEQS* RunEQS(AAIController* Controller, UEnvQuery* QueryTemplate);

    void SetQueryTemplate(UEnvQuery& InQueryTemplate) { EQSRequest.QueryTemplate = &InQueryTemplate; }
    void SetNotificationDelegate(const FQueryFinishedSignature& InNotificationDelegate) { NotificationDelegate = InNotificationDelegate; }


    virtual void Activate() override;

    void OnEQSRequestFinished(TSharedPtr<FEnvQueryResult> Result);

    FEQSParametrizedQueryExecutionRequest EQSRequest;
    FQueryFinishedSignature EQSFinishedDelegate;
    FQueryFinishedSignature NotificationDelegate;
    TSharedPtr<FEnvQueryResult> QueryResult;

    };


    cpp:

    #include "My_AITask_RunEQS.h"
    #include "AIController.h"
    #include "VisualLogger/VisualLogger.h"
    #include "EnvironmentQuery/EnvQueryManager.h"
    #include "GameplayTasksComponent.h"


    UMy_AITask_RunEQS::UMy_AITask_RunEQS(const FObjectInitializer& ObjectInitializer)
    : Super(ObjectInitializer)
    {
    bIsPausable = false;
    EQSFinishedDelegate = FQueryFinishedSignature::CreateUObject(this, &UMy_AITask_RunEQS::OnEQSRequestFinished);
    }

    UMy_AITask_RunEQS* UMy_AITask_RunEQS::RunEQS(AAIController* Controller, UEnvQuery* QueryTemplate)
    {

    if (QueryTemplate == nullptr || Controller == nullptr)
    {
    return nullptr;
    }

    UMy_AITask_RunEQS* MyTask = UAITask::NewAITask<UMy_AITask_RunEQS>(*Controller, EAITaskPriority::High);
    if (MyTask)
    {
    MyTask->EQSRequest.QueryTemplate = QueryTemplate;
    }

    return MyTask;
    }

    void UMy_AITask_RunEQS::Activate()
    {

    if (EQSRequest.QueryTemplate && OwnerController
    && OwnerController->GetBlackboardComponent()
    && OwnerController->GetPawn())
    {

    Super::Activate();
    auto get =EQSRequest.Execute(*OwnerController->GetPawn(), OwnerController->GetBlackboardComponent(), EQSFinishedDelegate);

    }
    }

    void UMy_AITask_RunEQS::OnEQSRequestFinished(TSharedPtr<FEnvQueryResult> Result)
    {

    if (IsFinished() == false)
    {
    QueryResult = Result;
    NotificationDelegate.ExecuteIfBound(Result);

    EndTask();
    }
    }
    i call RunEQS(AAIController* Controller, UEnvQuery* QueryTemplate) with static and none-static ( in soruce code is static ) version but none of them activated Task event .
    from what i undrestand with calling RunEQS() the event must work and call Activate() function and its give QueryResult , but with no matter i did i couldnt run it only way i could get querry result was with changing RunEQS() Function :

    U
    My_AITask_RunEQS* UMy_AITask_RunEQS::RunEQS(AAIController* Controller, UEnvQuery* QueryTemplate)
    {

    if (QueryTemplate == nullptr || Controller == nullptr)
    {
    return nullptr;
    }

    UMy_AITask_RunEQS* MyTask = UAITask::NewAITask<UMy_AITask_RunEQS>(*Controller, EAITaskPriority::High);
    if (MyTask)
    {

    MyTask->EQSRequest.QueryTemplate = QueryTemplate;

    FEnvQueryRequest HidingSpotQueryRequest = FEnvQueryRequest(QueryTemplate, Controller);

    HidingSpotQueryRequest.Execute(EEnvQueryRunMode::SingleResult, this, &UMy_AITask_RunEQS::OnEQSRequestFinished);

    }

    return MyTask;
    }

    its give me QuerryResult but not from multi-threads .

    i found this note about Activate() function in GamePlayeTask but i dont know it usefull or not :

    /** Called to trigger the actual task once the delegates have been set up
    * Note that the default implementation does nothing and you don't have to call it */
    virtual void Activate();

    pleas help so i can use this code to run EQS through Multi-threads.

    Thanks for your time .



    Last edited by AAriyAA; 02-24-2020, 01:59 PM.

    #2
    I prepare reference you can download it from :
    https://drive.google.com/drive/folde...d-?usp=sharing

    Comment

    Working...
    X