Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Possible to manually invoke a method that receives InputAction.CallbackContext?

Discussion in 'Input System' started by lemmons, Nov 2, 2021.

  1. lemmons

    lemmons

    Joined:
    Jun 20, 2016
    Posts:
    68
    For example, I've got some method I'm using to receive an input action

    Code (CSharp):
    1. public void SomeMethod(InputAction.CallbackContext context)
    2. {
    3.     if (context.performed)
    4.         //do stuff
    5. }
    Is it possible to invoke this same method from another script instead of from player input?
    Ex:
    SomeMethod(???);

    I can't for the life of me figure out what argument to pass in to make context.performed == true. Thanks in advance!
     
  2. spenceregart

    spenceregart

    Joined:
    Feb 18, 2019
    Posts:
    2
    A common pattern I've used is to have the input action callback just forward to other methods, then invoke those from other places as needed, since presumably you don't actually need the CallbackContext at that point.

    So:
    Code (CSharp):
    1. public void OnActionCallback (InputAction.CallbackContext context) {
    2.   if (context.performed) {
    3.     DoActionPerformed();
    4.   }
    5.   // etc...
    6. }
    7.  
    8. public void DoActionPerformed () {
    9.   // actually do the stuff.
    10. }
     
    lemmons likes this.
  3. lemmons

    lemmons

    Joined:
    Jun 20, 2016
    Posts:
    68
    Ah, that's what I ended up doing. It just feels like I have a lot of extra code now but if that's the way to do it then so be it. Thanks!