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

Getting c# class of custom Actions from PlayerInput

Discussion in 'Input System' started by Gretsok, Apr 10, 2021.

  1. Gretsok

    Gretsok

    Joined:
    Mar 20, 2020
    Posts:
    4
    Hello !

    I am trying to make a custom manager on top on the new input system to be able to get the input actions of one specific player.
    I am using a custom script next to my PlayerInput to access it.

    What I would like to do is access the c# generated class instance of a certain player instead of using getters that take a string in parameters.

    not like this:
    Code (CSharp):
    1. m_playerInput.actions.FindActionMap("Generic").FindAction("Jump").performed += Jump_performed;
    but like this:
    Code (CSharp):
    1. PlayerInputsActions customActions = new PlayerInputsActions();
    2. customActions.Generic.Jump.performed += Jump_performed;
    But since, we're referencing the inputaction asset in the PlayerInput and not the c# class (How could we?), I was afraid that we couldn't do it. Plus, when I look at the c# class, I see that it inherits from nothing but IInputActionCollection while the actions of the PlayerInput is directly an InputActionAsset (which also inherits from IInputActionCollection) so it is not castable toward my own class, which may makes sense since it's what I reference in the inspector, so I feel like there's no way to use the c# class then ?

    When continuing to search for a way to get my c# class, I found the attribute user of PlayerInput that allow you to access another actions attribute which is of type IInputActionCollection (Yey !), so I hoped that maybe, since my c# class inherits from this interface, I could cast it to get my c# class ? But no, it is just the same InputActionAsset as the actions directly accessible in the PlayerInput in a more generic container. So, if whatever is the inputActions, it will always be an InputActionAsset, why is it declared as an IInputActionCollection ?

    So, my two final questions may be :
    Is there anyway to get that c# class from the PlayerInput ?
    If not, what is the purpose of this generated c# class ?

    Wish you a nice day :D
     
  2. Gretsok

    Gretsok

    Joined:
    Mar 20, 2020
    Posts:
    4
    I'm still looking for an answer if someone have it ! :D
     
  3. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,913
    Generated c# class and PlayerInput have nothing to do with each other. AFAIK PlayerInput never uses the generated class. You can use the generated class if you do most things from code and not from components and you want a type-safe way of handling input.

    This indicates that you want local multiplayer? I'm not sure because you never stated it. But the generated c# class is not the way to go if you want local mp. Check the simple multiplayer sample how to handle multiplayer input control.
     
  4. Gretsok

    Gretsok

    Joined:
    Mar 20, 2020
    Posts:
    4
    Thank you for your answer !

    I'll go with the PlayerInput for local multiplayer then :D

    At first, I was not making local multiplayer, but I was just looking for a way to get the last control scheme used using the c# generated class (I am making a solo game). I found out that it was a lot easier to use PlayerInput to get it, I continued searching in that way because it's something that I know I would use for other projects.
    I guess I'll just switch to PlayerInput even for the solo game.

    Thank you again ! :D
     
    Lurking-Ninja likes this.