How can you swap devices?
If I spawn a PlayerInput and let it use the keyboard, how do I extend that in future so that if they connect a controller, i can add to that ReadOnly array, there appears to be nothing for it other than destroying the PlayerInput and recreating it with the new device.
Use control scheme, set up one with a controller, then you can switch to it
Even if the devices that are ‘used’ by the PlayerInput doesn’t change or doesn’t update so it still polls keyboard?
So again, the question was about updating the devices, not the scheme.
How can it use the controller scheme if its locked out of using that device?
Don’t really understand what you’re trying to do, but using PlayerInput you don’t have to care about device pairing, it handles everything for you
If only the player input did that, I wouldn’t be asking this question.
I instantiate a player with the player input variant of instantiate, passing Gamepad.all to the final device parameter.
That will assign keyboard/mouse if the controller isn’t plugged in, obviously after plugging in the controller unity knows the device has been added but the player input is locked out; the device array is read only so i cant forcibly say “This is your new device”.
DeviceRegained and DeviceLost are only applicable to the player input’s devices; so if i unplugged my keyboard/mouse it would say the device has been lost or found.
Edit; I have a temporary solution which doesn’t seem to impact performance, using the InputSystem.onDeviceChanged i just reconstruct the PlayerInput with the connected device
To get automatic switching, you’ll need control schemes. If there’s multiple control schemes and only a single PlayerInput, it should switch automatically when a compatible device is plugged in and used (the latter is important; the system won’t switch to a device where the user isn’t pressing buttons or sticks).
Note that you can also alter pairing manually at the API level using InputUser. PlayerInput has no wrapper API for that ATM but you can just grab PlayerInput.user and use the various pairing functions that InputUser has (PerformPairingWithDevice, UnpairDevices, etc).
I will quickly look at setting a secondary scheme and just letting the user construct with “first available”.
If i was to manage the pairing myself would I need to first unpair the current device and pair the new one? Or would simply pairing overwrite the existing? (I assume it gets put into that InputDevice array though)
It’s up to you.
// Switch device on a player.
InputUser.PerformPairingWithDevice(
Keyboard.current,
user: player.user,
options: InputUserPairingOptions.UnpairCurrentDevicesFromUser);
// Add a device to a player.
InputUser.PerformPairingWithDevice(
Keyboard.current,
user: player.user);
Ah didn’t pay attention to the options.
I may need to do that route however since despite having two control schemes and all assigned up (keyboard required for one and gamepad required for the other, all of the inputs that are specific to each device separated), the input did not switch. That was just from instantiating the object without passing any devices to it, it would however still spawn with the keyboard and mouse.
I’m actually just did something exactly like this right now. One thing I found a bit tricky to figure out was that AFTER performing this pairing, I also needed to manually trigger the control scheme being updated using:
player.user.ActivateControlScheme(null);
I can see the logic behind this in hindsight, but I think it might be good to point out somewhere in documentation. (Or did I miss this somewhere?) Note that I haven’t really looked into controlSchemes at all since I only want to use the "default’ schemes until my game is further along.
EDIT: Actually, it looks like while this works when I add a controller to a KBM player, when I add a mouse to a controller player it doesn’t work. (Though adding a keyboard DOES work)
So I’m confused now!