• HUD error
    12 replies, posted
Im making a custom HUD for my gamemode and i used the complete code from a tutorial. [PHP]function hidehud(name) for k, v in pairs{"CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo"} do if name == v then return false end end end hook.Add("HUDShouldDraw", "hidehud", hidehud) local function HUDPaint( ) client = client or LocalPlayer( ); -- set a shortcut to the client if( !client:Alive( ) ) then return; end -- don't draw if the client is dead local _, th = good_hud:TextSize( "TEXT", vars.font ); -- get text size( height in this case ) local i = 2; -- shortcut to how many items( bars + text ) we have local width = vars.width * ScrW( ); -- calculate width local bar_width = width - ( vars.padding * i ); -- calculate bar width and element height local height = ( vars.padding * i ) + ( th * i ) + ( vars.text_spacing * i ) + ( vars.bar_height * i ) + vars.bar_spacing; local x = vars.margin; -- get x position of element local y = ScrH( ) - vars.margin - height; -- get y position of element local cx = x + vars.padding; -- get x and y of contents local cy = y + vars.padding; good_hud:PaintPanel( x, y, width, height, colors.background ); -- paint the background panel local by = th + vars.text_spacing; -- calc text position local text = string.format( "Health: %iHP", client:Health( ) ); -- get health text good_hud:PaintText( cx, cy, text, vars.font, colors.text ); -- paint health text and health bar good_hud:PaintBar( cx, cy + by, bar_width, vars.bar_height, colors.health_bar, client:Health( ) / 100 ); by = by + vars.bar_height + vars.bar_spacing; -- increment text position local text = string.format( "Suit: %iSP", client:Armor( ) ); -- get suit text good_hud:PaintText( cx, cy + by, text, vars.font, colors.text ); -- paint suit text and suit bar good_hud:PaintBar( cx, cy + by + th + vars.text_spacing, bar_width, vars.bar_height, colors.suit_bar, client:Armor( ) / 100 ); end hook.Add( "HUDPaint", "PaintOurHud", HUDPaint );[/PHP] NOTE: all of this code is from a tutorial i didnt made it i was just checking how it looked
What is the error?
oh sorry forgot that [PHP]Hook 'PaintOurHud' Failed: tdmrpg\gamemode\cl_hud.lua:12: attempt to index global 'good_hud' (a nil value)[/PHP]
You either haven't followed the tutorial properly, or you just copied and pasted expecting it to work. There are about five or six function calls to non existent functions in your code.
ok i will look more into it [editline]09:56PM[/editline] NOTE: i made this post so someone knows i posted a new entry ok ive looked at the tutorial and tried to fix it but i cant i get this error now: [PHP]Hook 'PaintOurHud' Failed: tdmrpg\gamemode\cl_hud.lua:71: attempt to call method 'TextSize' (a nil value)[/PHP] [PHP]good_hud = { }; local vars = { font = "1", padding = 10, margin = 35, text_spacing = 2, bar_spacing = 5, bar_height = 16, width = 0.25 }; local colors = { background = { border = Color( 190, 255, 128, 255 ), background = Color( 120, 240, 0, 75 ) }, text = { shadow = Color( 0, 0, 0, 200 ), text = Color( 255, 255, 255, 255 ) }, health_bar = { border = Color( 255, 0, 0, 255 ), background = Color( 255, 0, 0, 75 ), shade = Color( 255, 104, 104, 255 ), fill = Color( 232, 0, 0, 255 ) }, suit_bar = { border = Color( 0, 0, 255, 255 ), background = Color( 0, 0, 255, 75 ), shade = Color( 136, 136, 255, 255 ), fill = Color( 0, 0, 219, 255 ) } }; local function HUDPaint( ) client = client or LocalPlayer( ); -- set a shortcut to the client if( !client:Alive( ) ) then return; end -- don't draw if the client is dead local _, th = good_hud:TextSize( "TEXT", vars.font ); -- get text size( height in this case ) local i = 2; -- shortcut to how many items( bars + text ) we have local width = vars.width * ScrW( ); -- calculate width local bar_width = width - ( vars.padding * i ); -- calculate bar width and element height local height = ( vars.padding * i ) + ( th * i ) + ( vars.text_spacing * i ) + ( vars.bar_height * i ) + vars.bar_spacing; local x = vars.margin; -- get x position of element local y = ScrH( ) - vars.margin - height; -- get y position of element local cx = x + vars.padding; -- get x and y of contents local cy = y + vars.padding; good_hud:PaintPanel( x, y, width, height, colors.background ); -- paint the background panel local by = th + vars.text_spacing; -- calc text position local text = string.format( "Health: %iHP", client:Health( ) ); -- get health text good_hud:PaintText( cx, cy, text, vars.font, colors.text ); -- paint health text and health bar good_hud:PaintBar( cx, cy + by, bar_width, vars.bar_height, colors.health_bar, client:Health( ) / 100 ); by = by + vars.bar_height + vars.bar_spacing; -- increment text position local text = string.format( "Suit: %iSP", client:Armor( ) ); -- get suit text good_hud:PaintText( cx, cy + by, text, vars.font, colors.text ); -- paint suit text and suit bar good_hud:PaintBar( cx, cy + by + th + vars.text_spacing, bar_width, vars.bar_height, colors.suit_bar, client:Armor( ) / 100 ); end hook.Add( "HUDPaint", "PaintOurHud", HUDPaint );[/PHP]
You are trying to call functions on good_hud, for example: Line 90 good_hud:PaintText(....). You haven't defined any of these functions.
yes i know but it doesnt stand anything about that in the tutorial: [url]http://wiki.garrysmod.com/?title=Creating_a_HUD[/url] The HUD is looking really good so i really want this to work.
I can see that the tutorial does define all the functions you are missing, you just have to read through it properly.
You are just copy and pasting from the stuff at the bottom. Be patient and read it, We need a lazy rating!
ok sorry for that i red it to fast and yeah im a bit lazy EDIT: I fixed it and i rated myself dumb! EDIT on EDIT: it didnt work voting your own post...
I have now tried to fix a error with it but it wont work. When i die i sometimes get this error and the hud closes: [PHP]Hook 'PaintOurHud' Failed: tdmrpg\gamemode\cl_hud.lua:165: attempt to call method 'Clip1' (a nil value)[/PHP] and the code thats making this is: [PHP] local mag_left = 0 local mag_extra = 0 if( client:Alive( ) == true ) then mag_left = client:GetActiveWeapon():Clip1() or 0 // How much ammunition you have inside the current magazine mag_extra = client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()) or 0 end[/PHP]
You should check if the Active Weapon is a valid entity before you try calling any methods on it. [lua]local mag_left = 0 local mag_extra = 0 if client:Alive( ) then -- you don't need to check for equality with true. local weap = client:GetActiveWeapon(); if ValidEntity(weap) then mag_left = weap:Clip1() or 0; mag_extra = client:GetAmmoCount(weap:GetPrimaryAmmoType()) or 0; end end[/lua]
oh thanks will try
Sorry, you need to Log In to post a reply to this thread.