Skip to content

Commit

Permalink
Make instances deinit when player leaves (#1889)
Browse files Browse the repository at this point in the history
* make instances deinit when player leaves

* Revert

* Fix

* Fix

* Fix
  • Loading branch information
thegrb93 authored Nov 2, 2024
1 parent 6f56e59 commit 927d962
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lua/starfall/editor/sfframe.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2079,7 +2079,7 @@ function PANEL:UpdatePlayers(players)

local svtotal = 0
local cltotal = 0
for instance, _ in pairs(SF.playerInstances[ply]) do
for instance, _ in pairs(rawget(SF.playerInstances, ply) or {}) do
svtotal = svtotal + (instance.entity:IsValid() and instance.entity:GetNWInt("CPUus") or 0)
cltotal = cltotal + instance.cpu_average
end
Expand Down
13 changes: 7 additions & 6 deletions lua/starfall/instance.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ SF.Instance.__index = SF.Instance
--- A set of all instances that have been created. It has weak keys and values.
-- Instances are put here after initialization.
SF.allInstances = {}
SF.playerInstances = setmetatable({}, {__index = function() return {} end})
SF.playerInstances = SF.EntityTable("playerInstances", function(ply, instances)
for instance in pairs(instances) do
instance:Error({message = "Player disconnected!", traceback = ""})
end
end)
getmetatable(SF.playerInstances).__index = function(t,k) local r={} t[k]=r return r end

local plyPrecacheTimeBurst = SF.BurstObject("model_precache_time", "Model precache time", 5, 0.2, "The rate allowed model precache time regenerates.", "Amount of allowed model precache time.")

Expand Down Expand Up @@ -608,11 +613,7 @@ function SF.Instance:initialize()
self.cpu_softquota = 1

SF.allInstances[self] = true
if rawget(SF.playerInstances, self.player) then
SF.playerInstances[self.player][self] = true
else
SF.playerInstances[self.player] = {[self] = true}
end
SF.playerInstances[self.player][self] = true

self:RunHook("initialize")

Expand Down

0 comments on commit 927d962

Please sign in to comment.