Skip to content

Commit 7c7a69a

Browse files
authored
Entity Markers refactor (#3459)
* Entity Marker cleanups * Cleanup advanced entity marker too * Don't need to check at this point (self.Marks now guaranteed to contain only valid entities) * Fix dupes + lint pass * This looks more clear for me * It's too simple to use PrepareOverlayData * Cleanup freezer too * Missing label * Missing space * Make it single * Use ipairs * Fix typo * Final cleanups
1 parent 74f0798 commit 7c7a69a

File tree

6 files changed

+290
-298
lines changed

6 files changed

+290
-298
lines changed
Lines changed: 87 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,84 @@
11
AddCSLuaFile()
2-
DEFINE_BASECLASS( "base_wire_entity" )
3-
ENT.PrintName = "Adv Wire Entity Marker"
4-
ENT.Author = "Divran"
2+
3+
DEFINE_BASECLASS("base_wire_entity")
4+
ENT.PrintName = "Adv Wire Entity Marker"
55
ENT.WireDebugName = "Adv EMarker"
66

7-
if CLIENT then return end -- No more client
7+
if CLIENT then return end
88

99
function ENT:Initialize()
10-
self:PhysicsInit( SOLID_VPHYSICS )
11-
self:SetMoveType( MOVETYPE_VPHYSICS )
12-
self:SetSolid( SOLID_VPHYSICS )
13-
10+
self:PhysicsInit(SOLID_VPHYSICS)
1411
self.Marks = {}
15-
local outputs = {"Entities [ARRAY]", "Nr"}
16-
for i=3,12 do
17-
outputs[i] = "Entity" .. (i-2) .. " [ENTITY]"
18-
end
19-
self.Inputs = WireLib.CreateInputs( self, {
12+
13+
WireLib.CreateInputs(self, {
2014
"Entity (This entity will be added or removed once the other two inputs are changed) [ENTITY]",
2115
"Add Entity (Change to non-zero value to add the entity specified by the 'Entity' input)",
2216
"Remove Entity (Change to non-zero value to remove the entity specified by the 'Entity' input)",
2317
"Clear Entities (Removes all entities from the marker)"
24-
} )
25-
self.Outputs = WireLib.CreateOutputs( self, outputs )
26-
self:SetOverlayText( "Number of entities linked: 0" )
18+
})
19+
20+
WireLib.CreateOutputs(self, {
21+
"Entities [ARRAY]",
22+
"Nr (Number of entities linked)",
23+
"Entity1 [ENTITY]",
24+
"Entity2 [ENTITY]",
25+
"Entity3 [ENTITY]",
26+
"Entity4 [ENTITY]",
27+
"Entity5 [ENTITY]",
28+
"Entity6 [ENTITY]",
29+
"Entity7 [ENTITY]",
30+
"Entity8 [ENTITY]",
31+
"Entity9 [ENTITY]",
32+
"Entity10 [ENTITY]"
33+
})
34+
35+
self:SetOverlayText("Number of entities linked: 0")
2736
end
2837

29-
function ENT:TriggerInput( name, value )
30-
if (name == "Entity") then
38+
function ENT:TriggerInput(name, value)
39+
if name == "Entity" then
3140
if IsValid(value) then
3241
self.Target = value
3342
end
34-
elseif (name == "Add Entity") then
35-
if IsValid(self.Target) then
36-
if (value ~= 0) then
37-
local bool, index = self:CheckEnt( self.Target )
38-
if (not bool) then
39-
self:LinkEnt( self.Target )
40-
end
41-
end
43+
elseif name == "Add Entity" then
44+
if value ~= 0 and IsValid(self.Target) and not self:CheckEnt(self.Target) then
45+
self:LinkEnt(self.Target)
4246
end
43-
elseif (name == "Remove Entity") then
44-
if IsValid(self.Target) then
45-
if (value ~= 0) then
46-
local bool, index = self:CheckEnt( self.Target )
47-
if (bool) then
48-
self:UnlinkEnt( self.Target )
49-
end
50-
end
47+
elseif name == "Remove Entity" then
48+
if value ~= 0 and IsValid(self.Target) and self:CheckEnt(self.Target) then
49+
self:UnlinkEnt(self.Target)
5150
end
52-
elseif (name == "Clear Entities") then
51+
elseif name == "Clear Entities" then
5352
self:ClearEntities()
5453
end
5554
end
5655

5756
function ENT:UpdateOutputs()
58-
-- Trigger regular outputs
59-
WireLib.TriggerOutput( self, "Entities", self.Marks )
60-
WireLib.TriggerOutput( self, "Nr", #self.Marks )
61-
62-
-- Trigger special outputs
63-
for i=3,12 do
64-
WireLib.TriggerOutput( self, "Entity" .. (i-2), self.Marks[i-2] )
65-
end
66-
67-
-- Overlay text
68-
self:SetOverlayText( "Number of entities linked: " .. #self.Marks )
69-
70-
-- Yellow lines information
57+
local marks = self.Marks
58+
WireLib.TriggerOutput(self, "Entities", marks)
59+
WireLib.TriggerOutput(self, "Nr", #marks)
60+
WireLib.TriggerOutput(self, "Entity1", marks[1])
61+
WireLib.TriggerOutput(self, "Entity2", marks[2])
62+
WireLib.TriggerOutput(self, "Entity3", marks[3])
63+
WireLib.TriggerOutput(self, "Entity4", marks[4])
64+
WireLib.TriggerOutput(self, "Entity5", marks[5])
65+
WireLib.TriggerOutput(self, "Entity6", marks[6])
66+
WireLib.TriggerOutput(self, "Entity7", marks[7])
67+
WireLib.TriggerOutput(self, "Entity8", marks[8])
68+
WireLib.TriggerOutput(self, "Entity9", marks[9])
69+
WireLib.TriggerOutput(self, "Entity10", marks[10])
70+
71+
self:SetOverlayText("Number of entities linked: " .. #marks)
7172
WireLib.SendMarks(self)
7273
end
7374

74-
function ENT:CheckEnt( ent )
75-
for index, e in pairs( self.Marks ) do
76-
if (e == ent) then return true, index end
75+
function ENT:CheckEnt(checkent)
76+
for index, ent in ipairs(self.Marks) do
77+
if checkent == ent then
78+
return true, index
79+
end
7780
end
81+
7882
return false, 0
7983
end
8084

@@ -83,32 +87,32 @@ function ENT:LinkEnt(ent)
8387

8488
table.insert(self.Marks, ent)
8589

86-
ent:CallOnRemove("AdvEMarker.Unlink", function(ent)
87-
if self:IsValid() then
88-
self:UnlinkEnt(ent)
89-
end
90+
ent:CallOnRemove("AdvEMarker.Unlink" .. self:EntIndex(), function(ent)
91+
self:UnlinkEnt(ent)
9092
end)
9193

9294
self:UpdateOutputs()
9395

9496
return true
9597
end
9698

97-
function ENT:UnlinkEnt( ent )
98-
local bool, index = self:CheckEnt( ent )
99-
if (bool) then
100-
table.remove( self.Marks, index )
99+
function ENT:UnlinkEnt(ent)
100+
local bool, index = self:CheckEnt(ent)
101+
102+
if bool then
103+
table.remove(self.Marks, index)
104+
ent:RemoveCallOnRemove("AdvEMarker.Unlink" .. self:EntIndex())
101105
self:UpdateOutputs()
102106
end
107+
103108
return bool
104109
end
105110

106111
function ENT:ClearEntities()
107-
for i=1,#self.Marks do
108-
if self.Marks[i]:IsValid() then
109-
self.Marks[i]:RemoveCallOnRemove( "AdvEMarker.Unlink" )
110-
end
112+
for index, ent in ipairs(self.Marks) do
113+
ent:RemoveCallOnRemove("AdvEMarker.Unlink" .. self:EntIndex())
111114
end
115+
112116
self.Marks = {}
113117
self:UpdateOutputs()
114118
end
@@ -117,18 +121,17 @@ function ENT:OnRemove()
117121
self:ClearEntities()
118122
end
119123

120-
duplicator.RegisterEntityClass( "gmod_wire_adv_emarker", WireLib.MakeWireEnt, "Data" )
121-
122124
function ENT:BuildDupeInfo()
123-
local info = BaseClass.BuildDupeInfo(self) or {}
125+
local info = BaseClass.BuildDupeInfo(self)
126+
127+
if #self.Marks > 0 then
128+
local tab = {}
124129

125-
if next(self.Marks) then
126-
local tbl = {}
127-
for index, e in pairs( self.Marks ) do
128-
tbl[index] = e:EntIndex()
130+
for index, ent in ipairs(self.Marks) do
131+
tab[index] = ent:EntIndex()
129132
end
130133

131-
info.marks = tbl
134+
info.marks = tab
132135
end
133136

134137
return info
@@ -137,16 +140,21 @@ end
137140
function ENT:ApplyDupeInfo(ply, ent, info, GetEntByID)
138141
BaseClass.ApplyDupeInfo(self, ply, ent, info, GetEntByID)
139142

140-
if (info.marks) then
141-
self.Marks = self.Marks or {}
142-
143-
for index, entid in pairs(info.marks) do
143+
if info.marks then
144+
for index, entid in ipairs(info.marks) do
144145
local ent = GetEntByID(entid)
145-
self.Marks[index] = ent
146-
ent:CallOnRemove("AdvEMarker.Unlink", function(ent)
147-
if IsValid(self) then self:UnlinkEnt(ent) end
148-
end)
146+
147+
if ent:IsValid() then
148+
table.insert(self.Marks, ent)
149+
150+
ent:CallOnRemove("AdvEMarker.Unlink" .. self:EntIndex(), function(ent)
151+
self:UnlinkEnt(ent)
152+
end)
153+
end
149154
end
155+
150156
self:UpdateOutputs()
151157
end
152158
end
159+
160+
duplicator.RegisterEntityClass("gmod_wire_adv_emarker", WireLib.MakeWireEnt, "Data")

lua/entities/gmod_wire_emarker.lua

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,81 @@
11
AddCSLuaFile()
2-
DEFINE_BASECLASS( "base_wire_entity" )
3-
ENT.PrintName = "Wire Entity Marker"
2+
3+
DEFINE_BASECLASS("base_wire_entity")
4+
ENT.PrintName = "Wire Entity Marker"
45
ENT.WireDebugName = "EMarker"
56

6-
if CLIENT then return end -- No more client
7+
if CLIENT then return end
78

89
function ENT:Initialize()
9-
self:PhysicsInit( SOLID_VPHYSICS )
10-
self:SetMoveType( MOVETYPE_VPHYSICS )
11-
self:SetSolid( SOLID_VPHYSICS )
10+
self:PhysicsInit(SOLID_VPHYSICS)
1211

13-
self.Outputs = WireLib.CreateSpecialOutputs(self, { "Entity" }, { "ENTITY" })
14-
self:SetOverlayText( "No Mark selected" )
12+
WireLib.CreateOutputs(self, { "Entity [ENTITY]" })
13+
self:SetOverlayText("No linked mark")
1514
end
1615

17-
function ENT:LinkEMarker(mark)
18-
if mark then self.mark = mark end
19-
if not IsValid(self.mark) then self:SetOverlayText( "No Mark selected" ) return end
20-
self.mark:CallOnRemove("EMarker.UnLink", function(ent)
21-
if IsValid(self) and self.mark == ent then self:UnLinkEMarker() end
22-
end)
23-
Wire_TriggerOutput(self, "Entity", self.mark)
24-
self:SetOverlayText( "Linked - " .. self.mark:GetModel() )
16+
function ENT:LinkEnt(ent)
17+
if ent ~= self.Mark then
18+
if self.Mark then
19+
self.Mark:RemoveCallOnRemove("EMarker.UnLink" .. self:EntIndex())
20+
self.Mark = nil
21+
end
22+
23+
ent:CallOnRemove("EMarker.UnLink" .. self:EntIndex(), function(ent)
24+
self:UnlinkEnt()
25+
end)
26+
27+
WireLib.SendMarks(self, { ent })
28+
WireLib.TriggerOutput(self, "Entity", ent)
29+
self:SetOverlayText("Linked to: " .. ent:GetClass())
30+
self.Mark = ent
31+
32+
return true
33+
end
34+
35+
return false
2536
end
2637

27-
function ENT:UnLinkEMarker()
28-
self.mark = NULL
29-
Wire_TriggerOutput(self, "Entity", NULL)
30-
self:SetOverlayText( "No Mark selected" )
38+
function ENT:UnlinkEnt()
39+
if self.Mark then
40+
WireLib.SendMarks(self, {})
41+
WireLib.TriggerOutput(self, "Entity", NULL)
42+
self:SetOverlayText("No linked mark")
43+
self.Mark:RemoveCallOnRemove("EMarker.UnLink" .. self:EntIndex())
44+
self.Mark = nil
45+
46+
return true
47+
end
48+
49+
return false
3150
end
3251

33-
duplicator.RegisterEntityClass( "gmod_wire_emarker", WireLib.MakeWireEnt, "Data" )
52+
function ENT:OnRemove()
53+
if self.Mark then
54+
self.Mark:RemoveCallOnRemove("EMarker.UnLink" .. self:EntIndex())
55+
self.Mark = nil
56+
end
57+
end
3458

3559
function ENT:BuildDupeInfo()
36-
local info = BaseClass.BuildDupeInfo(self) or {}
37-
if ( self.mark ) and ( self.mark:IsValid() ) then
38-
info.mark = self.mark:EntIndex()
60+
local info = BaseClass.BuildDupeInfo(self)
61+
62+
if self.Mark then
63+
info.mark = self.Mark:EntIndex()
3964
end
65+
4066
return info
4167
end
4268

4369
function ENT:ApplyDupeInfo(ply, ent, info, GetEntByID)
4470
BaseClass.ApplyDupeInfo(self, ply, ent, info, GetEntByID)
4571

46-
self:LinkEMarker(GetEntByID(info.mark))
72+
if info.mark then
73+
local ent = GetEntByID(info.mark)
74+
75+
if ent:IsValid() then
76+
self:LinkEnt(ent)
77+
end
78+
end
4779
end
80+
81+
duplicator.RegisterEntityClass("gmod_wire_emarker", WireLib.MakeWireEnt, "Data")

0 commit comments

Comments
 (0)