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 :/
Script runs on client side only
It does not. Internally, the function just returns the entity's health variable.
[QUOTE=code_gs;52723792]It does not. Internally, the function just returns the entity's health variable.[/QUOTE]
I know thatt the function returns the entity's health variable, But it returns it slow for me :/
For me it takes like a second or two to update
This is the script:
Located in lua\autorun\npchpbars.lua
[CODE]
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
[/CODE]
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_.
[QUOTE=code_gs;52723821]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_.[/QUOTE]
This is just a quick script ._.
For the variables, I've done this:
[CODE]
//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 )
[/CODE]
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.
[QUOTE=Elspin;52724003]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.[/QUOTE]
I took more look into serverside and i think i've done it
script:
[CODE]
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
[/CODE]
You're sharing one global variable -- that won't work for multiple NPCs.
[QUOTE=code_gs;52725103]You're sharing one global variable -- that won't work for multiple NPCs.[/QUOTE]
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.
[QUOTE=code_gs;52725215]You could just use a network var on the NPC entity.[/QUOTE]
Can you please elaborate? How can i do this or if you could link me to a garry's mod wiki?
[QUOTE=code_gs;52725231][url]https://wiki.garrysmod.com/page/Entity/SetNWInt[/url][/QUOTE]
This works perfectly
Thank you all for your time :D
Code:
[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
[/CODE]
[QUOTE=Ia_grib;52726542]Why can't you just call ent:Health() clientside instead of networking it like that?[/QUOTE]
It's not networked immediately internally
Sorry, you need to Log In to post a reply to this thread.