ModdedMeghan Posted November 17, 2021 Share Posted November 17, 2021 (edited) So I'm working on a mod and it's possible to get a perk at the end that basically lets the player cast the detect life spell whenever.By default you do this by holding the 'B' key. However I'd like to let the player rebind it to whatever key they want but I'm having a hell of a time figuring out how, and searching on google and the forums for the last hour or so has gotten me nowhere.I know of course I could write a mile long script that says something like Event OnInIt()RegisterForKey( 8 )RegisterForKey(9)RegisterForKey(13)...RegisterForKey(259)RegisterForKey(260)EndEventAnd then have an even longer slew of if statements but I'd really really like to avoid doing that if it's possible I thought maybe I could use an array but they top out at 128 and the key codes go up to 260 so that's out. So is there an easier way to achieve this? Or am I gonna be typing an if block from 8 to 260 for a few hours? Thanks in advance to anyone who can help! Edited November 17, 2021 by ModdedMeghan Link to comment Share on other sites More sharing options...
DieFeM Posted November 17, 2021 Share Posted November 17, 2021 Disregarding that I think you should register the key when the user selects the key to bind (if you use MCM you should be using OnMCMSettingChange to unregister and register for the new key), if you still want to register for every single key you could use a while loop: Event OnQuestInit() Int Keycode = 8 While Keycode <= 260 RegisterForKey(Keycode) Keycode += 1 EndWhile EndEvent Link to comment Share on other sites More sharing options...
LarannKiar Posted November 17, 2021 Share Posted November 17, 2021 I respect your scripting choices and don't want to tell you how to write your code but registering a script for more than 200 keys seems a bit overkill. You really sould consider using MCM events for a project like this as suggested above. Link to comment Share on other sites More sharing options...
ModdedMeghan Posted November 17, 2021 Author Share Posted November 17, 2021 Disregarding that I think you should register the key when the user selects the key to bind (if you use MCM you should be using OnMCMSettingChange to unregister and register for the new key), if you still want to register for every single key you could use a while loop:Event OnQuestInit() Int Keycode = 8 While Keycode <= 260 RegisterForKey(Keycode) Keycode += 1 EndWhileEndEventI feel kinda silly for not thinking of a while loop lol And as for mcm I'm gonna be honest and say I ain't the sharpest tool in the shed and mcm has given me trouble as a result About a month ago I was trying to make an mcm menu for a couple of settings but I couldn't even get a basic on off switch working. The documentation I had found I didn't find very helpful either. So I just got too frustrated with the whole thing and just gave up and started making a settings holotape instead Link to comment Share on other sites More sharing options...
niston Posted November 17, 2021 Share Posted November 17, 2021 Just store the code of the key you want to bind in a variable.Then register for that key and that key only. When changing it, unregister <code in variable>, update the variable and register <new code in variable>. A Global could work as well as a variable, depending on your design. Also keep in mind that there might be menus open (terminal, holotape, crafting, etc) and reacting to the hotkey while they are open might be an exceptionally non-bright idea. Link to comment Share on other sites More sharing options...
ModdedMeghan Posted November 17, 2021 Author Share Posted November 17, 2021 Just store the code of the key you want to bind in a variable.Then register for that key and that key only. When changing it, unregister <code in variable>, update the variable and register <new code in variable>. A Global could work as well as a variable, depending on your design. Also keep in mind that there might be menus open (terminal, holotape, crafting, etc) and reacting to the hotkey while they are open might be an exceptionally non-bright idea.So yeah I'm already storing it with a global cause I have a couple different scripts that need to access it and that just makes my life easier The issue is when I let the player rebind I don't know how to detect the new key they want without basically registering for everything cause if you haven't actually registered for a particular key you can press it all day and f4se won't do anything Link to comment Share on other sites More sharing options...
LarannKiar Posted November 17, 2021 Share Posted November 17, 2021 I feel kinda silly for not thinking of a while loop lol And as for mcm I'm gonna be honest and say I ain't the sharpest tool in the shed and mcm has given me trouble as a result About a month ago I was trying to make an mcm menu for a couple of settings but I couldn't even get a basic on off switch working. The documentation I had found I didn't find very helpful either. So I just got too frustrated with the whole thing and just gave up and started making a settings holotape instead I always use settings holotapes myself so I can understand that :) Just store the code of the key you want to bind in a variable.Then register for that key and that key only. When changing it, unregister <code in variable>, update the variable and register <new code in variable>. A Global could work as well as a variable, depending on your design. Also keep in mind that there might be menus open (terminal, holotape, crafting, etc) and reacting to the hotkey while they are open might be an exceptionally non-bright idea.So yeah I'm already storing it with a global cause I have a couple different scripts that need to access it and that just makes my life easier The issue is when I let the player rebind I don't know how to detect the new key they want without basically registering for everything cause if you haven't actually registered for a particular key you can press it all day and f4se won't do anything If you just temporarily register your script for the keys, it will be fine. (I.e. if you have a terminal menu button "Change Perk Key" >> the user presses this button >> script registers for the keys, waits for the new key to be pressed >> the user presses a key >> the new key is now stored >> the script unregisters immediatelly). Link to comment Share on other sites More sharing options...
ModdedMeghan Posted November 17, 2021 Author Share Posted November 17, 2021 I feel kinda silly for not thinking of a while loop lol And as for mcm I'm gonna be honest and say I ain't the sharpest tool in the shed and mcm has given me trouble as a result About a month ago I was trying to make an mcm menu for a couple of settings but I couldn't even get a basic on off switch working. The documentation I had found I didn't find very helpful either. So I just got too frustrated with the whole thing and just gave up and started making a settings holotape instead I always use settings holotapes myself so I can understand that :) Just store the code of the key you want to bind in a variable.Then register for that key and that key only. When changing it, unregister <code in variable>, update the variable and register <new code in variable>. A Global could work as well as a variable, depending on your design. Also keep in mind that there might be menus open (terminal, holotape, crafting, etc) and reacting to the hotkey while they are open might be an exceptionally non-bright idea. So yeah I'm already storing it with a global cause I have a couple different scripts that need to access it and that just makes my life easier The issue is when I let the player rebind I don't know how to detect the new key they want without basically registering for everything cause if you haven't actually registered for a particular key you can press it all day and f4se won't do anything If you just temporarily register your script for the keys, it will be fine. (I.e. if you have a terminal menu button "Change Perk Key" >> the user presses this button >> script registers for the keys, waits for the new key to be pressed >> the user presses a key >> the new key is now stored >> the script unregisters immediatelly).Yeah looks like that's probably what I'm gonna end up doing, I had just hoped for a simpler way. But at least DieFeM reminding me that while loops exist will save me some trouble lol Link to comment Share on other sites More sharing options...
Recommended Posts