Can you check if a key is equal to an integer?

I’m making a hotbar system and inside my hotbar there are 8 slots. I want to check which key is being pressed and have it use the item in the corresponding slot.

Ex. Press 1 and it activates the item in slot 1, press 2 and it activates the item in slot 2, etc.

I know this is doable manually by Ctrl + V the function 8 times for each slot, but I’m just wondering if there’s a more efficient way to do this, as I don’t really like having inefficient scripts in my work.

You could make an array of all the numbers then see if the key pressed is in that array.

You still have to have 8 input keys. I don’t know if you can parse keys to Integers in blueprints, but even if you can, it will process all the key strokes, not just 1-8.

I’d make it this way: on key down you set an int variable to 1-8 respectively. Then I’d make a function that activates slots depending on the variable value. Thus you’ll have only 1 function and 8 key input events.

There’s a number of solutions to this:

  • make each slot a User Widget, have it carry a isFocusable flag, with its own index and have it detect key presses; since a slot is a self-contained entity, there’s no need to copy / paste anything. The functionality is handled by the slot instance.

or

  • create a Key | Widget (slot type) Map (dictionary) in the widget housing the slots, something like this:

And find it by key-press:

Ideally, combine both for the best results.


There are more ways, of course. If it’s supposed to be super simple, you could use what KitKat suggested (kind of since there’s no real need for an extra array). Get Key display name, convert to Int and fetch a child from the container holding the slots, by index. You’d need to add script to catch out-of-range ints.

Or convert key to Int and Switch on Int - this will result in delicious blueprint spaghetti, though.