Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Resolved How do I unregister a InputAction.performed ?

Discussion in 'Input System' started by Cry4775, Jul 31, 2020.

  1. Cry4775

    Cry4775

    Joined:
    Apr 4, 2020
    Posts:
    10
    I am doing an inventory system controlled with keyboard (no mouse) and I'm implementing a drop/remove item function.
    When I select an inventory slot it gathers the slot infos (location, item inside, etc.)
    When I deselect of course I clear the variables
    I'm using the new input system, I've set up a key (DELETE) for that function, the thing is that every time I select a button, I'm adding the performed event, and so all the slots I've been on will be deleted, cause I'm not unsubscribing the event.
    The code is this:
    Code (CSharp):
    1. public void OnSelect(GameObject obj)
    2.     {
    3.         selectedItem.destinationObj = obj;
    4.         if (itemsDisplayed.ContainsKey(obj))
    5.             selectedItem.destinationItem = itemsDisplayed[obj];
    6.  
    7.         inputInventory.InventoryUInavigation.Drop.performed += ctx => inventory.RemoveItem(itemsDisplayed[obj].item);
    8.     }
    9.     public void OnDeselect(GameObject obj)
    10.     {
    11.         selectedItem.destinationObj = null;
    12.         selectedItem.destinationItem = null;
    13.     }
    As you can see, I have this method called RemoveItem that needs a variable to pass, which is the slot I'm on. I have seen that you can unsubscribe if you write something like this:
    Code (CSharp):
    1. public void Drop(InputAction.CallbackContext context)
    2.     {
    3.         // Do things
    4.     }
    and then
    Code (CSharp):
    1. inputInventory.InventoryUInavigation.Drop.performed += Drop;
    2.  
    3. inputInventory.InventoryUInavigation.Drop.performed -= Drop;
    4.  
    But I need to pass a variable on that method, and with this I can't do it. Please help me
     
    Last edited: Jul 31, 2020
  2. Kreesty

    Kreesty

    Joined:
    Feb 21, 2016
    Posts:
    11
    you need
    Code (CSharp):
    1. void FunctionName(UnityEngine.InputSystem.InputAction.CallbackContext obj){
    2.  
    3.  
    4. }
     
  3. Cry4775

    Cry4775

    Joined:
    Apr 4, 2020
    Posts:
    10
    Yeah but I can't pass my GameObject argument with that
     
  4. Kreesty

    Kreesty

    Joined:
    Feb 21, 2016
    Posts:
    11
    Hmm, can't you just do .. ?
    Code (CSharp):
    1. void FunctionName(UnityEngine.InputSystem.InputAction.CallbackContext obj, GameObject yourObj){
    2.  
    3. }
     
  5. Cry4775

    Cry4775

    Joined:
    Apr 4, 2020
    Posts:
    10
    Unfortunately I already tried and no, it doesn't work...

    EDIT: I solved it, tomorrow I'll post the code if someone ever needs this.
     
    Last edited: Aug 1, 2020
  6. Cry4775

    Cry4775

    Joined:
    Apr 4, 2020
    Posts:
    10
    Code (CSharp):
    1. public Action<InputAction.CallbackContext> handler;
    2.  
    3. void Drop (InputAction.CallbackContext ctx, GameObject obj)
    4.     {
    5.         // This is the method called when you subscribe to handler's event
    6.         inventory.RemoveItem(itemsDisplayed[obj].item);
    7.     }
    8.  
    9.     public void OnSelect(GameObject obj)
    10.     {
    11.         selectedItem.destinationObj = obj;
    12.         if (itemsDisplayed.ContainsKey(obj))
    13.             selectedItem.destinationItem = itemsDisplayed[obj];
    14.        
    15.         handler = (InputAction.CallbackContext ctx) => Drop(ctx, obj);
    16.  
    17.         inputInventory.InventoryUInavigation.Drop.performed += handler;
    18.     }
    19.     public void OnDeselect(GameObject obj)
    20.     {
    21.         selectedItem.destinationObj = null;
    22.         selectedItem.destinationItem = null;
    23.         inputInventory.InventoryUInavigation.Drop.performed -= handler;
    24.     }
    That's how I solved it.
     
unityunity