Respawn map-spawned coop pickups

This commit is contained in:
2026-09-06 18:03:55 +02:00
parent 4f1b6758ba
commit 7aea9a3749
+17 -2
View File
@@ -94,6 +94,21 @@ local function IsRespawnable(ent)
return string.StartWith(className, "item_") or string.StartWith(className, "weapon_")
end
local function IsMapWorldPickup(ent)
if not IsRespawnable(ent) then return false end
if ent:CreatedByMap() or ent.CustomCoopRespawnTemplate then return true end
-- point_template and other Hammer spawners create new entities at runtime,
-- so GMod reports CreatedByMap() as false for them. Their mapper-assigned
-- targetname survives, which lets us include them without also making
-- player-spawned or anonymous NPC-dropped weapons permanently respawn.
if IsValid(ent:GetOwner()) or IsValid(ent:GetParent()) or IsValid(ent:GetCreator()) then
return false
end
return ent:GetName() ~= ""
end
local function CaptureTemplate(ent)
local bodygroups = {}
for _, bodygroup in ipairs(ent:GetBodyGroups() or {}) do
@@ -200,7 +215,7 @@ end
hook.Add("OnEntityCreated", "CustomCoopTrackMapItems", function(ent)
-- OnEntityCreated fires before keyvalues and map-created state are ready.
timer.Simple(0, function()
if IsRespawnable(ent) and ent:CreatedByMap() then
if IsMapWorldPickup(ent) then
TrackItem(ent)
end
end)
@@ -208,7 +223,7 @@ end)
local function TrackExistingItems()
for _, ent in ipairs(ents.GetAll()) do
if IsRespawnable(ent) and (ent:CreatedByMap() or ent.CustomCoopRespawnTemplate) then
if IsMapWorldPickup(ent) then
TrackItem(ent)
end
end