(Unreal) How to get KeyPress and mouseclick to work with non Player Class

70 views Asked by At

I am trying to get a non player class to respond to a key press for testing purposes and i got a working player character. I want to try and setup a MassProcessor that can take mouseclick and get position in 3D space.

MassProcessor is from unreal engine 5 MassEntity. https://docs.unrealengine.com/5.1/en-US/overview-of-mass-gameplay-in-unreal-engine/

But i am still new a C++ so i am unsure of i would procced.

Bellow is my current code which compiles, and i would want to execute the function UMassTestProcessor::GetCursorPosition when i leftclick on the mouse.

.CCP file

UMassTestProcessor::UMassTestProcessor()
{
    bAutoRegisterWithProcessingPhases = true;
    ExecutionFlags = (int32)EProcessorExecutionFlags::All;
    ExecutionOrder.ExecuteBefore.Add(UE::Mass::ProcessorGroupNames::Avoidance);

    MousePosition = FVector(0,0,0);
    /*
    if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
    {
        PlayerInputComponent->BindAction("LeftMouseButton", IE_Pressed, this, GetCursorPosition());


    }
    */
}

void UMassTestProcessor::ConfigureQueries()
{
    EntityQuery.AddRequirement<FTransformFragment>(EMassFragmentAccess::ReadOnly);
    EntityQuery.AddRequirement<FMassMoveTargetFragment>(EMassFragmentAccess::ReadWrite);


    EntityQuery.AddTagRequirement<FMyTag>(EMassFragmentPresence::All);

}

void UMassTestProcessor::Execute(FMassEntityManager& EntityManager, FMassExecutionContext& Context)
{


    EntityQuery.ForEachEntityChunk(EntityManager, Context, 
        ([this](FMassExecutionContext& Context)
        {
                
            const TArrayView<FTransformFragment> TransformsList = Context.GetMutableFragmentView<FTransformFragment>();
            const TArrayView<FMassMoveTargetFragment> TargetList = Context.GetMutableFragmentView<FMassMoveTargetFragment>();
            //const float WorldDeltaTime = Context.GetDeltaTimeSeconds();

            for (int32 EntityIndex = 0; EntityIndex < Context.GetNumEntities(); EntityIndex++)
            {
                FTransform& Transform = TransformsList[EntityIndex].GetMutableTransform();
                FMassMoveTargetFragment& Target = TargetList[EntityIndex];

                Target.Center = FVector(100.f, 100.f, 0.f);

                Target.Forward = (Target.Center - Transform.GetLocation()).GetSafeNormal();
                Transform.SetRotation(FQuat::FindBetweenVectors(FVector::ForwardVector,Target.Forward));

            }

        }));

}

void UMassTestProcessor::GetCursorPosition()
{
    FHitResult Hit;
    bool bHitSuccessful = false;

    if (PlayerInputComponent)
    {
        APlayerController PlayerController;
        bHitSuccessful = PlayerController.GetHitResultUnderCursor(ECollisionChannel::ECC_Visibility, true, Hit);
        UE_LOG(LogTemp, Warning, TEXT("Position is: %s"), *Hit.Location.ToString());
        MousePosition = Hit.Location;
    }



}

.H file

UCLASS()
class PROJECTMASS_API UMassTestProcessor : public UMassProcessor
{
    GENERATED_BODY()

public:
    UMassTestProcessor();

    UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = WorldPositionFromMouse)
    FVector MousePosition;

    UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
    class UInputAction* PlayerInputComponent;

    //virtual void ACharacter::ACharacter SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

protected:
    virtual void ConfigureQueries() override;
    virtual void Execute(FMassEntityManager& EntityManager, FMassExecutionContext& Context) override;

    void GetCursorPosition();

private:
    FMassEntityQuery EntityQuery;
};

I would like some simple code example, that way its a lot easier for me to understand, but i would be thankful for any suggestions.

0

There are 0 answers