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

Feedback Why generated c# class from inputactions asset is not a static?

Discussion in 'Input System' started by vizgl, Mar 28, 2021.

  1. vizgl

    vizgl

    Joined:
    Nov 4, 2014
    Posts:
    61
    So, question is in the title.
    Let say, I generate class GameInput from inputactions asset. Now to use it I should create instance in the Player class.

    Code (CSharp):
    1. void Awake()
    2. {
    3.     _input = new GameInput();
    4. }
    Now, I need create same instance in the Tutorial behaviour, to know which input hint to show, based on active control scheme.
    Code (CSharp):
    1. class Tutorial: MonoBehaviour
    2. {
    3.     GameInput _input;
    4.  
    5.    void Awake()
    6.    {
    7.         _input = new GameInput();
    8.    }
    9. }
    10.  
    I should make same steps in the every place, where input is used. Why class GameInput is not a static? It's waste of memory and cpu time, to instantiate GameInput in the many places.
     
  2. BenniKo

    BenniKo

    Joined:
    Mar 24, 2015
    Posts:
    100
    Well you can always do it like this, if you prefer static

    class Tutorial: MonoBehaviour
    {
    static GameInput _input;
    void Awake()
    {
    if(_input == null) {
    _input = new GameInput();
    }
    }
    }
     
  3. vizgl

    vizgl

    Joined:
    Nov 4, 2014
    Posts:
    61
    I made same thing, but little bit different. Create new cs file with content:
    Code (CSharp):
    1. public partial class GameInput
    2. {
    3.     private static GameInput _instance = null;
    4.  
    5.     public static GameInput Get
    6.     {
    7.         get
    8.         {
    9.             if (_instance == null)
    10.                 _instance = new GameInput();
    11.  
    12.             return _instance;
    13.         }
    14.     }
    15.  
    16.     [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
    17.     static void ResetStatic()
    18.     {
    19.         _instance = null;
    20.     }
    21. }
    Now I use it like this GameInput.Get.Player.Movement.ReadValue<Vector2>()

    But, I still think, this workarounds is unnecessary(until Unity's team will prove opposite :)), because GameInput can be static by default
     
  4. palapapa93

    palapapa93

    Joined:
    Apr 29, 2020
    Posts:
    3
    You can wrap around a non-static class to make it static but can't do the opposite. Unity does this to allow maximum flexibility.
     
  5. VSwedeHD

    VSwedeHD

    Joined:
    Jun 8, 2022
    Posts:
    2
    I was absolutely scratching my hair out when trying to do something similar, and you came through with an actually sensible solution to it :D. Thank you so much, also agree that this should just be standard if you're auto-generating the controls into a file.
     
  6. vizgl

    vizgl

    Joined:
    Nov 4, 2014
    Posts:
    61
    After a long time I realized that non-static class for game input is good solution ;). Good for tests. :p
     
  7. rdjadu

    rdjadu

    Joined:
    May 9, 2022
    Posts:
    99
    Another reason is to allow doing stuff like this

    Code (CSharp):
    1. var player1 = new MyControls();
    2. var player2 = new MyControls();
    3.  
    4. player1.devices = new[] { Gamepad.all[0] };
    5. player2.devices = new[] { Gamepad.all[1] };