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 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.
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:
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.