[SOLVED] Send button press callback on every Update

Is it possible for PlayerInput component to send callback on every frame when a button is down? With the old systems I used to check Input.GetKey() inside Update function. But now I see that the systems can send callbacks only when button pressed and released.

bool isMyKeyDown;

YourCallBackForPress set it to true
YourCallBackForRelease set it to false

done :stuck_out_tongue:

Yeah, I myself figured out this is likely the only way. Regardless, thank you very much!

When using actions, an equivalent would be using ReadValue.

void Update()
{
    if (myAction.ReadValue<float>() > 0)
        /* ... */;

For querying the keyboard state directly a la Input.GetKey, you can poll Keyboard.

void Update()
{
    if (Keyboard.current.aKey.isPressed)
        /* ... */;

    // Or...
    if (Keyboard.current[Key.A].isPressed)
        /* ... */;

    // Or...
    if (((KeyControl)Keyboard.current["a"]).isPressed)
        /* ... */;
1 Like

@Rene-Damm

Any reason one would use this instead of the other way (PlayerInput with callbacks for exemple as it is what I’m currently using) ? Wouldn’t this break if there is no keyboard ?

On a side note, why are Action of type “button” floats 0 / 1 instead of bools ?

Mostly depends on your particular use case and preferences. For example, when using the feature to generate C# classes for .inputactions, just polling actions in an update method can be very convenient compared to having callbacks store state for later processing in Update. But neither method is inherently better or more recommendable than the other.

Yes. If the code is meant to run on platforms that may not have a keyboard (ATM on desktops, there will always be a keyboard device), you’d have to null check.

Not every button is strictly binary. Triggers on the gamepad, for example, are generally useful to consider as buttons but they have a full [0…1] floating-point range. By basing ButtonControl on AxisControl (which in turn is an InputControl) the system can treat axes and buttons interchangeably when needed/desired.

Yeah, I still have troubles accepting that there is no “better” solution and so much differents ways of doing things :smile:

Then I definitely don’t understand this (see attached screenshot) :

for me, “Button” was binary (gamepad buttons, keyboard keys…) and axis (triggers…) were “Value”.

But I tested a bit and I don’t even see a difference when I switch from one to another ?

Could you explain a bit what are the 3 items of the dropdown supposed to be/be used ?

Thanks a lot !

5056289--496403--Sans titre.png

Thank you very much! This is exactly what I needed.

@Rene-Damm
I have one more question. I want to switch weapons using keyboard digits from Key.Digit1 to Key.Digit5. Currently I store those Key enums in array in my script and on every Update check if any of them was pressed, then I switch weapon to the index of the pressed key. My question is, can I map several buttons to one action so that the action will return a value depending on which binding was pressed?

I think I will need to write a custom composite for that. But so far I only saw composites having a fixed number of components, while I want it to behave just like with the bindings where you can add or remove several of them.

Figured it out myself, the scale processor solved the problem.

@Rene-Damm Great work with the new system, it is amazing!