Hey there,
the last couple of days I spent some time fiddling around with the new Input System (reading the docs, looking into these forums, studying the API) and so far I really do like what I’m seeing. Setting up working controls isn’t too complicated and for know most of the things are well documented, although here’s my first nitpick:
- the filter in the Docs (Input System | Input System | 1.0.2) is dumb. While looking into the API I stumbled over something called “InputUser”. Eager to know what that is and how it can be used I typed that into the filter and nothing popped up. However there is this big section talking about UserManagement with “InputUser” plastered all over the site (User Management | Input System | 1.0.2)
But yeah, moving on!
So here’s what I’m trying to achieve and I don’t know exactly how. We’re developing a multiplatform game (PC/Xbox, Playstation, Nintendo Switch) and from the ground up we want to design the Input to be able to support all these platforms. We also installed the Input Packages for the respective console platforms, so that’s not an issue. Also we want to allow the player to use at least the popular gamepads also on the PC version if they should decide to hook up a gamepad (Playstation Gamepad on PC, any Xbox Gamepad on PC, Nintendo Switch JoyCons on PC)
Which means, we came up with this:
For every possible way of playing the game we created a very own control scheme, at least when playing on PC. We would also further add control schemes for the Consoles as well.
Now at first I asked myself. Why would I want to use different control schemes at all. I tried to find anything about the control schemes in the docs but didn’t find the information which I need. Most of the examples and video tutorials (which aren’t that much btw.) max. two control schemes (Keyboard and Gamepad) or one universal scheme which combined both.
Aaaand then I stumbled over this topic: Multiple control schemes or actions
There @Rene-Damm stated:
Yeah! This is exactly what I want. What I need is: the game starts on PC and the default control scheme is KeyboardOnly but as soon a let’s say Xbox One Controller is connected I want to immediately change the control scheme to the XOne Controller. And not only simple change; the keyboard control scheme should be blocked/disabled/whatever. Of course in the actual game there will be an UI Popup asking for permission before actually changing.
For now I need the information how to do this in code. In the topic above there are some directions to potential solutions but they are all related to the PlayerInput Behaviour. Isn’t there any other way?
Currently I’m trying to handle my input like this (and please tell me if this is a dumb way of doing it)
I have these ActionMaps for now, selected the InteractionMap for PC and Xbox as an example:
I have this class InputHandler:
public class InputHandler : MonoBehaviour
{
private PlayerInputActions playerInputActions;
public PlayerInputActions PlayerInputActions
{
get { return playerInputActions; }
set { playerInputActions = value; }
}
private void Awake()
{
playerInputActions = new PlayerInputActions();
}
private void OnEnable()
{
InputSystem.onDeviceChange += InputSystemOnDeviceChange;
}
private void InputSystemOnDeviceChange(InputDevice device, InputDeviceChange deviceChange)
{
switch (deviceChange)
{
case InputDeviceChange.Added:
Debug.Log("A new device has been added! Device is: " + device.displayName);
//TODO: Change pairing from Keyboard to Xbox and dont allow/disable any other schemes
break;
case InputDeviceChange.Removed:
Debug.Log(device.displayName + "has been removed!");
break;
case InputDeviceChange.Disconnected:
Debug.Log(device.displayName + "has been disconnected!");
break;
case InputDeviceChange.Reconnected:
break;
case InputDeviceChange.Enabled:
break;
case InputDeviceChange.Disabled:
break;
case InputDeviceChange.UsageChanged:
break;
case InputDeviceChange.ConfigurationChanged:
break;
case InputDeviceChange.Destroyed:
break;
}
}
}
And then a simple behaviour like this which sets callbacks for the Interaction Map:
public class Interaction : MonoBehaviour, PlayerInputActions.IInteraction_MapActions
{
private InputHandler inputHandler;
private void Awake()
{
inputHandler = GetComponent<InputHandler>();
inputHandler.PlayerInputActions.Interaction_Map.SetCallbacks(this);
}
private void OnEnable()
{
inputHandler.PlayerInputActions.Interaction_Map.Enable();
}
private void OnDisable()
{
inputHandler.PlayerInputActions.Interaction_Map.Disable();
}
public void OnInteract(InputAction.CallbackContext context)
{
Debug.Log("Phase: " + context.phase);
Debug.Log("Interaction triggered!"); }
}
As for now when I hit the “E” key on the Keyboard the OnInteract-Callback is triggered. When I connect my Xbox One Controller the InputHandler recognises it and prints the name. When I hit “X” on the controller the OnInteract-Callback gets triggered as well, awesome. Only thing is: I still can hit the “E” key on the keyboard.
So ultimately: how can I enable/disable controlschemes in code without using the PlayerInput Behaviour?