Hello
I am having a problem with this function. My code gives no errors, but does not work until I run gamemode_reload in console. What I am trying to do is get the entity that the player is looking at and network it, so I can get that entity's networked damage int for my damage system, to show the entity's damage as a HUD Element. I think this should be possible fully client side, but I've had similar problems with it, and a think'd error before I run gamemode_reload. After I run this console command, everything works perfectly. Thank you for your time in reading/responding.
The first code is the server side code, and the second is the client side. They are both included in a gamemode.
[CODE]
function GM:Think()
allply = player.GetAll()
for k, v in pairs( allply ) do
if v:GetEyeTrace().Entity:IsValid() then
v:SetNWEntity( "lookent", v:GetEyeTrace().Entity )
v:SetNWInt("drawdmgbox", 1 )
else
v:SetNWInt("drawdmgbox", 0 )
end
end
end
[/CODE]
[CODE]
ply = LocalPlayer()
function GM:HUDPaint()
drawdmg = ply:GetNWInt( "drawdmgbox" )
lookent = ply:GetNWEntity( "lookent" )
local textdata = {}
textdata.pos = {}
textdata.pos[1] = ScrW()-82
textdata.pos[2] = ScrH()*0.3 - 1
textdata.color = Color(255, 255, 255, 255)
textdata.font = "HUDNumber5"
textdata.xalign = TEXT_ALIGN_LEFT
textdata.yalign = TEXT_ALIGN_TOP
if drawdmg == 1 then
health = lookent:GetNWInt( "wl_health" )
if health > 0 then
textdata.text = tostring( health )
else
textdata.text = "?"
end
draw.RoundedBox( 6, ScrW()-90, ScrH()*0.3, 100, 40, Color( 0, 0, 0, 150 ) )
draw.Text( textdata )
end
end
[/CODE]
I don't get it, you're passing the entity to the client, and then getting a "wl_health" NW'd int off of it.
Why not just pass the NW int in the first place? What is wl_health?W here's it getting assigned?
[QUOTE=Lord Ned;26536069]I don't get it, you're passing the entity to the client, and then getting a "wl_health" NW'd int off of it.
Why not just pass the NW int in the first place? What is wl_health?W here's it getting assigned?[/QUOTE]
Oh, right. Idk why I had it in my head to do it this way.... Really tired I guess. Thanks, I'll see if this works and put results here. That is a much better way of doing it, but I'm not thinking it will fix this problem.
I should have put all of the relevant code. I have a damage system, and wl_health is the entity's health. I know this probably isn't the most efficient way to make a damage system, but it works.
[CODE]
-- Add entities to the preset max health list here. This will override the weight-dependant method
entListHealth = {}
entListHealth["gmod_turret"] = 100
entListHealth["gmod_wire_expression2"] = 10
entListHealth["gmod_wire_gate"] = 10
--topclamp = 10000
--CreateConVar( "war_maxenthealth", "10000", { FCVAR_REPLICATED, FCVAR_ARCHIVE } )
--cvars.AddChangeCallback("war_maxenthealth", setTopClamp)
--function setTopClamp(CVar, PValue, NValue)
-- print("Max entity health changed from "..PValue.." to "..NValue.."!")
-- topClamp = NValue
--end
function GM:OnEntityCreated( ent )
ent:SetNetworkedInt( "wl_damage", 0 )
end
function GM:EntityTakeDamage( ent, inflictor, attacker, amount, damageinfo )
if !ent:IsPlayer() then
maxhealth = ent:GetPhysicsObject():GetMass()
for k, v in pairs(entListHealth) do
if ent:GetClass() == k then
maxhealth = v
end
end
maxhealth = math.Clamp( maxhealth, 1, 10000 )
curdamage = ent:GetNetworkedInt( "wl_damage" )
newdamage = ( curdamage + amount )
ent:SetNetworkedInt( "wl_damage", newdamage)
ent:SetNetworkedInt( "wl_maxhealth", maxhealth )
health = maxhealth - newdamage
ent:SetNetworkedInt( "wl_health", health )
if health <= 0 then
ent:Remove()
effectdata = EffectData()
effectdata:SetStart( ent:GetPos() )
effectdata:SetOrigin( ent:GetPos() )
effectdata:SetScale( 1 )
util.Effect( "Explosion", effectdata )
end
end
end
[/CODE]
[b]EDIT:[/b] Rewrote it how you said to, no change.
Sorry, you need to Log In to post a reply to this thread.