Skip to content

Commit

Permalink
Merge pull request #9 from TwistedTail/fix-links-error
Browse files Browse the repository at this point in the history
Fixed missing ENT._links error
  • Loading branch information
TwistedTail authored Jun 28, 2024
2 parents 839af48 + 1f81873 commit c1a3823
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
9 changes: 6 additions & 3 deletions lua/cfw/classes/link_sv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,13 @@ do
end

function ENT:GetLinks() -- Creates a shallow copy of the links table
local out = {}
local links = self._links
local out = {}

for k, v in pairs(self._links) do
out[k] = v
if links then
for k, v in pairs(links) do
out[k] = v
end
end

return out
Expand Down
7 changes: 5 additions & 2 deletions lua/cfw/core/connectivity_sv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ local function floodFill(source, sinkIndex)

if entIndex == sinkIndex then return true, closed end

for neighborIndex, _ in pairs(Entity(entIndex)._links) do -- neighborIndex, neighborLink
for neighborIndex in pairs(Entity(entIndex)._links) do -- neighborIndex, neighborLink
if not closed[neighborIndex] then
open[neighborIndex] = true
end
Expand Down Expand Up @@ -65,7 +65,10 @@ end
function CFW.disconnect(entA, indexB)
if entA:EntIndex() == indexB then return end -- Should not happen normally, but ragdolls allow you to constrain to other bones on the same ragdoll, and it is the same entity

local link = entA._links[indexB]
local link = entA._links[indexB]

if not link then return end -- There's nothing to disconnect here

local contraptionPopped = link:Sub()

if contraptionPopped then return end
Expand Down
16 changes: 15 additions & 1 deletion lua/cfw/core/parenting_sv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,18 @@ hook.Add("Initialize", "CFW", function()
end)

hook.Remove("Initialize", "CFW")
end)
end)

-- In order to prevent NULL entities flooding the ENT._links table, we'll just get rid of them before they get removed
-- This is a fix for a really annoying issue that was showing up in multiple different ways
hook.Add("EntityRemoved", "cfw.entityRemoved", function(ent)
if not IsValid(ent) then return end

local links = ent:GetLinks()

if not links then return end

for index in pairs(links) do
disconnect(ent, index)
end
end)

0 comments on commit c1a3823

Please sign in to comment.