Hello, I have a very specific use case in which I am trying to get this line of code from the old input system converted to the new input system. I am using an Input Actions asset to create my action maps.
This line of code reads the number of objects inside an array, then maps a number key on the keyboard to it, passing the index value in so it can print the object with the same index number as the number key pressed. I.e, If I hit ‘1’, it will read the index number of the first item in the array as ‘0’. If I hit the number ‘2’ key, it will read the index value of the second item in the array as ‘1’.
for (int i = 0; i < list.Length; i++)
{
if (Input.GetKeyDown((i + 1).ToString()))
{
Print(i);
}
}
How can I set up an Action inside my Action Map to mimic this sort of thing? Or, is this not currently possible with the new input system? If not, is there another way this can be done?
I don’t think you can use a for loop in the Input Action Asset graphic interface but you could potentially create them using code. Here is a reference that you may find interesting if your list does not have a fixed size: Actions | Input System | 1.1.1
If you do want to do it with the interface, I think your list should not vary in size and then you can set up bindings for each button. You could name the actions using numbers and get it via CallbackContext.actions.name. Then you can parse it to int and call appropriate method/find corresponding item in an array.
The true replacement for Input.GetKeyDown is the Keyboard device, not the Actions system.
ie:
if (Keyboard.current[Key.A].wasPressedThisFrame)
{
//do stuff
}
//more directly to the question
for(int i = (int)Key.Digit1; i <= (int)Key.Digit0; i++)
{
if (Keyboard.current[(Key)i].wasPressedThisFrame)
{
Debug.Log((Key)i);
}
}
You are free to enumerate the Key the same way as the old input system. (in this case Digit is enumerated based on key code order, which on QWERTY and most others starts at 1 and ends at 0)
If you do decide to use action assets, you can also use action.triggered to detect button presses as well. I don’t think you can loop through actions from an action map though.
To put it like the others
if (controls.Player.Jump.triggered)
{
//do stuff
}
It takes more set up on your end but also means you keyboard, gamepad, etc inputs are all called by the same code instead of if statements.
One of the big problems with relying on Actions for in-line polling like that is you can’t contextually know what event is responsible for the triggered flag. It could be press-Up or Down or a Hold. There are strong reasons to use either, but good for everyone to know what/when/why/where to use each