Adding a new command to keybindings

MCMLXXXIVMCMLXXXIV Join Date: 2010-04-14 Member: 71400Members
edited January 2011 in Modding
I've hit a brick wall when trying to make a brand new command that can be bound to a key, so thought I'd post my steps so far (they can kind of double as a guide) and see if anyone else has run into this problem yet. As usual, the bits that I thought were going to be hard were quite easy (thanks UWE!) and now the bit that I thought would be easy has got me stumped...

<b>Step 1: Define your new key in the key bindings menu</b>
Choose a name for your new command (I chose "ToggleTechUpgrades", Edit BindingsDialog.lua to add your key:

<ol type='1'><li>Edit the variable "defaults" to add a new record for your new key. This sets the default value that shows up.</li><li>Edit the variable "globalControlBindings" to add a new record for your key (you can also define a category in there). This shows the entry in the menu and allows you to set the key up.</li></ol>

<b>Step 2: Pick up the keypresses</b>
Based on the existing sayings menu, there is a place in Player.lua that allows the menu toggle to be pressed and intercepts the number keys to allow you to select items from the menu.

<i>To test for your key:</i>
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->if (bit.band(input.commands, Move.ToggleSayings2) ~= 0)) then
  // The key was pressed.
end<!--c2--></div><!--ec2-->

<i>Afterwards you might want to "eat" the keypress so that nothing else will pick it up:</i>
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->local removeToggleTechUpgradesMask = bit.bxor(0xFFFFFFFF, Move.ToggleSayings2)
// Nom nom nom
input.commands = bit.band(input.commands, removeToggleTechUpgradesMask)<!--c2--></div><!--ec2-->

The bit I am stuck on is step 3:
<b>Add a new command to the "Move" bitmask enum, or some other method, to allow the keypress to be detected even if the user changes the key from the bindings menu</b>

It looks like the key bindings are stored as a bitmask, which can be decoded using an enum called "Move". All of the custom functions (e.g. weapon1, togglesayings1, etc.) are defined there too. Unfortunately, It is defined on the C++ side and I can't find a way to add a command to this bitmask (unless there's a function that I missed in my code trawl today).

This means that to allow users to customise my "Player Upgrades" menu key I will need to <strike>write a hacky key handler that looks at the currently bound key via BindingsDialog.lua and check the bitmask based on the standard Move.A, Move.B elements in the mask</strike>, No, I can't see any options. Move.A etc. return the ASCII code, which bears no relation to the input.commands variable!

Can anyone see a way around this?

Comments

  • fsfodfsfod uk Join Date: 2004-04-09 Member: 27810Members, NS2 Developer, Constellation, NS2 Playtester, Squad Five Blue, Squad Five Silver, Squad Five Gold, Subnautica Playtester, NS2 Community Developer, Pistachionauts
    The Move bit mask is limited to 32 bits you could just repurpose some of the commander bits. Theres also the hotkey field of the move object that stores the last letter key pressed thats still down but if you really want you could use the keybind system i created to avoid these limitations.
  • MCMLXXXIVMCMLXXXIV Join Date: 2010-04-14 Member: 71400Members
    Thanks for the reply fsfod! It took a little trawl of this forum but I found the keybinding code you were referring to.

    I checked out the key handler code and it looks like it could be useful - at the moment I'm trying not to diverge too far from vanilla NS2 so as you say I can probably bind it to one of the commander hotkeys (why didn't I think of that??). In the future I may consider merging in your key handler if I hit a brick wall. For my alpha version it's just using the sayings #2 keybind.
Sign In or Register to comment.