• Showing information when hovering over a player?
    2 replies, posted
I am looking for a way to show text on the player when somebody looks at them (Like TTT does when it says their karma). Is there a hook for when a player looks at another player, or is it a property I can set?
I remember seeing this somewhere. You could try having a client side tracer and when it hits someone it draws their name on the screen. I will try and find it, hang on. --Edit Found it, this might not be quite what you are looking for but I am sure you can change it to your liking. [CODE]function SPM.HUDPaint() local tr = utilx.GetPlayerTrace(LocalPlayer(), LocalPlayer():GetCursorAimVector()) local trace = util.TraceLine(tr) if(!trace.Hit) then return end if(!trace.HitNonWorld) then return end local Entity = trace.Entity if(Entity:IsValid() and Entity:IsNPC() and Entity:GetNetworkedBool("IsPet") == true) then local Name = Entity:GetNetworkedString("Name") local Owner = Entity:GetNetworkedEntity("Player") local Age = Entity:GetNetworkedString("Age") local Gender = Entity:GetNetworkedString("Gender") local Pos = Entity:GetPos() - LocalPlayer():GetPos() local Length = Pos:Length() if(Length < 128) then Length = 128 end local Alpha = math.max(math.min(255 - ((255 / 1024) * Length), 255), 0) surface.SetFont("TargetID") local x, y = gui.MousePos() if(x == 0 and y == 0) then x = ScrW() / 2 y = ScrH() / 2 end draw.SimpleText("Name: "..Name, "ChatFont", x, y + 32, Color(255, 255, 255, Alpha), 1, 1) draw.SimpleText("Owner: "..Owner:Nick(), "ChatFont", x, y + 48, Color(255, 255, 255, Alpha), 1, 1) draw.SimpleText("Age: "..Age, "ChatFont", x, y + 64, Color(255, 255, 255, Alpha), 1, 1) draw.SimpleText("Gender: "..Gender, "ChatFont", x, y + 80, Color(255, 255, 255, Alpha), 1, 1) end end hook.Add("HUDPaint", "SPM.HUDPaint", SPM.HUDPaint)[/CODE]
I looked through the base Garry's Mod Lua code (which can be found [URL="https://github.com/garrynewman/garrysmod/"]here[/URL]), and found this to show a person's name when they are looked at. [code] --[[--------------------------------------------------------- Name: gamemode:HUDDrawTargetID( ) Desc: Draw the target id (the name of the player you're currently looking at) -----------------------------------------------------------]] function GM:HUDDrawTargetID() local tr = util.GetPlayerTrace( LocalPlayer() ) local trace = util.TraceLine( tr ) if (!trace.Hit) then return end if (!trace.HitNonWorld) then return end local text = "ERROR" local font = "TargetID" if (trace.Entity:IsPlayer()) then text = trace.Entity:Nick() else return --text = trace.Entity:GetClass() end surface.SetFont( font ) local w, h = surface.GetTextSize( text ) local MouseX, MouseY = gui.MousePos() if ( MouseX == 0 && MouseY == 0 ) then MouseX = ScrW() / 2 MouseY = ScrH() / 2 end local x = MouseX local y = MouseY x = x - w / 2 y = y + 30 -- The fonts internal drop shadow looks lousy with AA on draw.SimpleText( text, font, x+1, y+1, Color(0,0,0,120) ) draw.SimpleText( text, font, x+2, y+2, Color(0,0,0,50) ) draw.SimpleText( text, font, x, y, self:GetTeamColor( trace.Entity ) ) y = y + h + 5 local text = trace.Entity:Health() .. "%" local font = "TargetIDSmall" surface.SetFont( font ) local w, h = surface.GetTextSize( text ) local x = MouseX - w / 2 draw.SimpleText( text, font, x+1, y+1, Color(0,0,0,120) ) draw.SimpleText( text, font, x+2, y+2, Color(0,0,0,50) ) draw.SimpleText( text, font, x, y, self:GetTeamColor( trace.Entity ) ) end[/code] I assume you could just modify how it works (change the draw.SimpleText lines), and then add this hook to make it how you want :) [code]hook.add("HUDPaint", "NameOfCustomDrawTargetID", NameOfCustomDrawTargetID)[/code] I also found the actual [URL="https://github.com/garrynewman/garrysmod/blob/master/garrysmod/gamemodes/terrortown/gamemode/cl_targetid.lua"]TTT DrawTargetID[/URL] too, but there's so much code it's not worth pasting here.
Sorry, you need to Log In to post a reply to this thread.