Okay so im starting to learn how to code lua and I started coding a hud but its getting a error with the weapon clip part in the draw can someone please help me
heres my code
function HideHud(default) -- This removes default hud
for k, v in pairs({"CHudHealth","CHudBattery","CHudAmmo","CHudSecondaryAmmo", })do
if name == v then return false end
end
end
hook.add("HUDShouldDraw", "default", HideHud)
function GM:Initialize()
end
surface.CreateFont( "Myfont",
{
font = "DefaultBold", -- Font NAME
size = 24,
weight = 400,
antialias = true,
shadow = true
})
function GM:HUDPaint()
self.BaseClass:HUDPaint() -- ALWAYS PUT IN FUNCTION HUDPaint
local ply = LocalPlayer()
local wep = LocalPlayer():GetActiveWeapon():Clip1()
local HP = LocalPlayer():Health()
local Arm = LocalPlayer():Armor()
surface.SetTextColor( 18, 45, 199, 255)
surface.SetTextPos( 20, 735 )
surface.SetFont( "Myfont" )
surface.DrawText( "Health: "..HP) // Health
surface.SetTextColor( 18, 45, 199, 255)
surface.SetTextPos( 420, 735 )
surface.SetFont( "Myfont" )
surface.DrawText( "Magazine: "..wep) // Ammo
surface.SetTextColor( 18, 45, 199, 255)
surface.SetTextPos( 240, 735 )
surface.SetFont( "Myfont" )
surface.DrawText( "Armor: "..Arm) //Armor
end
[highlight](User was banned for this post ("Undescriptive thread title" - postal))[/highlight]
Welcome to Facepunch; use [code] tags around code. Also, read this for how to optimize your hud-hiding feature and why looping through a table is bad: https://dl.dropboxusercontent.com/u/26074909/tutoring/benchmarking_tips/benchmarking_hud_stuff.lua.html
A player doesn't always have a weapon out, use something like a ternary operation ( shorthand for if then else end )
[lua]// Will either return ammo in magazine, or 0. It looks to make sure LocalPlayer( ) exists which should be done at the top of hudpaint, then it checks to make sure GetActiveWeapon function exists, then it makes sure the weapon is valid, then it checks to make sure that weapon has Clip1 function as HL2 weapons won't; then if all of that is true it returns the Clip1( ) magazine ammo count, or 0
local ammo = ( IsValid( LocalPlayer( ) ) && LocalPlayer( ).GetActiveWeapon && IsValid( LocalPlayer( ):GetActiveWeapon( ) ) && LocalPlayer( ):GetActiveWeapon( ).Clip1 ) && LocalPlayer( ):GetActiveWeapon( ):Clip1( ) || 0;[/lua]
On top of that, you're defining ply = LocalPlayer( ) but you don't use ply for any subsequent calls...
[lua]hook.Add( "HUDPaint", "MyHud", function( )
local _p = LocalPlayer( );
if ( !IsValid( _p ) ) then return; end
// Get the weapon - it can be null
local _w = _p:GetActiveWeapon( );
// makes sure the weapon is valid and the weapon has Clip1 magazine ammo-count function; if so returns that or returns 0
local _ammo = ( IsValid( _w ) && _w.Clip1 ) && _w:Clip1( ) || 0;
end );[/lua]
Ok thanks for the quick reply is there anyway you can explain this to me in a little more noob friendly I sort of get that piece of code but I dont understand how if I was to make my own version how I would put that in along with everything else
so he can answer your question what is it that you do not understand about it? or need help on? if you are as nooby as you say i suggest looking here [url]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index4bb6.html[/url] or reading every thing here [url]http://wiki.garrysmod.com/page/Main_Page[/url]
A Ternary operation is short-hand for
[lua]local _var = nil;
if <boolean_statement> then
_var = x;
else
_var = y;
end[/lua]
and all of these are the same as above
[lua]local _var = <boolean_statement> && x || y;[/lua]
[lua]local _var = ( <boolean_statement> ) && x || y;[/lua]
[lua]local _var = ( <boolean_statement> ) and x or y;[/lua]
[lua]local _var = <boolean_statement> and x or y;[/lua]
You can add more boolean statements; combined it must return true or false.
so this is the same as the example I gave as a regular if with a default of 0:
[lua]local _ammo = 0;
if ( IsValid( _w ) && _w.Clip1 ) then
_ammo = _w:Clip1( );
end[/lua]
As Ternary, it would be referenced like this - the local _ammo needs to be pre-set because if I defined local variable inside of an if / else statement, the scope of that variable exists only inside that if/else statement, by defining it outside of the if, it can be changed from within ( as long as local is not re-added to the front, because then it would be a local within the scope of the if, and the outside var would never change ).
[lua]local _ammo = 0;
if ( IsValid( _w ) && _w.Clip1 ) then
_ammo = _w:Clip1( );
else
_var = 0;
end[/lua]
so, the example I gave:
[lua]// Add a hook into HUDPaint so this will be drawn to the player each frame
hook.Add( "HUDPaint", "MyHud", function( )
// Define the player as a variable; you defined it as ply, I'm defining it as _p
local _p = LocalPlayer( );
// Make sure the LocalPlayer( ) is valid; it's referenced here as _p - if it isn't valid, there's no point to continue drawing this custom hud to a player that doesn't exist
if ( !IsValid( _p ) ) then return; end
// Get the weapon - it can be NULL ( which means an empty/undefined entity ) or it can be nil ( which means it's not set at all - because _p is defined at this point, GetActiveWeapon will be defined because it's a META_PLAYER function )
local _w = _p:GetActiveWeapon( );
// IF we are drawing a hud strictly for the weapon the player has, uncomment this line:
-- if ( !IsValid( _w ) ) then return; end
// makes sure the weapon is valid and the weapon has Clip1 magazine ammo-count function; if so returns that or returns 0 -- IF the !IsValid( _w ) line is active, the "IsValid( _w ) &&" can be removed from this line
local _ammo = ( IsValid( _w ) && _w.Clip1 ) && _w:Clip1( ) || 0;
// Another way _ammo can be defined is:
local _ammo = 0;
// IF the !IsValid( _w ) line is active, the "IsValid( _w ) &&" can be removed from this line
-- if ( IsValid( _w ) && _w.Clip1 ) then
-- _ammo = _w:Clip1( );
-- end
//
// Continue with your drawing of the hud...
//
end );[/lua]
In the future, please give name the thread something more useful than "HELP"; describe the problem in a few short words...
Sorry, you need to Log In to post a reply to this thread.