Very confusing HUD issue - Lua gamemode scripting - Need help

Before I begin, I would like to state that I am not sure if this is the correct place for this thread and I apologize if it is not. Just as well, my first language is not English so I am sorry if I make any grammatical mistakes.

This is very, very annoying to me mainly because I can not figure out why it is happening. I am trying to throw in a new HUD for a game mode, but the current HUD will simply not disappear. My code is:

cl_init.lua



function HUDRemove( HUD )
	for k, v in pairs { "CHudHealth", "CHudBattery", "CHudAmmo", "CHudCrosshair" } do 
		if HUD == v then
			return false
		end
	end
end
hook.Add( "HUDShouldDraw", "HUDRemove", HUDRemove )


I am pretty sure I spelled everything correctly and this is not the first time I have made a HUD for a game mode. Am I making a little mistake? I have also tried the version put up on the Garry’s Mod Wiki, which goes like this:



function GM:HUDShouldDraw( name )
    if ( name == "CHudHealth" or name == "CHudBattery" ) then
        return false
    end
    return true
end


I wasn’t sure where to place this one but I tried it in both the init.lua and the cl_init.lua (not at the same time, of course). I also understand that this second version only hides the Health and the Armor, but even those don’t disappear.

I am frustrated enough to call myself pissed because I can not figure this out. Any help would be very appreciated!

Thanks!

You can approach that problem differently. Also, do it all in one place; just the function; no hook, increase the list size as I have.

use a list, where the index is the name of the element = true. Then, return !table[ name ].

Direct access is faster, I’ll show you two comparisons below.
[lua]local HideHudElements = {
CHudCrosshair = true;
CHudHealth = true;
CHudBattery = true;
CHudAmmo = true;
CHudSecondaryAmmo = true;
CHudHintDisplay = true;
CHudHistoryResource = true;
};
function GM:HUDShouldDraw( _name )
return !HideHudElements[ _name ];
end[/lua]

I ran this example for you, essentially the same as yours using a loop to search 100 million times. The result below.
[lua]local HideHudElements = { “CHudCrosshair”,“CHudHealth”, “CHudBattery”, “CHudAmmo”, “CHudSecondaryAmmo”, “CHudHintDisplay”, “CHudHistoryResource” };
function GM:HUDShouldDraw( name )
local _bHasValue = table.HasValue( HideHudElements, name );
return !_bHasValue;
end

RunBenchmark( function( )
for i = 0, 100000000 do
GM:HUDShouldDraw( “test” );
end
end );[/lua]
Using a loop to search, after 100 million times: elapsed time: 15.36
Even running it only a thousand or more times, I was starting to see results of 0.08 seconds while the other was 0.

Now, using direct access, 100 million times.
[lua]local HideHudElements = {
CHudCrosshair = true;
CHudHealth = true;
CHudBattery = true;
CHudAmmo = true;
CHudSecondaryAmmo = true;
CHudHintDisplay = true;
CHudHistoryResource = true;
};
function GM:HUDShouldDraw( _name )
return !HideHudElements[ _name ];
end

RunBenchmark( function( )
for i = 0, 100000000 do
GM:HUDShouldDraw( “test” );
end
end );[/lua]
Using direct variable access to return the value, after 100 million times: elapsed time: 0.08

Here’s the RunBenchmark function I wrote to test stuff like this:
[lua]//
// Benchmark
//
function RunBenchmark( func, … )
local x = os.clock( )
local ret = func( … );
print( string.format( "elapsed time: %.2f
", os.clock( ) - x ) )
return ret;
end[/lua]

Thank you very much! I noticed that my problem was that I was editing the gamemode in my directory instead of the one in the server’s directory by accident.

Thank you for your optimization tips though. Even though it wasn’t my problem it is very useful to me - especially since I’m still learning Lua ( I am used to C++ )