0
\$\begingroup\$

I am currently working on a combat system and I wanted to be able to have key combos to execute combat moves and such, and I am detecting key input type as well for example, if the key is tapped, or long pressed.

I use this line below to record the time a key is pressed, and time when the same key is released, so I can differentiate whether the key is just a quick tapped or long pressed.

FPlatformTime::Seconds();

and when the the game detected a key press, it will execute the function below, using a timer and stopped on released.

//Long Press
void ABasePlayerController::StartRepeat(UFunction* FunctionNamesToCall, FKey KeyInput)
{
  //Execute if Timer is still active
  if (IsKeyDown != false && GetWorld()->GetTimerManager().IsTimerActive(RepeatTimerHandle) && WasInputKeyJustPressed(KeyInput))
  {
     CalledFunction.Broadcast(FunctionNamesToCall, KeyInput);
  }
}

The problem is when I wanted to make a key combination, and I stopped this function, whenever a new Key is pressed, the new key will be considered as a Long Pressed as well. My current codes only able to register input types after the key is released, and I wanted to be able to make a charging combo as well.

For example when I'm holding a Tab and I tap a new different key, both keys will be considered as a Long Pressed, while the expected result supposed to be Long Pressed Tab key, and the new pressed key is a simple quick tap.

Any ideas? Thanks!

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

If I'm understanding it correctly, you have a single timer but many buttons that could be pressed. Why not have N timers, where N == Number of Buttons. This may sound daunting and gross; but it's quite easy to manage with a map (which is called a Dictionary in C# or Python.)

Then use the keys that you're checking for as the inputs to the map, with the timer being the piece of data being stored. Now you have a (constant time!) algorithm for managing any number of timers and it doesn't take much code to access/modify the timers.

This would probably be a map< FKey, FPlatformTime > if I'm reading your types correctly. Simply start, stop, and access your timers in the map via the FKey being pressed and voila.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ The map TMap<> in UE4 can also be dynamic - allocate the timer when the button is pressed - this way you will save some performance that timers are eating. Can also use the Reserve() call to save up dynamic memory allocations - and be on the fast side. \$\endgroup\$ Nov 22, 2019 at 1:58
  • 1
    \$\begingroup\$ Thanks to both of you, after days cramming about TMap and countless of crashes during its iteration I finally managed to do it. )) \$\endgroup\$
    – Mira
    Nov 26, 2019 at 4:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .