Skip to content

Commit 7ee8185

Browse files
Optimize overlay visibility logic (#3084)
* Optimize overlay visibility logic Also renames base_wire_entity's overlay visibility function to ENT:WireBeingLookedAtByLocalPlayer() * Don't rename any functions * Better last looked at check
1 parent 4232fdb commit 7ee8185

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

lua/entities/base_wire_entity.lua

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ ENT.IsWire = true
1111

1212
if CLIENT then
1313
local wire_drawoutline = CreateClientConVar("wire_drawoutline", 1, true, false)
14-
-- Funny reverse-detour because this function is sometimes nil and sometimes not, but is never nil when drawing for the first time.
15-
local function beingLookedAtByLocalPlayer(self)
16-
beingLookedAtByLocalPlayer = BaseClass.BeingLookedAtByLocalPlayer
17-
return beingLookedAtByLocalPlayer(self)
18-
end
1914

2015
function ENT:Initialize()
2116
self.NextRBUpdate = CurTime() + 0.25
@@ -232,7 +227,7 @@ if CLIENT then
232227

233228
-- Custom better version of this base_gmodentity function
234229
function ENT:BeingLookedAtByLocalPlayer()
235-
local trbool = beingLookedAtByLocalPlayer(self)
230+
local trbool = BaseClass.BeingLookedAtByLocalPlayer(self)
236231
local self_table = self:GetTable()
237232

238233
if self_table.PlayerWasLookingAtMe ~= trbool then
@@ -251,15 +246,37 @@ if CLIENT then
251246
return trbool
252247
end
253248

249+
local looked_at
250+
251+
-- Shared by all derivative entities to determine if the overlay should be visible
252+
hook.Add("Think", "wire_base_lookedatbylocalplayer", function()
253+
local ply = LocalPlayer()
254+
if not IsValid(ply) then
255+
looked_at = nil
256+
return
257+
end
258+
259+
local cur_ent = ply:GetEyeTrace().Entity
260+
261+
if cur_ent ~= looked_at and IsValid(looked_at) and looked_at.IsWire then
262+
looked_at:BeingLookedAtByLocalPlayer()
263+
end
264+
265+
if cur_ent.IsWire and cur_ent:BeingLookedAtByLocalPlayer() then
266+
looked_at = cur_ent
267+
else
268+
looked_at = nil
269+
end
270+
end)
271+
254272
function ENT:DoNormalDraw(nohalo, notip)
255-
local looked_at = self:BeingLookedAtByLocalPlayer()
256-
if not nohalo and wire_drawoutline:GetBool() and looked_at then
273+
if not nohalo and wire_drawoutline:GetBool() and looked_at == self then
257274
self:DrawEntityOutline()
258275
self:DrawModel()
259276
else
260277
self:DrawModel()
261278
end
262-
if not notip and looked_at then
279+
if not notip and looked_at == self then
263280
self:AddWorldTip()
264281
end
265282
end

0 commit comments

Comments
 (0)