Hud wont paint

Well, Ive been working on my gamemode, and I went to start on the HUD, but it wouldn’t paint, and I dont know why, no errors or anything.

Code!
[lua]function GM:HUDPaint()

draw.RoundedBox( 5, 5, 5, LocalPlayer():Health(), 20, Color(255, 25, 0, 255))

self.BaseClass:HUDPaint()

end[/lua]

Couldnt you just hook it?

[lua]hook.Add( “HUDPaint”, “hud”, function()
draw.RoundedBox( 5, 5, 5, LocalPlayer():Health(), 20, Color(255, 25, 0, 255))
end)[/lua]

Also make sure the file that is running this function is included both the init.lua and cl_init.lua

[LUA]
function ExampleHUD()

draw.RoundedBox( 5, 5, 5, LocalPlayer():Health(), 20, Color(255, 25, 0, 255))

end
hook.Add( “HUDPaint”, “ExampleHUD”, ExampleHUD )
[/LUA]

[Editline]What[/Editline]
What are you making a HUD for?

It’s for a gamemode, you use GM functions.

Your not required to run it inside the GM:HUDPaint function, you can just have it present such as:

[lua]
function ExampleHUD()

draw.RoundedBox( 5, 5, 5, LocalPlayer():Health(), 20, Color(255, 25, 0, 255))

end
hook.Add( “HUDPaint”, “ExampleHUD”, ExampleHUD )

function GM:HUDPaint()

end
[/lua]

[editline]when[/editline]
If you are even required to have it present.

If you are sure its required, I’m sure you could just add the function to the Paint.

[lua]
function ExampleHUD()

draw.RoundedBox( 5, 5, 5, LocalPlayer():Health(), 20, Color(255, 25, 0, 255))

end

function GM:HUDPaint()

ExampleHUD()

end
[/lua]

Look at your code and then look at this:
[lua]
function GM:HUDPaint()
draw.RoundedBox(5, 5, 5, LocalPlayer():Health(), 20, Color(255, 25, 0, 255))
end
[/lua]

Half the lines, cleaner, and works.

[editline]1st February 2012[/editline]

That’s completely unnecessary, though. You’re making your code more complex than it needs to be.

Hooking could be more useful than overriding, though. Example below.

[lua]
–Example #1.
function GM:HUDPaint()
if( drawHealthBar ) then
<draw>
end
end

–Example #2.
function DrawHealthBar( enable )
if( enable ) then
hook.Add( “HUDPaint”, “DrawHealthBar”, function()
<draw>
)
else
hook.Remove( “HUDPaint”, “DrawHealthBar” )
end
end
[/lua]

Doing it like I did will pan out better, if his HUD evolves and becomes more complex.
Doing it like you did is obviously going to better if its going to be a simple HUD.

Or you do this. Which you mentioned. Because the other method is messy and isn’t necessary for a gamemode.

[lua]
local enableFlag = false

function GM:HUDPaint()
if enableFlag then
<draw>
end
end
[/lua]

[editline]1st February 2012[/editline]

Lolwut. My example to you wasn’t about what you should do for a complex HUD, you already showed that by including functions in the functions. The point is that for drawing a simple box you shouldn’t ever need to do that. It makes your code convoluted for no reason. You don’t need a separate function to draw a single box. Obviously, for more complex elements you may need to use separate functions included in the original. Even then, that doesn’t justify using messy hooks when you could just use the functions already provided to you (GM:HUDPaint()).

There are multiple ways to do this. You can use the GM or GAMEMODE tables, or use the hook.Add function. If you look at the code in the hook.Call function ( which is the one used for GM:… functions too! ), it all boils down to the same thing. Don’t go saying they are wrong , when you are both right.

IMO, your method will get a lot messier and more unoptimized in the end. Here’s what I mean:

[lua]
local draw = { healthbar = true, ammobar = true, … }
function GM:HUDPaint()
if( draw[healthbar] ) then
<draw>
end

if( draw[ammobar] ) then
    &lt;draw&gt;
end

...

end
[/lua]

Hooking will look better and be more optimized in the long run.

I have every thing included correctly, the HUD just won’t paint, Im going to try hooking it

Don’t forget that all of your client-side stuff needs to be in garrysmod/gamemodes/<your gamemode name>/gamemode/cl_init.lua.

Yeah I know ive been scripting for like 12 months…
I got it btw, didnt return true on the should draw function on accident

You’re very good at reading. I never said they were “wrong”, it just looks unnecessarily messy to use hooks when you are provided clean, explicit GM functions to work from.

My reasons against using hooks in a gamemode when you don’t need to:
Readability: A complex HUD’s code, regardless of what is being said, will look much more convoluted using a hook structure over a simple function. “hook.Add(“HUDPaint”, “abc”, function() end)” is 26 more characters typed than “GM:HUDPaint() end”. As you said, they’re the exact same, so why not use the one that is more readable? Also, if I were to post a code snippet from my gamemode using hooks, no one would know without me telling them that it was a gamemode. This all boils down into explicitness. Using the GM or GAMEMODE (in some cases) provided functions for a gamemode makes the code more explicit in its meaning. I can use hooks anywhere, GM functions are purely for gamemodes. When I post a snippet of code from a gamemode, you will immediately and without any real thought know that it is a gamemode. If I gave you hooks, you would have to waste time reasoning it out or the OP would have to post an unnecessary sentence when all they had to do was use more explicit coding practises.

I really don’t even have to argue this much, because the creator of Garry’s Mod and therefore GLua obviously created the GM abstractions for a purpose.

Why are you bent on imposing your will? You should consider the fact that different coders have different coding styles. Using the abbrevation GM as prefix for your functions simply overrides that given function.

Also, if the wiki is correct, then overriding your functions will actually hinder developement.

No, hooking in this case isn’t optimised at all. Look better? Law of brevity, friend. If I can say that your house is burning down in five words instead of a paragraph, I’m going to say that your house is burning down, not hook.Add(“NotifyNeighbor”, “virtuous”, function print(“Your house is burning down.”) end).

If you think the code you just posted looks dirty, then stop using parenthesis when they aren’t necessary and adding extra space to those parenthesis when not needed. That isn’t how Lua is supposed to look:

https://github.com/tukz/Tukui/blob/master/Tukui/core/functions.lua

Without your unnecessary notation:
[lua]
local todraw = { healthbar = true, ammobar = true, expbar = false }

function GM:HUDPaint()
if todraw.healthbar then
<draw>
end

if todraw.ammobar then
    &lt;draw&gt;
end

if todraw.expbar then
    &lt;draw&gt;
end

end
[/lua]

If you think that’s dirtier than this:

[lua]
local todraw = { healthbar = true, ammobar = true, expbar = false }

hook.Add(“HUDPaint”, “drawmyhud”, function()
if todraw.healthbar then
<draw>
end

if todraw.ammobar then
    &lt;draw&gt;
end

if todraw.expbar then
    &lt;draw&gt;
end

end)
[/lua]

You’re proper insane.

[editline]1st February 2012[/editline]

I’m not imposing my will, you are arguing against common sense. Ask anyone who’s even touched a proper programming text or taken a CompSci or related course. If there’s an abstraction for a function that is more explicit and doesn’t hinder development, you use it. Your team-mates will thank you, the guy managing your code 10 years from now will thank you, and whoever you need or that person needs to help debug your code will thank you.

As for overriding, I guess you don’t know what self.BaseClass:<FUNCTION NAME>() does.

[lua]
– Original function of HUDPaint maintained, just appended to.
function GM:HUDPaint()
self.BaseClass():HUDPaint()
<draw>
end
[/lua]

You’re completely missing my point. Compare these two codes.

[lua]
function DrawHealthBar( enable )
if( enable ) then
hook.Add( “HUDPaint”, “DrawHealthBar”, function()
<draw>
)
else
hook.Remove( “HUDPaint”, “DrawHealthBar” )
end
end

function DrawAmmoBar( enable )
if( enable ) then
hook.Add( “HUDPaint”, “DrawAmmoBar”, function()
<draw>
)
else
hook.Remove( “HUDPaint”, “DrawAmmoBar” )
end
end

function DrawXPBar( enable )
if( enable ) then
hook.Add( “HUDPaint”, “DrawXPBar”, function()
<draw>
)
else
hook.Remove( “HUDPaint”, “DrawXPBar” )
end
end

–Now, use this anywhere.
–Instead of all those unneccessary boolean checks which occur when overriding the GM:HUDPaint,
–you simply hook/unhook your drawing codes to the HUDPaint function.
DrawHealthBar( true )
DrawAmmoBar( true )
DrawXPBar( false )
[/lua]
[lua]
hook.Add(“HUDPaint”, “drawmyhud”, function()
if todraw.healthbar then
<draw>
end

if todraw.ammobar then
    &lt;draw&gt;
end
 
if todraw.expbar then
    &lt;draw&gt;
end

end)
[/lua]

I code in the style I want and in which I’m most proficient in.

Who stated that law? I’d rather sacrifice readability for optimizations and simpler usage.