Skip to content

Commit

Permalink
Merge pull request #307 from GMLambda/fix-npcmaker
Browse files Browse the repository at this point in the history
Fix NPC maker not respecting spawn frequency -1
  • Loading branch information
ZehMatt authored Jul 20, 2024
2 parents a97e18a + 8fe431e commit fec5cbb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
0.9.26 (in development)
- Improved: Going in and out of the settings no longer clips into the player model.
- Fixed: NPC makers not respecting the spawn frequency -1 causing a lot of NPCs being spawned in some cases.

0.9.25
- Improved: Hints will no longer show duplicates, instead it will renew the existing hint that has the same text.
Expand Down
38 changes: 32 additions & 6 deletions entities/entities/lambda_npcmaker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,9 @@ function ENT:Initialize()
BaseClass.Initialize(self)
self:SetSolid(SOLID_NONE)
if self:GetNWVar("Disabled") == false then
self:NextThink(CurTime() + 0.1)
self.Think = self.MakerThink
self:Disable()
else
self.Think = self.StubThink
self:Enable()
end
end

Expand Down Expand Up @@ -275,7 +274,14 @@ end
function ENT:Enable()
self:SetNWVar("Disabled", false)
self.Think = self.MakerThink
self:NextThink(CurTime())
local spawnFrequency = self:GetNWVar("SpawnFrequency")
local curTime = CurTime()
if spawnFrequency ~= -1 then
self.NextSpawnTime = curTime + spawnFrequency
else
self.NextSpawnTime = curTime
end
self:NextThink(self.NextSpawnTime)
end

function ENT:Disable()
Expand Down Expand Up @@ -358,6 +364,8 @@ function ENT:CanMakeNPC(ignoreSolidEnts)
end

function ENT:StubThink()
self:NextThink(CurTime() + 1)
return true
end

--DbgPrint(self, "ENT:StubThink", ent)
Expand All @@ -369,9 +377,25 @@ function ENT:MakerThink()
self.CachedPlayerCount = player.GetCount()
end

self:NextThink(CurTime() + self:GetNWVar("SpawnFrequency"))
local curTime = CurTime()

if self.NextSpawnTime == -1 or self.NextSpawnTime > curTime then
self:NextThink(curTime + 0.1)
return true
end

-- Spawn the next NPC.
self:MakeNPC()

local spawnFrequency = self:GetNWVar("SpawnFrequency")
if spawnFrequency ~= -1 then
self.NextSpawnTime = curTime + spawnFrequency
self:NextThink(self.NextSpawnTime)
else
self.NextSpawnTime = -1
self:NextThink(curTime + 0.1)
end

return true
end

Expand All @@ -381,8 +405,10 @@ function ENT:DeathNotice(ent)
end

self:SetNWVar("LiveChildren", self:GetNWVar("LiveChildren") - 1)

if self:GetNWVar("SpawnFrequency") == -1 then
self:MakeNPC()
-- Allow it to spawn again.
self.NextSpawnTime = CurTime()
end

if self:GetNWVar("LiveChildren") <= 0 then
Expand Down

0 comments on commit fec5cbb

Please sign in to comment.