Hi guys I have been trying to remove elements from the custom hud with a timer. I have tried a few methods and non seem to work no cause any errors.
I have a start screen which looks awesome when the player initaly spawns. But it is overlapped by my hud. I want to remove the hud on a timer and make it reappear when the timer is up. Here is what I have tried. This is all in the function GM:PlayerInitialSpawn( ply ) btw.
The bottom code is calling upon the materials name, which I used when setting the graphic.
[code]
timer.Simple(8,function ()
for k, v in pairs({ "hud.png", "health.png", "timer.png",}) do
if names == v then return false end
end
return true
end)
[/code]
[code]
timer.Simple(8,function ()
for k, v in pairs({ "hudsplat", "hpsplat", "timersplat",}) do
if names == v then return false end
end
return true
end)
[/code]
Any help is greatly appreciated.
Thanks.
-Duby
Show us your GM:HUDPaint function, that's where all drawing functions are called from, you'll be able to control them from there.
I see ok. :) Don't judge if its not the most optimized code you have ever seen.
[code]
function GM:HUDPaint() -- this creates the hud for the player
if ( self.RoundEnded ) then
self:DrawEndHUD()
return
end
local yea = math.max(0, GetGlobalInt("Round.End") - CurTime())
timeleft = math.Round(yea, 2)
if ( timeleft == 0 ) then
timeleft = "Round Ended"
end
draw.SimpleText( timeleft,"MyFont", ScrW() / 2 - -700, ScrH() - 1000, Color(220, 78, 78, 255),TEXT_ALIGN_CENTER)
if ( GetGlobalInt("Round_ToMapChange") == 0 ) then
draw.SimpleText( "Last Round", "MyFont4", ScrW() / 2 - 750, ScrH() - 1000, Color(220, 78, 78, 255),TEXT_ALIGN_CENTER)
else
draw.SimpleText( GetGlobalInt("Round_ToMapChange") + 1 .. " Rounds left", "MyFont4", ScrW() / 2 - 750, ScrH() - 1000, Color(220, 78, 78, 255),TEXT_ALIGN_CENTER)
end
local ply = LocalPlayer()
if ( not ply or not IsValid(ply) ) then
return
end
local Ammo = 0
local wep = ply:GetActiveWeapon()
if ( wep and IsValid(wep) and wep.GetClass and wep.Clip1 ) then
Ammo = ply:GetActiveWeapon():Clip1()
local class = wep:GetClass()
if ( class and class != "" ) then
for k,v in ipairs(UnlimitedAmmoGuns) do
if ( class == v ) then
Ammo = "Unlimited"
end
end
end
end
local HP = ply:Health()
surface.SetTextColor( 220, 78, 78, 255)
surface.SetTextPos((ScrW()/1.8)-(ScrW()/2.2), (ScrH()/1.8)+(ScrH()/3))
surface.SetFont("MyFont")
surface.DrawText( HP )
surface.SetTextPos((ScrW()/2)+(ScrW()/3), (ScrH()/1.85)+(ScrH()/3))
surface.DrawText(Ammo)
if ( hudsplat:IsError() or hpsplat:IsError() or timersplat:IsError() ) and not NotifiedBrokenDL then
NotifiedBrokenDL = true
MsgN("A Material hasn't downloaded properly, try to reconnect or notify an admin !")
end
if ( !hudsplat:IsError() ) then
surface.SetMaterial(hudsplat)
surface.SetDrawColor(225, 225, 225, 225 )
surface.DrawTexturedRect(0, ScrH()-1080, 1920, 1100 )
end
if ( !hpsplat:IsError() ) then
surface.SetMaterial(hpsplat)
surface.SetDrawColor(225, 225, 225 )
surface.DrawTexturedRect(100, ScrH()-110, 65, 65 )
end
if ( !timersplat:IsError() ) then
surface.SetMaterial(timersplat)
surface.SetDrawColor(225, 225, 225, 225 )
surface.DrawTexturedRect(1790, ScrH()-1030, 65,65 )
end
--Cross hair
draw.RoundedBox( 4,ScrW() / 2 - 3.5,ScrH() / 2 - 3.5,7,7, Color ( 0,0,0,100 ))
draw.RoundedBox( 4,ScrW() / 2 - 3.5,ScrH() / 2 - 3.5,5,5, Color ( 255,150,0,100 ))
end
[/code]
Here's how to hide gmod / hl2 hud elements and the importance of tweaking for performance gains:
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/benchmarking_tips/benchmarking_hud_stuff.lua[/url]
As for hiding yours, just put an if ( timer_is_active ) then return; end at the top of your code to hide your hud until you want it to display.
Sorry I don't quite understand so will the method I am using work with a tweak or ?
Also what is this timer_is_active at the top of the hudpaint gonna do? Can you explain how I would do this? I am a little confused.
So at some point you want to trigger some timer, right? And for the duration of that timer you want the hud to be !visible, right?
Here, using a console command to set a global variable to the current time. This lets us easily get the change in time, so it's a start time and when we subtract this value from CurTime( ) we get a positive number; time in seconds since that time. That makes it easy to define an expiration in seconds.
[lua]concommand.Add( "dev_hidehud", function( _p, _cmd, _args )
TIMER_IS_ACTIVE = CurTime( );
TIMER_EXPIRY = 5; // Hide for 5 seconds
end );
hook.Add( "HUDPaint", "MyHud", function( )
if ( TIMER_IS_ACTIVE && CurTime( ) - TIMER_IS_ACTIVE < TIMER_EXPIRY ) then return; end
TIMER_IS_ACTIVE = nil;
TIMER_EXPIRY = nil;
// HUD STUFF
end );[/lua]
Alternatively, you could set one var as TIMER_EXPIRY = CurTime( ) + 5; then just check for
[lua]if ( TIMER_EXPIRY && CurTime( ) < TIMER_EXPIRY ) then return; end
TIMER_EXPIRY = nil;[/lua]
We set them to nil, because of the first check which checks to see if it exists.
Right I see how this will work but how will this tie in with getting it for when the player spawns. Will I need a .net for this to work sending from the initalspawn function?
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/game_event_listeners.lua[/url]
If you're running it locally, a game-event listener player_spawn is shared so you can do everything client-side.
[lua]//
// This is a basic template for creating game_event listeners/hooks with the purpose of including all
// arguments / table variables for each game-event to make it easily known which values can be accessed
// and when: player_spawn. player_spawn is triggered when the player initially spawns, or respawns.
//
// SHARED-REALM -- Both the CLIENT and SERVER can receive this hook.
//
gameevent.Listen("player_spawn")
hook.Add( "player_spawn", "player_spawn:Example", function( _data )
local _id = _data.userid; // Same as Player:UniqueID( );
// Called when the player spawns initially or respawns.
end );[/lua]
This gameevent.listen should it be at the start of the cl_init? Or should it be placed within the paint function?
Exactly as it's listed above. By itself. You can put it above the HUDPaint, but not inside.
I have fixed my issue via external help. I used a .net feature with my hud and on some other places on the file. I forgot to include a [code]return false [/code]
Ah, I'd recommend switching it out later on. A simple net message won't really bother, but when there are ways to avoid networking, take advantage of them.
I've found that sending large files through net: 2mb or so, the 2mb needs to transfer FIRST before any other net message will be transferred. They're basically sent in order so if it ends up constraining too much ( which, again to reiterate, one net message on respawn really shouldn't affect this ) then there's an additional spot to optimize.
Ah I see, thanks I will try this technique out in my project and get into the hang of working with it appropriately!
Sorry, you need to Log In to post a reply to this thread.