Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Invoking a Unity Event with CallbackContext and another argument

Discussion in 'Input System' started by ProvencalG, May 12, 2020.

  1. ProvencalG

    ProvencalG

    Joined:
    Jan 24, 2018
    Posts:
    9
    I wonder, for cleanness in my case, if it's possible to link a method (to an invoked UnityEvent) with an argument and still use context.

    I have 0 to 9 keys that I use the same way and wondered if I could call the same method for all those keys, but with an int argument to differentiate them. But I also need to use context. And if I put only the int parameter I can use it but if I add the context in an argument it breaks: I can't access it in the UnityEvent method dropdown.
    The method:
    Code (CSharp):
    1. public void PlayNumButtonAnim(int numKey, InputAction.CallbackContext context)
    2.     {
    3.        // Stuff
    4.     }
    I tried to put the arguments in both orders, it doesn't work.

    Am I missing something or is it impossible?
    I find it odd that it would accept an argument, be able to be set which is very useful, but then having to sacrifice context.
     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Unfortunately, no, that's not possible. The signature of the method has to match the signature expected by the event.

    One way to do this is something like.

    Code (CSharp):
    1. void OnKey(InputAction.CallbackContext context)
    2. {
    3.     var digit = ((KeyControl)context.control).keyCode - Key.Digit0;
    At a higher level, the request for a composite binding that works like an enum has come up here and there. This would fit in the same use case. Basically, you'd have a composite where each part binding is associated with an integer and if the part binding triggers, the value of the whole composite is that of the index of the part binding. Real sucky explanation...