InputSystem icon indicating copy to clipboard operation
InputSystem copied to clipboard

Polling API

Open fabiopicchi opened this issue 7 years ago • 8 comments

I need to read an input after physics has been processed (need to know if the character is grounded) and with the current event based API this quickly becomes a hassle.

I have to listen to the "performed" events that happen either before FixedUpdate or Update (depending on your configurations) and store in a boolean whether or not an event happened during a certain frame to later use that variable inside my Update(). Finally, I have to setup a coroutine with WaitForEndOfFrame to reset the boolean.

Are there any alternatives? I think this is a pretty common use case so I am probably missing something...

fabiopicchi avatar May 14 '19 08:05 fabiopicchi

You might have better luck to ask questions in the forum: https://forum.unity.com/forums/new-input-system.103/

JiaUnity avatar May 23 '19 00:05 JiaUnity

Thank you @JiaUnity ! I asked in the forum and people apparently don't know of an immediate mode API for the new input system. The thread progresses slowly as it seems that not many people have adopted the new input system so far... The workaround I am currently using is dealing directly with raw input using the Gamepad and Keyboard classes but I think that the best way would be to have a flexible API for InputActions. Just giving the option to check if an action was performed on the current frame would be great!

fabiopicchi avatar Jun 02 '19 18:06 fabiopicchi

A polling API for actions is in the works. The current design is

// Has the action been performed in the current update?
bool InputAction.triggered;

// Read the value the action was last performed with. Returns default value if action is canceled or not yet started.
InputAction.ReadValue<T>();

So, "jump if grounded" could become something like

if (m_MyControls.gameplay.jump.triggered && CharacterIsGrounded())
    Jump();

Is this what you're looking for?

Rene-Damm avatar Jun 03 '19 14:06 Rene-Damm

Yeah! That's exactly it! I saw some mentions in the documentation of a wasPerformedThisFrame but couldn't find it anymore.

I'll try it out :)

fabiopicchi avatar Jun 03 '19 18:06 fabiopicchi

Wait, just to be clear, these two APIs aren't in there yet. It's in the works, though. Have hope it'll land before 1.0.

Rene-Damm avatar Jun 04 '19 12:06 Rene-Damm

Oh, ok! Sorry, I was so excited that I didn't pay attention the "in the works" part of your post hahaha

Anyway, good to hear that this API will be available in the future :)

Well, as a suggestion, I would probably name these methods as they are currently named for the devices. So:

  • wasPressedThisFrame
  • wasReleasedThisFrame
  • pressed
  • ...

fabiopicchi avatar Jun 04 '19 15:06 fabiopicchi

The polling API has landed on the develop branch with #722 and should be in the next package.

Rene-Damm avatar Jul 15 '19 18:07 Rene-Damm

0.9.0-preview is out and contains the aforementioned changes.

Rene-Damm avatar Jul 19 '19 21:07 Rene-Damm