While making a HUD it started crashing shortly after loading the HUD and I have no clue on what in the code is make it crash
[CODE]function myhud()
local client = LocalPlayer()
if not client:Alive() then return end
if (client:GetActiveWeapon() == NULL or client:GetActiveWeapon() == "Camera") then return end
local HP = client:Health() --Hp Client has
local AR = client:Armor() --Armor Client has
-- HUD Base
surface.SetDrawColor( 0, 0, 0, 120)
surface.DrawRect( 90, ScrH() - 90, 250, 60) --Hud Back
-- HUD Font
surface.CreateFont( "FontULine", {
font = "Arial",
size = 20,
weight = 500,
blursize = 0,
scanlines = 0,
antialias = true,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = false,
additive = false,
outline = true,
} )
surface.CreateFont( "Font1", {
font = "Arial",
size = 20,
weight = 500,
blursize = 0,
scanlines = 0,
antialias = true,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = false,
additive = false,
outline = false,
} )
--Red HP- HUD Precents
surface.SetFont( "FontULine" )
surface.SetTextColor( 255, 0, 0, 255)
surface.SetTextPos( 100, 1116 )
surface.DrawText( HP )
--Blue Armor-
surface.SetTextColor( 0, 0, 255, 255)
surface.SetTextPos( 100, 1146 )
surface.DrawText( AR )
-- HUD HP Slider
surface.SetDrawColor( 128, 128, 128, 200)
surface.DrawRect( 135, ScrH() - 85, 200, 20) -- HP Slide BackGround
surface.SetDrawColor( 77, 77, 77, 200)
surface.DrawRect( 135, ScrH() - 85, 200, 20) -- HP Slide BackGround Shade
surface.SetDrawColor( 255, 0, 0, 255)
surface.DrawRect( 135, ScrH() - 85, math.Clamp( HP, 0, 100)*2, 20) -- HP Slide
surface.SetDrawColor( 255, 117, 117, 255)
surface.DrawRect( 135, ScrH() - 85, math.Clamp( HP, 0, 100)*2, 8) -- HP Slide shade
surface.SetDrawColor( 128, 128, 128, 255)
surface.DrawOutlinedRect( 135, ScrH() - 85, 200, 20, Color( 255, 0, 0, 255 ) ) -- HP Outline
-- HUD AR Slider
surface.SetDrawColor( 128, 128, 128, 255)
surface.DrawRect( 135, ScrH() - 55, 200, 20) --AR Slide BackGround
surface.SetDrawColor( 0, 0, 255, 255)
surface.DrawRect( 135, ScrH() - 55, math.Clamp( AR, 0, 100)*2, 20) --AR Slide
surface.SetDrawColor( 117, 117, 255, 255)
surface.DrawRect( 135, ScrH() - 55, math.Clamp( AR, 0, 100)*2, 8) -- AR Slide Shade
surface.SetDrawColor( 128, 128, 128, 255)
surface.DrawOutlinedRect( 135, ScrH() - 55, 200, 20) --AR Outline
-- HUD Alerts!
surface.SetTextColor( 255, 0, 0, 255)
surface.SetTextPos( 200, 1116 )
surface.SetFont( "Font1" )
if HP <= 20 then surface.DrawText( "Heath Low!" ) end
surface.SetTextColor( 0, 0, 255, 255)
surface.SetTextPos( 200, 1146 )
if AR >= 21 then surface.SetTextColor( 0, 0, 0, 0) end
if AR == 0 then surface.DrawText( "No Armor" ) else surface.DrawText( "Armor Low" ) end
end
hook.Add("HUDPaint", "myhud", myhud)
[/CODE]
You are creating a font each frame, move these fonts outside of the function
How would I do that
[QUOTE=Rtyan;45839335]How would I do that[/QUOTE]
Instead of
[CODE]
function myhud()
// ...
surface.CreateFont( "FontULine", {
font = "Arial",
size = 20,
weight = 500,
blursize = 0,
scanlines = 0,
antialias = true,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = false,
additive = false,
outline = true,
// ...
end
[/CODE]
Do this
[CODE]
surface.CreateFont( "FontULine", {
font = "Arial",
size = 20,
weight = 500,
blursize = 0,
scanlines = 0,
antialias = true,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = false,
additive = false,
outline = true,
function myhud()
// ...
end
[/CODE]
[QUOTE=gonzalolog;45839071]You are creating a font each frame, move these fonts outside of the function[/QUOTE]
How would I do that and make the function be able to use in the original function
[editline]29th August 2014[/editline]
[QUOTE=escenraL;45839385]Instead of
[CODE]
function myhud()
// ...
surface.CreateFont( "FontULine", {
font = "Arial",
size = 20,
weight = 500,
blursize = 0,
scanlines = 0,
antialias = true,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = false,
additive = false,
outline = true,
// ...
end
[/CODE]
Do this
[CODE]
surface.CreateFont( "FontULine", {
font = "Arial",
size = 20,
weight = 500,
blursize = 0,
scanlines = 0,
antialias = true,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = false,
additive = false,
outline = true,
function myhud()
// ...
end
[/CODE][/QUOTE]
That does not work the function does not get the font for surface.DrawText
Just try it. The problem is that each frame, you are creating a new font. Just create it the once, and use it after that as many times as you want.
[QUOTE=dingusnin;45839471]Just try it. The problem is that each frame, you are creating a new font. Just create it the once, and use it after that as many times as you want.[/QUOTE]
got a error
[ERROR] lua/hud.lua:19: '(' expected near 'myhud'
1. unknown - lua/hud.lua:0
We don't know your code, what you did, so i'll give you a vague response
You commited a syntax error...
[QUOTE=gonzalolog;45839571]We don't know your code, what you did, so i'll give you a vague response
You commited a syntax error...[/QUOTE]
[CODE] surface.CreateFont( "FontULine", {
font = "Arial",
size = 20,
weight = 500,
blursize = 0,
scanlines = 0,
antialias = true,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = false,
additive = false,
outline = true,
function myhud()
local client = LocalPlayer()
if not client:Alive() then return end
if (client:GetActiveWeapon() == NULL or client:GetActiveWeapon() == "Camera") then return end
local HP = client:Health() --Hp Client has
local AR = client:Armor() --Armor Client has
-- HUD Base
surface.SetDrawColor( 0, 0, 0, 120)
surface.DrawRect( 90, ScrH() - 90, 250, 60) --Hud Back
--Red HP- HUD Precents
surface.SetFont( "FontULine" )
surface.SetTextColor( 255, 0, 0, 255)
surface.SetTextPos( 100, 1116 )
surface.DrawText( HP )
--Blue Armor-
surface.SetTextColor( 0, 0, 255, 255)
surface.SetTextPos( 100, 1146 )
surface.DrawText( AR )
-- HUD HP Slider
surface.SetDrawColor( 128, 128, 128, 255)
surface.DrawRect( 135, ScrH() - 85, 200, 20) -- HP Slide BackGround
surface.SetDrawColor( 255, 0, 0, 255)
surface.DrawRect( 135, ScrH() - 85, math.Clamp( HP, 0, 100)*2, 20) -- HP Slide
surface.SetDrawColor( 255, 117, 117, 255)
surface.DrawRect( 135, ScrH() - 85, math.Clamp( HP, 0, 100)*2, 8) -- HP Slide shade
surface.SetDrawColor( 128, 128, 128, 255)
surface.DrawOutlinedRect( 135, ScrH() - 85, 200, 20, Color( 255, 0, 0, 255 ) ) -- HP Outline
-- HUD AR Slider
surface.SetDrawColor( 128, 128, 128, 255)
surface.DrawRect( 135, ScrH() - 55, 200, 20) --AR Slide BackGround
surface.SetDrawColor( 0, 0, 255, 255)
surface.DrawRect( 135, ScrH() - 55, math.Clamp( AR, 0, 100)*2, 20) --AR Slide
surface.SetDrawColor( 117, 117, 255, 255)
surface.DrawRect( 135, ScrH() - 55, math.Clamp( AR, 0, 100)*2, 8) -- AR Slide Shade
surface.SetDrawColor( 128, 128, 128, 255)
surface.DrawOutlinedRect( 135, ScrH() - 55, 200, 20) --AR Outline
-- HUD Alerts!
surface.SetTextColor( 255, 0, 0, 255)
surface.SetTextPos( 200, 1116 )
surface.SetFont( "Font1" )
if HP <= 20 then surface.DrawText( "Heath Low!" ) end
surface.SetTextColor( 0, 0, 255, 255)
surface.SetTextPos( 200, 1146 )
if AR >= 21 then surface.SetTextColor( 0, 0, 0, 0) end
if AR == 0 then surface.DrawText( "No Armor" ) else surface.DrawText( "Armor Low" ) end
end
hook.Add("HUDPaint", "myhud", myhud)
local tohide = { -- HUD items to hide
["CHudHealth"] = true,
["CHudBattery"] = true,
["CHudAmmo"] = true,
["CHudSecondaryAmmo"] = true
}
local function HUDShouldDraw(name)
if (tohide[name]) then -- If the HUD name is a key in the table
return false; -- Return false.
end
end
hook.Add("HUDShouldDraw", "HUD hider", HUDShouldDraw)[/CODE]
Error
[ERROR] lua/crash_test.lua:2: unexpected symbol near '.'
1. unknown - lua/crash_test.lua:0
You haven't closed your CreateFont function.
You should learn the Lua syntaxis...
[lua]
surface.CreateFont( "FontULine", {
font = "Arial",
size = 20,
weight = 500,
blursize = 0,
scanlines = 0,
antialias = true,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = false,
additive = false,
outline = true,
[/lua]
You are missing there a [I]})[/I] to close [I]( "FontULine", {[/I]
(Please, don't ask how to fix it...Just read the comment and TRY to solve it)
[QUOTE=ms333;45839617]You haven't closed your CreateFont function.[/QUOTE]
[CODE] surface.CreateFont( "FontULine", {
font = "Arial",
size = 20,
weight = 500,
blursize = 0,
scanlines = 0,
antialias = true,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = false,
additive = false,
outline = true,
})
function myhud()
...
end[/CODE]
after closing got
[ERROR] lua/crash_test.lua:2: unexpected symbol near '.'
1. unknown - lua/crash_test.lua:0
Test it, if this fails, continue experimenting with it until you reach a valid solution :v:
[QUOTE=gonzalolog;45839652]Test it, if this fails, continue experimenting with it until you reach a valid solution :v:[/QUOTE]
[ERROR] lua/crash_test.lua:2: unexpected symbol near '.'
1. unknown - lua/crash_test.lua:0
[QUOTE=Rtyan;45839681][ERROR] lua/crash_test.lua:2: unexpected symbol near '.'
1. unknown - lua/crash_test.lua:0[/QUOTE]
I think that last line shouldn't have a comma.
Post the full code again.
You're also forgetting some mandatory checks.. [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/proper_hud_creation.lua.html[/url]
You're missing Font1 too. I changed my normal _p for your client, but I did define _w as the weapon instead of repeating GetActiveWeapon. You also tried comparing an entity to a string ( camera ), so I added in GetClass for that comparison; if you're referring to weapon_camera or gmod_camera then the name would need to be that or you'd need to use a string.find
[code]surface.CreateFont( "FontULine", {
font = "Arial",
size = 20,
weight = 500,
blursize = 0,
scanlines = 0,
antialias = true,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = false,
additive = false,
outline = true,
} );
surface.CreateFont( "Font1", {
font = "Arial",
size = 20,
weight = 500,
blursize = 0,
scanlines = 0,
antialias = true,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = false,
additive = false,
outline = true,
} );
//
// Create a basic HUD which uses the player to draw stuff.. BASIC!
//
hook.Add("HUDPaint", "LuaHUDOurBasicHud", function( )
// Init vars
local client = LocalPlayer( );
// Check if we should draw this hud
local _shoulddraw = hook.Call( "HUDShouldDraw", GAMEMODE, "LuaHUDOurBasicHud" );
// Mandatory Checks: If the player isn't valid, or we didn't want this to draw, then don't draw/return.
if ( !IsValid( client ) || !_shoulddraw || !client:Alive( ) ) then return; end
// Mandatory Weapon checks
local _w = client:GetActiveWeapon( );
if ( !IsValid( _w ) || _w:GetClass( ) == "camera" ) then return; end
// Do HUD stuff...
local HP = client:Health( ) --Hp Client has
local AR = client:Armor( ) --Armor Client has
-- HUD Base
surface.SetDrawColor( 0, 0, 0, 120)
surface.DrawRect( 90, ScrH() - 90, 250, 60) --Hud Back
--Red HP- HUD Precents
surface.SetFont( "FontULine" )
surface.SetTextColor( 255, 0, 0, 255)
surface.SetTextPos( 100, 1116 )
surface.DrawText( HP )
--Blue Armor-
surface.SetTextColor( 0, 0, 255, 255)
surface.SetTextPos( 100, 1146 )
surface.DrawText( AR )
-- HUD HP Slider
surface.SetDrawColor( 128, 128, 128, 255)
surface.DrawRect( 135, ScrH() - 85, 200, 20) -- HP Slide BackGround
surface.SetDrawColor( 255, 0, 0, 255)
surface.DrawRect( 135, ScrH() - 85, math.Clamp( HP, 0, 100)*2, 20) -- HP Slide
surface.SetDrawColor( 255, 117, 117, 255)
surface.DrawRect( 135, ScrH() - 85, math.Clamp( HP, 0, 100)*2, 8) -- HP Slide shade
surface.SetDrawColor( 128, 128, 128, 255)
surface.DrawOutlinedRect( 135, ScrH() - 85, 200, 20, Color( 255, 0, 0, 255 ) ) -- HP Outline
-- HUD AR Slider
surface.SetDrawColor( 128, 128, 128, 255)
surface.DrawRect( 135, ScrH() - 55, 200, 20) --AR Slide BackGround
surface.SetDrawColor( 0, 0, 255, 255)
surface.DrawRect( 135, ScrH() - 55, math.Clamp( AR, 0, 100)*2, 20) --AR Slide
surface.SetDrawColor( 117, 117, 255, 255)
surface.DrawRect( 135, ScrH() - 55, math.Clamp( AR, 0, 100)*2, 8) -- AR Slide Shade
surface.SetDrawColor( 128, 128, 128, 255)
surface.DrawOutlinedRect( 135, ScrH() - 55, 200, 20) --AR Outline
-- HUD Alerts!
surface.SetTextColor( 255, 0, 0, 255)
surface.SetTextPos( 200, 1116 )
surface.SetFont( "Font1" )
if HP <= 20 then surface.DrawText( "Heath Low!" ) end
surface.SetTextColor( 0, 0, 255, 255)
surface.SetTextPos( 200, 1146 )
if AR >= 21 then surface.SetTextColor( 0, 0, 0, 0) end
if AR == 0 then surface.DrawText( "No Armor" ) else surface.DrawText( "Armor Low" ) end
end );[/code]
[QUOTE=Acecool;45839729]You're also forgetting some mandatory checks.. [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/proper_hud_creation.lua.html[/url]
You're missing Font1 too. I changed my normal _p for your client, but I did define _w as the weapon instead of repeating GetActiveWeapon. You also tried comparing an entity to a string ( camera ), so I added in GetClass for that comparison; if you're referring to weapon_camera or gmod_camera then the name would need to be that or you'd need to use a string.find
[code]surface.CreateFont( "FontULine", {
font = "Arial",
size = 20,
weight = 500,
blursize = 0,
scanlines = 0,
antialias = true,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = false,
additive = false,
outline = true,
} );
surface.CreateFont( "Font1", {
font = "Arial",
size = 20,
weight = 500,
blursize = 0,
scanlines = 0,
antialias = true,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = false,
additive = false,
outline = true,
} );
//
// Create a basic HUD which uses the player to draw stuff.. BASIC!
//
hook.Add("HUDPaint", "LuaHUDOurBasicHud", function( )
// Init vars
local client = LocalPlayer( );
// Check if we should draw this hud
local _shoulddraw = hook.Call( "HUDShouldDraw", GAMEMODE, "LuaHUDOurBasicHud" );
// Mandatory Checks: If the player isn't valid, or we didn't want this to draw, then don't draw/return.
if ( !IsValid( client ) || !_shoulddraw || !client:Alive( ) ) then return; end
// Mandatory Weapon checks
local _w = client:GetActiveWeapon( );
if ( !IsValid( _w ) || _w:GetClass( ) == "camera" ) then return; end
// Do HUD stuff...
local HP = client:Health( ) --Hp Client has
local AR = client:Armor( ) --Armor Client has
-- HUD Base
surface.SetDrawColor( 0, 0, 0, 120)
surface.DrawRect( 90, ScrH() - 90, 250, 60) --Hud Back
--Red HP- HUD Precents
surface.SetFont( "FontULine" )
surface.SetTextColor( 255, 0, 0, 255)
surface.SetTextPos( 100, 1116 )
surface.DrawText( HP )
--Blue Armor-
surface.SetTextColor( 0, 0, 255, 255)
surface.SetTextPos( 100, 1146 )
surface.DrawText( AR )
-- HUD HP Slider
surface.SetDrawColor( 128, 128, 128, 255)
surface.DrawRect( 135, ScrH() - 85, 200, 20) -- HP Slide BackGround
surface.SetDrawColor( 255, 0, 0, 255)
surface.DrawRect( 135, ScrH() - 85, math.Clamp( HP, 0, 100)*2, 20) -- HP Slide
surface.SetDrawColor( 255, 117, 117, 255)
surface.DrawRect( 135, ScrH() - 85, math.Clamp( HP, 0, 100)*2, 8) -- HP Slide shade
surface.SetDrawColor( 128, 128, 128, 255)
surface.DrawOutlinedRect( 135, ScrH() - 85, 200, 20, Color( 255, 0, 0, 255 ) ) -- HP Outline
-- HUD AR Slider
surface.SetDrawColor( 128, 128, 128, 255)
surface.DrawRect( 135, ScrH() - 55, 200, 20) --AR Slide BackGround
surface.SetDrawColor( 0, 0, 255, 255)
surface.DrawRect( 135, ScrH() - 55, math.Clamp( AR, 0, 100)*2, 20) --AR Slide
surface.SetDrawColor( 117, 117, 255, 255)
surface.DrawRect( 135, ScrH() - 55, math.Clamp( AR, 0, 100)*2, 8) -- AR Slide Shade
surface.SetDrawColor( 128, 128, 128, 255)
surface.DrawOutlinedRect( 135, ScrH() - 55, 200, 20) --AR Outline
-- HUD Alerts!
surface.SetTextColor( 255, 0, 0, 255)
surface.SetTextPos( 200, 1116 )
surface.SetFont( "Font1" )
if HP <= 20 then surface.DrawText( "Heath Low!" ) end
surface.SetTextColor( 0, 0, 255, 255)
surface.SetTextPos( 200, 1146 )
if AR >= 21 then surface.SetTextColor( 0, 0, 0, 0) end
if AR == 0 then surface.DrawText( "No Armor" ) else surface.DrawText( "Armor Low" ) end
end );[/code][/QUOTE]
There is no errors but the text does not draw
[lua]
local _shoulddraw = hook.Call( "HUDShouldDraw", GAMEMODE, "LuaHUDOurBasicHud" ) or true;
[/lua]
?
[QUOTE=gonzalolog;45839817][lua]
local _shoulddraw = hook.Call( "HUDShouldDraw", GAMEMODE, "LuaHUDOurBasicHud" ) or true;
[/lua]
?[/QUOTE]
didn't fix the draw problem dont know if it is not finding the font or just not drawing
That line calls the HUDShouldDraw hook which returns whether or not the hud should draw. It is there to essentially show how to hide it based on the hook we were given if it should be hidden under certain circumstances; although a simple return could do it too.
For the text not drawing, you may be drawing it off screen. Look into scaling the positions based on screensize. So, hard-code it for your resolution, then add a modifier for it: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/understanding_hardcoding_of_screensizes.lua.html[/url]
The text draws for me without any issues if I set the Y position to 200 instead of 11xx.
[QUOTE=Acecool;45840223]That line calls the HUDShouldDraw hook which returns whether or not the hud should draw. It is there to essentially show how to hide it based on the hook we were given if it should be hidden under certain circumstances; although a simple return could do it too.
For the text not drawing, you may be drawing it off screen. Look into scaling the positions based on screensize. So, hard-code it for your resolution, then add a modifier for it: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/understanding_hardcoding_of_screensizes.lua.html[/url]
The text draws for me without any issues if I set the Y position to 200 instead of 11xx.[/QUOTE]
thx im still learning and must of some how messed up res when I did somthing
Sorry, you need to Log In to post a reply to this thread.