Skip to content

Commit

Permalink
fix: Combobox ConVar change callback deletion (#1075)
Browse files Browse the repository at this point in the history
  • Loading branch information
saibotk authored Oct 16, 2023
1 parent 0270844 commit b0873db
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ All notable changes to TTT2 will be documented here. Inspired by [keep a changel
- Scoreboard now sets preferred player volume and mute state in client's new `ttt2_voice` table (by @EntranceJew)
- Keyed by steamid64, making it more reliable than UniqueID or the per-session mute and volume levels.

### Fixed

- Fixed removing the convar change callback in `DComboboxTTT2` (by @saibotk)

## [v0.11.7b](https://github.com/TTT-2/TTT2/tree/v0.11.7b) (2022-08-27)

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,23 +338,29 @@ end

local convarTracker = 0
---
-- @param Panel menu to set the value of
-- @param Panel panel to set the value of
-- @param string conVar name of the convar
local function AddConVarChangeCallback(menu, conVar)
convarTracker = convarTracker % 1023 + 1
local myIdentifierString = "TTT2F1MenuConVarChangeCallback" .. tostring(convarTracker)

local function OnConVarChangeCallback(conVarName, oldValue, newValue)
if not IsValid(menu) then
cvars.RemoveChangeCallback(conVarName, myIdentifierString)
local function AddConVarChangeCallback(panel, conVar)
convarTracker = convarTracker + 1
local myIdentifierString = "TTT2F1MenuComboboxConVarChangeCallback" .. tostring(convarTracker)

local callback = function(conVarName, oldValue, newValue)
if not IsValid(panel) then
-- We need to remove the callback in a timer, because otherwise the ConVar change callback code
-- will throw an error while looping over the callbacks.
-- This happens, because the callback is removed from the same table that is iterated over.
-- Thus, the table size changes while iterating over it and leads to a nil callback as the last entry.
timer.Simple(0, function()
cvars.RemoveChangeCallback(conVarName, myIdentifierString)
end)

return
end

menu:SetValue(newValue, true)
panel:SetValue(newValue, true)
end

cvars.AddChangeCallback(conVar, OnConVarChangeCallback, myIdentifierString)
cvars.AddChangeCallback(conVar, callback, myIdentifierString)
end

---
Expand Down

0 comments on commit b0873db

Please sign in to comment.