Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Combobox ConVar change callback deletion #1075

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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