From ba635b91e7e15f14027bf56187ba26fd196ba928 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Thu, 24 Nov 2022 17:16:18 +1100 Subject: [PATCH] fix(client/addKeybind): get control button when indexing currentKey Changing the key binding for a command invalidates the previous result from GetControlInstructionalButton. Use an index metamethod for backwards compatibility and add the getCurrentKey method. --- imports/addKeybind/client.lua | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/imports/addKeybind/client.lua b/imports/addKeybind/client.lua index 1ba0be8..b9449c7 100644 --- a/imports/addKeybind/client.lua +++ b/imports/addKeybind/client.lua @@ -10,6 +10,7 @@ ---@class CKeybind : KeybindProps ---@field currentKey string +---@field getCurrentKey fun(): string local keybinds = {} @@ -18,6 +19,28 @@ local function disableKeybind(self, toggle) end local IsPauseMenuActive = IsPauseMenuActive +local GetControlInstructionalButton = GetControlInstructionalButton +local joaat = joaat + +local keybind_mt = { + disabled = false, +} + +function keybind_mt:__index(index) + local value = keybind_mt[index] + + if value then + return value + end + + if index == 'currentKey' then + return self:getCurrentKey() + end +end + +function keybind_mt:getCurrentKey() + return GetControlInstructionalButton(0, joaat('+' .. self.name) | 0x80000000, true):sub(3) +end ---@param data KeybindProps ---@return CKeybind @@ -41,10 +64,9 @@ function lib.addKeybind(data) TriggerEvent('chat:removeSuggestion', ('/-%s'):format(data.name)) end) - data.currentKey = GetControlInstructionalButton(0, joaat('+' .. data.name) | 0x80000000, true):sub(3) - data.disabled = data.disabled or false + data.disabled = data.disabled data.disable = disableKeybind - keybinds[data.name] = data + keybinds[data.name] = setmetatable(data, keybind_mt) ---@cast data -KeybindProps return data end