From 0de76f1be5518f23fc6a73f06b9cf97f6de2b667 Mon Sep 17 00:00:00 2001 From: Titouan Guionneau Date: Fri, 22 Dec 2023 18:52:55 +0100 Subject: [PATCH 1/4] Added CallOnRemove on entity to stop emiting sound --- lua/entities/gmod_wire_expression2/core/sound.lua | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lua/entities/gmod_wire_expression2/core/sound.lua b/lua/entities/gmod_wire_expression2/core/sound.lua index 4b38944c3c..5e438da333 100644 --- a/lua/entities/gmod_wire_expression2/core/sound.lua +++ b/lua/entities/gmod_wire_expression2/core/sound.lua @@ -303,6 +303,17 @@ local function EmitSound(e2, ent, snd, level, pitch, volume) level = maxlevel end + local emitting_sounds = ent:GetVar("E2_emitting_sounds", {}) + table.insert(emitting_sounds, snd) + ent:SetVar("E2_emitting_sounds", emitting_sounds) + + ent:CallOnRemove("E2_EmitSound_stop_all", function() + local emitting_sounds = ent:GetVar("E2_emitting_sounds", {}) + for _, snd in pairs(emitting_sounds) do + ent:StopSound(snd) + end + end) + ent:EmitSound(snd, level, pitch, volume) end From 89157494cc12a8571ca66a4342544248c85c7dbc Mon Sep 17 00:00:00 2001 From: Titouan Guionneau Date: Fri, 22 Dec 2023 20:18:36 +0100 Subject: [PATCH 2/4] return if nil & use ipairs instead of pairs --- lua/entities/gmod_wire_expression2/core/sound.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/core/sound.lua b/lua/entities/gmod_wire_expression2/core/sound.lua index 5e438da333..78cdc27d14 100644 --- a/lua/entities/gmod_wire_expression2/core/sound.lua +++ b/lua/entities/gmod_wire_expression2/core/sound.lua @@ -308,8 +308,9 @@ local function EmitSound(e2, ent, snd, level, pitch, volume) ent:SetVar("E2_emitting_sounds", emitting_sounds) ent:CallOnRemove("E2_EmitSound_stop_all", function() - local emitting_sounds = ent:GetVar("E2_emitting_sounds", {}) - for _, snd in pairs(emitting_sounds) do + local emitting_sounds = ent:GetVar("E2_emitting_sounds") + if not emitting_sounds then return end + for _, snd in ipairs(emitting_sounds) do ent:StopSound(snd) end end) From c27b3b2f6a415a762c754eb70dae17a3db28227b Mon Sep 17 00:00:00 2001 From: Titouan Guionneau Date: Sat, 23 Dec 2023 15:38:01 +0100 Subject: [PATCH 3/4] Reduced size of E2_EmitSound_stop_all table --- lua/entities/gmod_wire_expression2/core/sound.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/core/sound.lua b/lua/entities/gmod_wire_expression2/core/sound.lua index 78cdc27d14..e13b9a9b41 100644 --- a/lua/entities/gmod_wire_expression2/core/sound.lua +++ b/lua/entities/gmod_wire_expression2/core/sound.lua @@ -304,14 +304,16 @@ local function EmitSound(e2, ent, snd, level, pitch, volume) end local emitting_sounds = ent:GetVar("E2_emitting_sounds", {}) - table.insert(emitting_sounds, snd) + emitting_sounds[snd] = (emitting_sounds[snd] or 0) + 1 ent:SetVar("E2_emitting_sounds", emitting_sounds) ent:CallOnRemove("E2_EmitSound_stop_all", function() local emitting_sounds = ent:GetVar("E2_emitting_sounds") if not emitting_sounds then return end - for _, snd in ipairs(emitting_sounds) do - ent:StopSound(snd) + for snd, count in pairs(emitting_sounds) do + for i = 1, count do + ent:StopSound(snd) + end end end) From 67ccb9b14dea6127e91d8930534395d839722318 Mon Sep 17 00:00:00 2001 From: thegrb93 Date: Sat, 30 Dec 2023 14:43:12 -0500 Subject: [PATCH 4/4] Limit number of calls --- lua/entities/gmod_wire_expression2/core/sound.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/entities/gmod_wire_expression2/core/sound.lua b/lua/entities/gmod_wire_expression2/core/sound.lua index e13b9a9b41..19554eb09e 100644 --- a/lua/entities/gmod_wire_expression2/core/sound.lua +++ b/lua/entities/gmod_wire_expression2/core/sound.lua @@ -304,7 +304,7 @@ local function EmitSound(e2, ent, snd, level, pitch, volume) end local emitting_sounds = ent:GetVar("E2_emitting_sounds", {}) - emitting_sounds[snd] = (emitting_sounds[snd] or 0) + 1 + emitting_sounds[snd] = math.min((emitting_sounds[snd] or 0) + 1, 32) ent:SetVar("E2_emitting_sounds", emitting_sounds) ent:CallOnRemove("E2_EmitSound_stop_all", function()