11AddCSLuaFile ()
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 "
55ENT .WireDebugName = " Adv EMarker"
66
7- if CLIENT then return end -- No more client
7+ if CLIENT then return end
88
99function 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" )
2736end
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
5554end
5655
5756function 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 )
7273end
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
7983end
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
9597end
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
104109end
105110
106111function 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 ()
114118end
@@ -117,18 +121,17 @@ function ENT:OnRemove()
117121 self :ClearEntities ()
118122end
119123
120- duplicator .RegisterEntityClass ( " gmod_wire_adv_emarker" , WireLib .MakeWireEnt , " Data" )
121-
122124function 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
137140function 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
152158end
159+
160+ duplicator .RegisterEntityClass (" gmod_wire_adv_emarker" , WireLib .MakeWireEnt , " Data" )
0 commit comments