Is there a delay in Entity:Health()?

I think the title is self explanatory, I’ve noticed that the Entity:Health() has some sort of delay
I don’t know if it’s because of the for loop or gmod :confused:
Script runs on client side only

It does not. Internally, the function just returns the entity’s health variable.

I know thatt the function returns the entity’s health variable, But it returns it slow for me :confused:
For me it takes like a second or two to update

This is the script:
Located in lua\autorun
pchpbars.lua




if(CLIENT) then

	hook.Add( "HUDPaint", "DrawHealthBar", function()

		for k, v in pairs(ents.FindByClass("npc_*")) do

			NPC_HEALTH_MAX = v:GetMaxHealth()
			NPC_HEALTH_CURRENT = v:Health()
			
			//HP bar Background
			surface.SetDrawColor( 0,0,0,255 )
			surface.DrawRect( v:GetPos():ToScreen().x, v:GetPos():ToScreen().y, NPC_HEALTH_MAX, 7 )


			//HP bar Foreground
			surface.SetDrawColor( 0,255,0,255 )
			surface.DrawRect( v:GetPos():ToScreen().x, v:GetPos():ToScreen().y, NPC_HEALTH_CURRENT, 7 )

		end

	 end)

end



You should be using local variable, and only drawing if the NPC is on screen. Also, that code only works if the NPC’s class name starts with npc_.

This is just a quick script ._.

For the variables, I’ve done this:


			
  //HP bar Background
  surface.SetDrawColor( 0,0,0,255 )
  surface.DrawRect( v:GetPos():ToScreen().x, v:GetPos():ToScreen().y, v:GetMaxHealth(), 7 )


  //HP bar Foreground
  surface.SetDrawColor( 0,255,0,255 )
  surface.DrawRect( v:GetPos():ToScreen().x, v:GetPos():ToScreen().y, v:Health(), 7 )


Only drawing when NPC is on screen won’t matter much because i’m testing it with only one NPC

And i want this to work with NPC’s only

Are you testing this on a local server or a remote one? I’ve never worked with the underlying source engine netcode directly but I imagine health is just replicated to the client and only at any kind of priority for the player it’s relevant to. You could try printing the same value on a local server serverside and see if it’s delayed in the same way to quickly rule that out.

I took more look into serverside and i think i’ve done it

script:




local NPC_CURRENT_HEALTH= GetGlobalFloat("NPC_GLOBAL_HEALTH")


if(SERVER) then

	AddCSLuaFile()
	hook.Add( "Think", "tik", function()

		for k, v in pairs(ents.FindByClass("npc_*")) do
			SetGlobalFloat("NPC_GLOBAL_HEALTH", v:Health())
			//print("SERVER NPC HEALTH"..v:Health())
			print("SERVER NPC HEALTH:"..GetGlobalFloat("NPC_GLOBAL_HEALTH"))
		end

 	end)


elseif(CLIENT) then

	hook.Add( "HUDPaint", "DrawHealthBar", function()
		for k, v in pairs(ents.FindByClass("npc_*")) do
			
			//HP bar Background
			surface.SetDrawColor( 0,0,0,255 )
			surface.DrawRect( v:GetPos():ToScreen().x, v:GetPos():ToScreen().y, v:GetMaxHealth(), 7 )

			//HP bar Foreground
			surface.SetDrawColor( 0,255,0,255 )
			surface.DrawRect( v:GetPos():ToScreen().x, v:GetPos():ToScreen().y, GetGlobalFloat("NPC_GLOBAL_HEALTH"), 7 )
			print("CLINET NPC HEALTH:"..GetGlobalFloat("NPC_GLOBAL_HEALTH"))

		end

	 end)

end




You’re sharing one global variable – that won’t work for multiple NPCs.

I know that. Like i said, I’m testing this with 1 NPC to see the response
Now i’m figuring out how to make this work with multiple NPCs

You could just use a network var on the NPC entity.

Can you please elaborate? How can i do this or if you could link me to a garry’s mod wiki?

This works perfectly
Thank you all for your time :smiley:

Code:




if(SERVER) then

	AddCSLuaFile()
	hook.Add( "Think", "tik", function()

		for k, v in pairs(ents.FindByClass("npc_*")) do
			v:SetNWInt( 'NPC_HEALTH', v:Health() )
		end

 	end)


elseif(CLIENT) then

	hook.Add( "HUDPaint", "DrawHealthBar", function()
		for k, v in pairs(ents.FindByClass("npc_*")) do

			if(v:GetNWInt( 'NPC_HEALTH' )>0) then

				if(v:GetNWInt( 'NPC_HEALTH' )<v:GetMaxHealth()) then

					//HP bar Background
					surface.SetDrawColor( 0,0,0,255 )
					surface.DrawRect( v:GetPos():ToScreen().x, v:GetPos():ToScreen().y, v:GetMaxHealth(), 7 )

					//HP bar Foreground
					surface.SetDrawColor( 0,255,0,255 )
					surface.DrawRect( v:GetPos():ToScreen().x, v:GetPos():ToScreen().y, v:GetNWInt( 'NPC_HEALTH' ), 7 )

				end
				
			end

		end

	 end)

end



Why can’t you just call ent:Health() clientside instead of networking it like that?

It’s not networked immediately internally

That’s good to know, thanks for info.