Making my first Hud.

[ERROR] gamemodes/sandbox/gamemode/cl_init.lua:110: ‘then’ expected near ‘=’

  1. unknown - gamemodes/sandbox/gamemode/cl_init.lua:0

I keep getting ^ that error.


function hidehud(name) --hides the native hud.
	for k, v in pairs({"CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo", })do

		if name == v then return false end
	end
end

hook.Add("HUDShouldDraw", "hidehud", hidehud)

function GM:Initialize() end

surface.CreateFont( "stats", {
		font = "CenterPrintText",
        size = 24,
        weight = 400,
        antialias = true,
        shadow = true
	} ) 

function GM:HUDPaint() --this is where we actually make the hud.
	self.BaseClass:HUDPaint()

	local ply = LocalPlayer() --variables to make calling stats faster.
    local HP  = LocalPlayer():Health()
    local ARM = LocalPlayer():Armor()
    local name = LocalPlayer():GetName()
    local weap = LocalPlayer():GetActiveWeapon()

	local function GetAmmo()
		local weap = ply:GetActiveWeapon()
		if not weap or not ply:Alive() then return -1 end

   		local ammo = weap:Clip1() or 0

   		return ammo
	end

	local function GetWeapon()
		local wepname = LocalPlayer():GetActiveWeapon():GetPrintName()
		if not wepname or not ply:Alive() then return -1 end
		if wepname == "#HL2_SMG1" then
			local wep = "SMG"
		elseif wepname == "#HL2_Crowbar" then
			local wep = "Crowbar"
		else
			local wep = "undefined"
		end
		return wep
	end

    draw.RoundedBoxEx(8, 0, 660, 180, 110, Color(46, 46, 46, 150), false, true, false, false)

    draw.RoundedBox(4,9,690,(HP/100*160+3),25,Color(84,217,63,70))
    local wep=GetWeapon()
    if wep=="SMG" then
    	local ammo=GetAmmo()
    	draw.RoundedBox(4,9,715,((2+(2/9))/100*ammo*160+3),25,Color(235,163,7,70))

    	surface.SetTextColor(255,255,255,255)
    	surface.SetTextPos(10,715)
    	surface.SetFont("stats")
    	surface.DrawText(wep..": "..ammo)
	elseif wep=="Crowbar" then
    	draw.RoundedBox(4,9,715,163,25,Color(240,32,48,50))

    	surface.SetTextColor(255,255,255,255)
    	surface.SetTextPos(10,715)
    	surface.SetFont("stats")
    	surface.DrawText(wep)
	else
		draw.RoundedBox(4,9,715,163,25,Color(32,32,250,50))

		surface.SetTextColor(255,255,255,255)
    	surface.SetTextPos(10,715)
    	surface.SetFont("stats")
    	surface.DrawText(wep)
	end

	if ply:Team()==2 then
		surface.SetTextColor(240,32,48,255)
    	surface.SetTextPos(10,740)
    	surface.SetFont("stats")
    	surface.DrawText("Deaths")
    elseif ply:Team()==3 then
    	surface.SetTextColor(84,217,63,255)
    	surface.SetTextPos(10,740)
    	surface.SetFont("stats")
    	surface.DrawText("Runners")
    end







    surface.SetTextColor(255,255,255,255)
    surface.SetTextPos(10,690)
    surface.SetFont("stats")
    surface.DrawText("Health: "..HP)

    surface.SetTextColor(255,255,255,255)
    surface.SetTextPos(10,665)
    surface.SetFont("stats")
    surface.DrawText(name)

end


Which line is line 110?

if wepname == “#HL2_SMG1” then

Why are you editing sandbox? Don’t do that. Use


hook.Add("HUDPaint","myhook",function()
    /*draw here*/
end)

Also, the function




	local function GetWeapon()
		local wepname = LocalPlayer():GetActiveWeapon():GetPrintName()
		if not wepname or not ply:Alive() then return -1 end
		if wepname == "#HL2_SMG1" then
			local wep = "SMG"
		elseif wepname == "#HL2_Crowbar" then
			local wep = "Crowbar"
		else
			local wep = "undefined"
		end
		return wep
	end

Will never work, cause of “local wep” part, please read up on “scopes” in programming.

I understand why it doesn’t work now, I didn’t realize an if statement was considered a block. Is there a way to make the variable local only to the function and not the if statement?

As for using the HUDPaint hook over the GM:HUDPaint() function, is this the correct format?



function hud()
--Hud code
end

hook.Add("HUDPaint","hud",hud)


Move the local up and only set the value in the ifs.

eg.

[lua]
local a
if b then
a = “something”
else
a = “something else”
end
return a
[/lua]

And yes for the hook.Add but best make the function local.

Change the way you’re doing the hide-hud stuff.

https://dl.dropboxusercontent.com/u/26074909/tutoring/benchmarking_tips/benchmarking_hud_stuff.lua

There’s a post I made on FP showing that just by changing the table.HasValue search method to direct key index method, you reduce the time needed to access by a LOT!

In your code:
[lua]if wepname == “#HL2_SMG1” then
local wep = “SMG”
elseif wepname == “#HL2_Crowbar” then
local wep = “Crowbar”
else
local wep = “undefined”
end[/lua]

When you return wep, it will be nil. The reason being is that you’re defining wep inside the scope of the if, at the end of the if which ends at elseif, else, or end, wep becomes nil.

define local wep = nil; before the if statement, then instead of local wep =… use wep = …

Edit: Woops, opened this an hour or so ago, and didn’t refresh before responding. Previous post about the moving the local up is the second thing I responded about, however the first is still relevant.

This is what I have so far. For appearance It looks great, the help on the GetWeapon function worked but after being on the server for about 1 minute my framerate starts to drop. It continues to drop for a couple of minutes until at around 7 frames per second. I think the problem may be that it is drawing the hud every tick without erasing it but I have no idea how to fix it. Help please?


function GM:HUDPaint() --this is where we actually make the hud.
	local ply = LocalPlayer() --variables to make calling stats faster.
    local HP  = LocalPlayer():Health()
    local ARM = LocalPlayer():Armor()
    local name = LocalPlayer():GetName()
    local weap = LocalPlayer():GetActiveWeapon()

	local function GetAmmo()
		local weap = ply:GetActiveWeapon()
		if not weap or not ply:Alive() then return -1 end

   		local ammo = weap:Clip1() or 0

   		return ammo
	end

	local function GetWeapon()
		local wep
		if not ply:Alive() then return -1 end
		local wepname = LocalPlayer():GetActiveWeapon():GetPrintName()
		if not weap then return -1 end
		if wepname == "#HL2_SMG1" then
			wep = "SMG"
		elseif wepname == "#HL2_Crowbar" then
			wep = "Crowbar"
		else
			wep = "undefined"
		end
		return wep
	end

    draw.RoundedBox(0, 0, 660, 180, 110, Color(46, 46, 46, 150))

    draw.RoundedBox(0,9,690,163,25,Color(0,0,0,200))

    draw.RoundedBox(0,9,715,163,25,Color(0,0,0,200))

    draw.RoundedBox(0,9,690,(HP/100*160+3),25,Color(17,69,0,255))
    draw.RoundedBox(0,9,690,(HP/100*160+3),7,Color(32,117,8,255))

    local wep=GetWeapon()
    if wep=="SMG" then
    	local ammo=GetAmmo()
    	draw.RoundedBox(0,9,715,((2+(2/9))/100*ammo*160+3),25,Color(235,163,7,70))

    	surface.SetTextColor(255,255,255,255)
    	surface.SetTextPos(10,727)
    	surface.SetFont("stats")
    	surface.DrawText(wep..": "..ammo)
	elseif wep=="Crowbar" then
    	draw.RoundedBox(0,9,715,163,25,Color(200,0,0,255))
    	draw.RoundedBox(0,9,715,163,7,Color(255,0,0,255))

    	surface.SetTextColor(255,255,255,255)
    	surface.SetTextPos(10,727)
    	surface.SetFont("stats")
    	surface.DrawText(wep..": ".."∞")
	elseif wep=="undefined" then
		draw.RoundedBox(0,9,715,163,25,Color(32,32,250,50))

		surface.SetTextColor(255,255,255,255)
    	surface.SetTextPos(10,727)
    	surface.SetFont("stats")
    	surface.DrawText(wep)
	end

	if ply:Team()==2 then
		surface.SetTextColor(240,32,48,255)
    	surface.SetTextPos(10,740)
    	surface.SetFont("name/team")
    	surface.DrawText("Deaths")
    elseif ply:Team()==3 then
    	surface.SetTextColor(84,217,63,255)
    	surface.SetTextPos(10,740)
    	surface.SetFont("name/team")
    	surface.DrawText("Runners")
    end

    if not ply:Alive() then
    	surface.SetTextColor(190,190,190,255)
    	surface.SetTextPos(10,715)
    	surface.SetFont("name/team")
    	surface.DrawText("Spectator")
    end


    	draw.RoundedBox(0,0,0,200,75,Color(0,0,0,150))

    	local points = ply:PS_GetPoints()

    	surface.SetTextColor(250,250,160,255)
    	surface.SetTextPos(60,5)
    	surface.SetFont("name/team")
    	surface.DrawText(points.." Eggs")

    	local rt = string.ToMinutesSeconds(self:GetRoundTime())

    	surface.SetTextColor(255,255,255,255)
    	surface.SetTextPos(10,665)
    	surface.SetFont("name/team")
    	surface.DrawText(rt)

    surface.SetTextColor(255,255,255,255)
    surface.SetTextPos(10,702)
    surface.SetFont("stats")
    surface.DrawText("Health: "..HP)

    surface.SetTextColor(255,255,255,255)
    surface.SetTextPos(10,40)
    surface.SetFont("name/team")
    surface.DrawText(name)

local Avatar = vgui.Create( "AvatarImage", Panel )
Avatar:SetSize( 32,32 )
Avatar:SetPos( 4,4 )
Avatar:SetPlayer( LocalPlayer(), 64 )



end

local HideHudElements = { 
	CHudCrosshair = false;
	CHudHealth = true;
	CHudBattery = true;
	CHudAmmo = true;
	CHudSecondaryAmmo = true;
	CHudHintDisplay = false;
	CHudHistoryResource = false;
};
function GM:HUDShouldDraw( _name )
	return !HideHudElements[ _name ];
end

The problem is that you’re creating an Avatar VGUI element every frame; among other things. It’s eating the memory until it’s gone.

Remove the local function out of the hud loop and put it at the top of the file or above the hud paint function. Make a secondary function, call it local init for all intensive purposes. Inside init do something like this:

[lua]if ( !LocalPlayer( ).__AvatarImage ) then LocalPlayer( ).__AvatarImage = vgui.Create( “AvatarImage” ); // Extend this with the information
end[/lua]

Then, you can call your Init as many times as you want inside the HUDPaint function, every frame, and it will be safe front overflowing your memory. Define things once / create them if they don’t exist. Define them under local variables either by attaching them to the player, or by creating a table at the top of the file as local, or single variables containing each part. Then simply render the data in HUDPaint.