Search Unity

[SOLVED] Send button press callback on every Update

Discussion in 'Input System' started by alkaitagi, Oct 10, 2019.

  1. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    87
    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.
     
  2. Jichaels

    Jichaels

    Joined:
    Dec 27, 2018
    Posts:
    237
    Code (CSharp):
    1. bool isMyKeyDown;
    2.  
    3. YourCallBackForPress set it to true
    4. YourCallBackForRelease set it to false
    done :p
     
  3. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    87
    Yeah, I myself figured out this is likely the only way. Regardless, thank you very much!
     
  4. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    When using actions, an equivalent would be using ReadValue.

    Code (CSharp):
    1. void Update()
    2. {
    3.     if (myAction.ReadValue<float>() > 0)
    4.         /* ... */;
    For querying the keyboard state directly a la Input.GetKey, you can poll Keyboard.

    Code (CSharp):
    1. void Update()
    2. {
    3.     if (Keyboard.current.aKey.isPressed)
    4.         /* ... */;
    5.  
    6.     // Or...
    7.     if (Keyboard.current[Key.A].isPressed)
    8.         /* ... */;
    9.  
    10.     // Or...
    11.     if (((KeyControl)Keyboard.current["a"]).isPressed)
    12.         /* ... */;
     
    alkaitagi likes this.
  5. Jichaels

    Jichaels

    Joined:
    Dec 27, 2018
    Posts:
    237
    @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 ?
     
  6. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    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<float>) the system can treat axes and buttons interchangeably when needed/desired.
     
  7. Jichaels

    Jichaels

    Joined:
    Dec 27, 2018
    Posts:
    237
    Yeah, I still have troubles accepting that there is no "better" solution and so much differents ways of doing things :D

    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 !
     

    Attached Files:

  8. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    87
    Thank you very much! This is exactly what I needed.
     
  9. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    87
    @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.
     
  10. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    87
    Figured it out myself, the scale processor solved the problem.

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

    upload_2019-10-17_11-48-2.png