Search Unity

Local multiplayer without PlayerInput and PlayerInputManager

Discussion in 'Input System' started by Sixoul, Jan 23, 2022.

  1. Sixoul

    Sixoul

    Joined:
    Jan 9, 2014
    Posts:
    20
    Hey, I'm using a ScriptableObject with Unity Events to register input. I've seen it used for local multiplayer but I'm trying to abstract it where it's not specific to players. Here's some code I found.

    Code (CSharp):
    1. Let's assume you have a generated C# class called "MyControls".
    2. // Let's create two instances of this and set one up for gamepad
    3. // and one up for keyboard&mouse. Let's assume there is a control
    4. // scheme called "Gamepad" and another called "KeyboardMouse".
    5. // P1 gets gamepad.
    6. var p1Controls = new MyControls();
    7. p1Controls.devices = new[] { Gamepad.all[0] };
    8. p1Controls.bindingMask = InputBinding.MaskByGroup("Gamepad");
    9. p1Controls.Enable();
    10. // P2 gets keyboard&mouse.
    11. var p2Controls = new MyControls();
    12. p2Controls.devices = new[] { Keyboard.current, Mouse.current };
    13. p2Controls.bindingMask = InputBinding.MaskByGroup("KeyboardMouse");
    14. p2Controls.Enable();
    This is helpful showing what's needed if the players were different devices. But what if all players were gamepads. Would I just keep count of players and set gamepad.all[count] and probably ignore the binding mask?

    I was also wondering would creating an instance or instantiating a MyControls result in the same as having two scriptableObjects, one for player 1 and another for player 2? This way it can be modular how many players are allowed?
     
    Walter_Hulsebos likes this.