input action elapsed seconds in c++

C++ Equivalent of this
elapsed seconds

InputAction::GetElapsedTime()

Found on line 192 of InputAction.h

1 Like

Hey can you make a function
For this

I am new to c++ and can’t able to call this function from UInputAction

Can you be more specific about what you want to do?

1 Like

Okk
So I have a UInputaction variable in a actor
Which has property specifier editanywhere so I can select it from blueprint class

I successfully bind the action to some functions
With the help of enhancedinputcomponent
In c++

And also a function for it’s action value

Now I need a function which returns
The elapsed seconds of input action’s variable

Yeah, I could infer all of that already. What are you doing with the elapsed time?

Instead of explaining the structure of your code, why not just post it?

1 Like

here
.h file

**// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

include “CoreMinimal.h”
include “GameFramework/Actor.h”
include “DebugActorBase.generated.h”

UCLASS()
class ENHANCEDINPUTSYSTEMCOURSE_API ADebugActorBase : public AActor
{
GENERATED_BODY()

public:
// Sets default values for this actor’s properties
ADebugActorBase();

protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;

public:
// Called every frame
virtual void Tick(float DeltaTime) override;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Input)
class UInputAction* InputAction;

UFUNCTION(BlueprintImplementableEvent)
	void TriggeredAction(const FInputActionValue& Value);

UFUNCTION(BlueprintImplementableEvent)
	void StartedAction(const FInputActionValue& Value);

UFUNCTION(BlueprintImplementableEvent)
	void OnGoingAction(const FInputActionValue& Value);

UFUNCTION(BlueprintImplementableEvent)
	void CanceledAction(const FInputActionValue& Value);

UFUNCTION(BlueprintImplementableEvent)
	void CompletedAction(const FInputActionValue& Value);

UFUNCTION(BlueprintPure)
void GetValue(FInputActionValue& Value);

UFUNCTION(BlueprintPure)
float GetElapsedSeconds();

struct FEnhancedInputActionValueBinding* MoveActionBinding;

};
**

.cpp file

include “DebugActorBase.h”
include “InputAction.h”
include “Components/InputComponent.h”
include “EnhancedInputComponent.h”
include “Kismet/GameplayStatics.h”

// Sets default values
ADebugActorBase::ADebugActorBase()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don’t need it.
PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void ADebugActorBase::BeginPlay()
{
Super::BeginPlay();

APlayerController* FirstLocalPlayer = UGameplayStatics::GetPlayerController(this, 0);

if (IsValid(FirstLocalPlayer) && IsValid(FirstLocalPlayer->InputComponent))
{
	UInputComponent* PlayerInputComponent = FirstLocalPlayer->InputComponent;
	if (PlayerInputComponent)
	{

		if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
		{
			EnhancedInputComponent->BindAction(InputAction, ETriggerEvent::Triggered, this, &ADebugActorBase::TriggeredAction);

			EnhancedInputComponent->BindAction(InputAction, ETriggerEvent::Started, this, &ADebugActorBase::StartedAction);

			EnhancedInputComponent->BindAction(InputAction, ETriggerEvent::Ongoing, this, &ADebugActorBase::OnGoingAction);

			EnhancedInputComponent->BindAction(InputAction, ETriggerEvent::Canceled, this, &ADebugActorBase::CanceledAction);

			EnhancedInputComponent->BindAction(InputAction, ETriggerEvent::Completed, this, &ADebugActorBase::CompletedAction);

			MoveActionBinding = &EnhancedInputComponent->BindActionValue(InputAction);
		}
	}
}

}

// Called every frame
void ADebugActorBase::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);

}

void ADebugActorBase::GetValue(FInputActionValue& Value)
{
if (MoveActionBinding)
{
Value = MoveActionBinding->GetValue();
}
}

float ADebugActorBase::GetElapsedSeconds()
{
FInputActionInstance ActionIns;
ActionIns = FInputActionInstance(InputAction);
return ActionIns.GetElapsedTime();
}

I tried a lot to get a result
but i think i am not that pro in it

Ah, now I see the issue. You can’t get the elapsed time directly from the input action. It comes from a struct holding instance data that you have to find through the enhanced input subsystem.

Try this:

float ADebugActorBase::GetElapsedSeconds(UInputAction* action)
{
	auto enhancedInput = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(this->GetLocalPlayer());
	if (enhancedInput)
	{
		auto playerInput = enhancedInput->GetPlayerInput();
		if (playerInput)
		{
			auto actionData = playerInput->FindActionInstanceData(action);
			if (actionData)
			{
				return actionData->GetElapsedTime();
			}
		}
	}
	return 0.f;
}

Add the action you want to check as a parameter. There are a lot of pointers to check, so if any of them fails, the function will just return 0.

I haven’t tested it in a game, so let me know if it works.

2 Likes

Thanks bro

it’s working perfectly

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.