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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Looking for Callback Context on a player input object

Discussion in 'Input System' started by notMateo, Jan 30, 2020.

  1. notMateo

    notMateo

    Joined:
    Mar 2, 2018
    Posts:
    36
    Me and friend are trying to figure out how to get a callback context from a Game Object using a Player Input component to get the device of the button that has been pressed, if it's possible?

    Basically I'm trying to get some multiplayer functionality going. When I press a face button, I need to get which controller pressed said button and return the device. This is what we tried so far:

    Code (CSharp):
    1.     void MyButtonPress(InputAction.CallbackContext ctx)
    2.     {
    3.         InputDevice device = ctx.control.device;
    4.         Debug.Log(device);      
    5.     }
    This is what my friend came up with, but I wanted to do this within the method called by the Player Input (MyButtonPress, in the example above)

    Code (CSharp):
    1.     void Awake()
    2.     {
    3.         ctrl.Ctrl.Act.performed += ctx => onButtonPress(ctx);
    4.     }
    5.  
    6.     void onButtonPress(InputAction.CallbackContext ctx)
    7.     {
    8.         InputDevice device = ctx.control.device;
    9.         Debug.Log(device);
    10.     }
     
  2. syphase

    syphase

    Joined:
    Jun 21, 2021
    Posts:
    2
    I am having the same issue. I want to be able to get the specific PlayerInput that called the action.