Search Unity

Feedback [Suggestion]:Generate separate interfaces for each action.

Discussion in 'Input System' started by Snowdrama, May 29, 2020.

  1. Snowdrama

    Snowdrama

    Joined:
    May 10, 2014
    Posts:
    28
    In my game, I have separate scripts for different actions, the idea would be to have "IThingMoveAction"and "IThingAttackAction" instead of just "IThingAction" (not saying remove that for those that do use it)

    Right now if you say "IPlayerActions" it requires you to implement all the controls associated with the player for example c"ontrols.Player.Move" and "controls.Player.Attack" even if this class is just for the player movement. The attack function can stay empty but I imagine if you had a binding for all the possible actions the player can make. It's like an unused function per button on a controller so like, I can only imagine like 11 or 12 other actions in a class that really only wants to implement "controls.Player.Move"

    The alternative of course is to manually add the action using "controls.Ship.Move.started += OnMove;" for each of started, performed and canceled.

    This also gives you the option to implement as many of the actions in a class as wanted. So you could combine say "IPlayerThrottleUpAction" and "IPlayerThrottleDownAction" into the same class it's just a matter of implementing those interfaces as well.

    I don't think this is a major change to the way you use the Input System, but it does make working with the generated code a bit cleaner and more friendly. Here's what I proposed might look like.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class PlayerMovementController : MonoBehaviour, Controls.IPlayerMoveAction, Controls.IPlayerThrottleUpAction, Controls.IPlayerThrottleDownAction
    5. {
    6.     Controls controls;
    7.     void OnEnable()
    8.     {
    9.         if(controls == null)
    10.         {
    11.             controls = new Controls();
    12.         }
    13.        //Sets the callbacks of just the Move action
    14.         controls.Ship.Move.SetCallbacks(this);
    15.         controls.Ship.ThrottleUp.SetCallbacks(this);
    16.         controls.Ship.ThrottleDown.SetCallbacks(this);
    17.         controls.Enable();
    18.     }
    19.     void OnDisable()
    20.     {
    21.         if(controls != null)
    22.         {
    23.             controls.Disable();
    24.         }
    25.     }
    26.  
    27.     public void OnMove(InputAction.CallbackContext ctx) { }
    28.  
    29.     public void OnThrottleUp(InputAction.CallbackContext ctx) { }
    30.  
    31.     public void OnThrottleDown(InputAction.CallbackContext ctx) { }
    32. }
    33.  


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class PlayerAttackController: MonoBehaviour, Controls.IPlayerAttackAction
    5. {
    6.     Controls controls;
    7.     void OnEnable()
    8.     {
    9.         if(controls == null)
    10.         {
    11.             controls = new Controls();
    12.         }
    13.        //Sets the callbacks of just the Attack action
    14.         controls.Ship.Attack.SetCallbacks(this);
    15.         controls.Enable();
    16.     }
    17.     void OnDisable()
    18.     {
    19.         if(controls != null)
    20.         {
    21.             controls.Disable();
    22.         }
    23.     }
    24.  
    25.     public void OnAttack(InputAction.CallbackContext ctx) { }
    26. }
    27.