|
| 1 | +--- |
| 2 | +author: TheZoroForce240 |
| 3 | +desc: This page explains how to create custom controls for your mod! |
| 4 | +lastUpdated: 2025-08-05T18:51:29.793Z |
| 5 | +title: Custom Controls |
| 6 | +--- |
| 7 | +# Custom Controls |
| 8 | +Custom controls can be easily added to your own mod and automatically setup inside the controls menu! (so you don't have to code it yourself) |
| 9 | + |
| 10 | +To get started, put a file in `./data/config` called `controls.xml`. |
| 11 | + |
| 12 | +Here is an example xml that you can copy from: |
| 13 | + |
| 14 | +```xml |
| 15 | +<controls> |
| 16 | + <category name="TEST CATEGORY"> |
| 17 | + <control name="dodge" saveName="dodge" menuName="DODGE" keyP1="SPACE" keyP2=""/> |
| 18 | + </category> |
| 19 | + <category name="SIX KEY"> |
| 20 | + <control name="6k0" saveName="control_6k0" menuName="LEFT" keyP1="S" keyP2="" menuIcon="game/notes/default" menuAnim="purple0"/> |
| 21 | + <control name="6k1" saveName="control_6k1" menuName="UP" keyP1="D" keyP2="" menuIcon="game/notes/default" menuAnim="green0"/> |
| 22 | + <control name="6k2" saveName="control_6k2" menuName="RIGHT" keyP1="F" keyP2="" menuIcon="game/notes/default" menuAnim="red0"/> |
| 23 | + <control name="6k3" saveName="control_6k3" menuName="LEFT" keyP1="J" keyP2="LEFT" menuIcon="game/notes/default" menuAnim="purple0"/> |
| 24 | + <control name="6k4" saveName="control_6k4" menuName="DOWN" keyP1="K" keyP2="DOWN" menuIcon="game/notes/default" menuAnim="blue0"/> |
| 25 | + <control name="6k5" saveName="control_6k5" menuName="RIGHT" keyP1="L" keyP2="RIGHT" menuIcon="game/notes/default" menuAnim="red0"/> |
| 26 | + </category> |
| 27 | +</controls> |
| 28 | +``` |
| 29 | + |
| 30 | +<img src="./Custom Controls.png" alt="Image showing the custom controls based on the example above"/> |
| 31 | + |
| 32 | +You want to start by creating a <syntax lang="xml"><category></syntax> node, which will include any options inside that section while in the menu (as shown in the screenshot) |
| 33 | + |
| 34 | +The <syntax lang="xml"><category></syntax> only has one available property: |
| 35 | +- `name` - This is the name that will appear for the category in the controls menu |
| 36 | + |
| 37 | +Each <syntax lang="xml"><control></syntax> node can then be added as a subnode of the category |
| 38 | + |
| 39 | +## <h2 id="properties" sidebar="Properties">Here's the list of properties for each control:</h2> |
| 40 | + |
| 41 | +- `name` - This name is what will be used to access the control later |
| 42 | +- `saveName` - This is the name used with `FlxG.save.data` for the control to be stored in, make sure to name this carefully and not break any other save data! |
| 43 | +- `menuName` - This is the name that will be displayed inside the menu |
| 44 | +- `keyP1`, `keyP2` - These are what the control key binding will default to, this name will need to be matching with a `FlxKey` name to correctly set, and can be set to `""` to default to none. The `P1` and `P2` determine which player uses the binding in co-op mode (when checking `.controls` on the `StrumLine`) or when checking directly through `controlsP1` or `controlsP2`, and will act as 2 bindings by default just like any other control. |
| 45 | +- `menuIcon` - This is an image path to a custom control icon that can be used in the menu, must be a sparrow xml spritesheet! (optional) |
| 46 | +- `menuAnim` - This is the animation prefix for the spritesheet set by `menuIcon` (optional, but required if using `menuIcon`) |
| 47 | + |
| 48 | +Accessing in scripts: |
| 49 | + |
| 50 | +The control state can be checked multiple ways, typically just by calling `controls` (assuming that you're in a `MusicBeatState`), and then calling the member functions `getJustPressed(name)`, `getJustReleased(name)`, or `getPressed(name)`. |
| 51 | + |
| 52 | +Per player controls can be accessed with `controlsP1` and `controlsP2`, and per strumline controls can be accessed with `.controls` on any `StrumLine` (example: `strumLines.members[0].controls`), which will change between `P1`/`P2` controls when using co-op mode (only while in `PlayState`). |
| 53 | + |
| 54 | +Example usage of accessing a control inside of a script: |
| 55 | + |
| 56 | +```haxe |
| 57 | +function update(elapsed) { |
| 58 | + //global |
| 59 | + if (controls.getJustPressed("dodge")) { |
| 60 | + trace("dodge!"); |
| 61 | + } |
| 62 | + if (controls.getJustReleased("dodge")) { |
| 63 | + trace("released dodge!"); |
| 64 | + } |
| 65 | + if (controls.getPressed("dodge")) { |
| 66 | + trace("holding dodge!"); |
| 67 | + } |
| 68 | +
|
| 69 | + //per player |
| 70 | + if (controlsP1.getJustPressed("dodge")) { |
| 71 | + trace("p1 dodge!"); |
| 72 | + } |
| 73 | + if (controlsP2.getJustPressed("dodge")) { |
| 74 | + trace("p2 dodge!"); |
| 75 | + } |
| 76 | +
|
| 77 | + //per strumline (changes on co-op mode) |
| 78 | + if (strumLines.members[0].controls.getJustPressed("dodge")) { |
| 79 | + trace("strumline0 dodge!"); |
| 80 | + } |
| 81 | + if (strumLines.members[1].controls.getJustPressed("dodge")) { |
| 82 | + trace("strumline1 dodge!"); |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +## Multikey Control Bindings |
| 88 | + |
| 89 | +You may notice that if you change a strumline's key count the bindings will not work by default. |
| 90 | + |
| 91 | +This can be easily solved by setting up controls in a specific way inside the controls xml. |
| 92 | + |
| 93 | +All you need to do is have the `name` property of the controls to be set to `keyCount`k`strumID` for each one, for example `name="5k0"` will be for the first strum when the key count is 5. |
| 94 | + |
| 95 | +If you've named it correctly you should have the bindings automatically set for that key count! |
0 commit comments