Posted: Sun Jul 17, 2022 9:49 pm Post subject: Cycle through list options with hotkey
Dark Byte has previously provided a tutorial on how to toggle bit values on hotkey using [value~1] as the value to set.
Here is a very simply way to cycle through a list of options, which I think is a much more common scenario:
Hotkey: Enter your selected hotkey/combo.
On Hotkey: Set Value To
Value: [(value < 259) and (value + 1) or 0]
In this example, 259 is the maximum number of items in the dropdown list, 0 is the first item, and values are being incremented by 1.
Technically that explanation is incorrect. 259 is the maximum allowable value for the field and 0 is the lowest. One can use this kind of hotkey setup even if you don't have a dropdown list. If the current value is 259, the hotkey will automatically cycle back to 0 without having to write a script to do so. You can have another hotkey to decrement values in a similar way.
Anything within square brackets is interpreted as Lua code so, within certain limitations, one can take this basic concept quite far with hotkeys.
If you are new to CE, note that memrecs always work with the actual value. It does not matter whether your dropdown list displays values and/or only descriptive text. Memrecs don't care about what the dropdown list displays. You are always working with the underlying value which is usually a number.
For those coming from other programming backgrounds, the 'and -- or' setup here works exactly like the (condition ? if-true : if-false) scenario in some other languages. _________________
Modular arithmetic is another way of getting this same behaviour. i.e. `[(value + 1) % 260]`
Nice. Didn't think of that. The conditional formula is probably easier for people to read and interpret though.
-------------
Forgot to mention that this only works if all your list items are equidistant -- in these examples, the distance is 1. But maybe you have a list that is 0,4,8,12,16,etc. You will want to increment values by 4, but the formula will still work. In fact, any mathematical formula which will let you reach the next item will work. So maybe your distance isn't just a plain old +1 or +4. Maybe it's value*(1+value).
However, let's say you determine that Item 12 is buggy and causes the game to crash. So you remove that option from the dropdown list. The formula will still put in 12 after 8. It won't jump to 16. The hotkey is not actually cycling through the dropdown options, it is only increasing/decreasing the actual value of the memory record. _________________
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum