How do you implement customizable controls? I'd like to make a menu the player could go into and rebind their controls, and I'd like to save those controls to local storage for future playthroughs.
For keyboard you want to look at keycodes. Create variables or an array where you store the keycodes for each button your game uses.
Var_JumpKey = 32 (32 is the spacebar keycode)
If by default you have made the space bar "jump" in your game, instead of using an "ON Spacebar Pressed" event, you use an "on Keycode Pressed" and for the keycode value, reference the Var_JumpKey variable where you stored the keycode.
If you want to change the jump key to shift, you create a system where the Var_JumpKey is updated to the keycode of the shift key (16) when it is pressed.
At a basic level, something like this:
On Any Key Pressed - Set Var_JumpKey to Keyboard.LastKeyCode
The process for gamepads is basically the same except you use button indexes instead of keycodes.
Awesome, I have rebindable controls for keyboard working now and am in progress of setting up gamepad support.
Is it possible to convert a gamepad's button index to the button name, in the same way the Keyboard behavior can convert a Key Code to a string? For example, show A/B/X/Y/L/R as the name of the button instead of the numerical index
You want to use an array and in X,Y0 store the button indexes, then in X,Y1 store the corresponding names.
Then set up a system to match the correct button index (based on the index of the button pressed) in that array and reference the adjacent value to display.
Y0 - 0,1,2,3
Y1 - A,B,X,Y
Develop games in your browser. Powerful, performant & highly capable.
Would it still work if I only used 1 row for the array, and referred to the cell number instead?
If you just want to do gamepads then yes it is easy to do a single row, but it would get whacky for keyboard codes because there are a lot of them and they don't go sequentially from 0 upwards. Typically starts at 8 (backspace) and goes to 222 (single quote) with lots of gaps in between.
i.e. near the end it goes 190,191,192,219,220,221,222
easier to just plonk both in the array and reference one from the other.