SWEP.DrawHUD() isn't updating even with networked vars
5 replies, posted
For some reason, I can't get SWEP.DrawHUD() to update. I have this code in my SWEP:
[code]function SWEP:DrawHUD()
draw.RoundedBox( 8, 5, ScrH()-150, 250, 40, Color(0, 0, 0, 180) );
if self:GetNWInt("RageHUD") > 50 then
draw.SimpleText("Rage: "..tostring(self:GetNWInt("RageHUD")), "ChatFont", 130, ScrH() - 130, Color(255,100,0,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
else
draw.SimpleText("Rage: "..tostring(self:GetNWInt("RageHUD")), "ChatFont", 130, ScrH() - 130, Color(255,0,0,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
end
end[/code]
No matter what value RageHUD is, it acts as if RageHUD is zero, and the HUD script shows "Rage: 0" and in red regardless of the RageHUD networked variable's value.
Before you do your regular "fuck noobs, get kings" thing, yes, I am calling SetNWInt("RageHUD", whatever) wherever needed.
Am I doing something wrong?
You're doing nothing wrong there, show us the code where you're calling SetNWInt?
[code]function SWEP:Think()
self:SetNWInt("RageModeHUD", self.RageMode)
self:SetNWInt("RageHUD", self.Rage)
if self.RageMode == 1 then
if self.Rage > 0 then
self.Owner:SetVelocity( self.Owner:GetForward() * 100 )
self:DashAttacking()
self.Rage = self.Rage - 1
else
self.RageMode = 0
self.Weapon:EmitSound("buttons/button11.wav")
self.RageTrail:Remove()
end
end
end[/code]
Reason for this is because before I started working on the HUD meter, the Rage and RageMode (1 if right-clicking, 0 if not) were SWEP variables (self.Rage and self.RageMode). Would it make a difference if I replaced them all with SetNWInt as well?
lua_run print(tostring(Entity(1):GetActiveWeapon():GetNWInt("RageHUD"))) returns the correct value.
Also, how would I go about dealing damage to / forcing an entity I'm colliding with?
I think, because the self:SetNWInt is also called client side, it overwrites the value the server sends to it (self.Rage and self.RageMode are different on the client and the server!).
Easy solution is:
[lua]
function SWEP:Think()
if CLIENT then return end
self:SetNWInt("RageModeHUD", self.RageMode)
self:SetNWInt("RageHUD", self.Rage)
if self.RageMode == 1 then
if self.Rage > 0 then
self.Owner:SetVelocity( self.Owner:GetForward() * 100 )
self:DashAttacking()
self.Rage = self.Rage - 1
else
self.RageMode = 0
self.Weapon:EmitSound("buttons/button11.wav")
self.RageTrail:Remove()
end
end
end
[/lua]
Clavus is right, however..
Quoted from [b][url=http://wiki.garrysmod.com/?title=Entity.SetNetworkedInt]Entity.SetNetworkedInt [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
- Only set networked variables when necessary - sending networked variables constantly is expensive.
If I were you, I would use datatables instead.
shared.lua
[lua]
function SWEP:SetupDataTables()
self:DTVar( "Int", 0, "RageHUD" )
-- for the next one, change the 0 to a 1, then 2 for the next, etc, you can have up to 4 of each data type
end
[/lua]
You can then access it on the server and client using [b]self.dt.RageHUD[/b], but as with networked variables, setting it on the client will only change it for that client.
[QUOTE=Clavus;31545314]I think, because the self:SetNWInt is also called client side, it overwrites the value the server sends to it (self.Rage and self.RageMode are different on the client and the server!).
Easy solution is:
[lua]
function SWEP:Think()
if CLIENT then return end
self:SetNWInt("RageModeHUD", self.RageMode)
self:SetNWInt("RageHUD", self.Rage)
if self.RageMode == 1 then
if self.Rage > 0 then
self.Owner:SetVelocity( self.Owner:GetForward() * 100 )
self:DashAttacking()
self.Rage = self.Rage - 1
else
self.RageMode = 0
self.Weapon:EmitSound("buttons/button11.wav")
self.RageTrail:Remove()
end
end
end
[/lua][/QUOTE]
Worked like a charm, thanks.
[QUOTE=Drakehawke;31545349]Clavus is right, however..
Quoted from [b][url=http://wiki.garrysmod.com/?title=Entity.SetNetworkedInt]Entity.SetNetworkedInt [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
- Only set networked variables when necessary - sending networked variables constantly is expensive.
If I were you, I would use datatables instead.
shared.lua
[lua]
function SWEP:SetupDataTables()
self:DTVar( "Int", 0, "RageHUD" )
-- for the next one, change the 0 to a 1, then 2 for the next, etc, you can have up to 4 of each data type
end
[/lua]
You can then access it on the server and client using [b]self.dt.RageHUD[/b], but as with networked variables, setting it on the client will only change it for that client.[/QUOTE]
I'll look into it once I set up the other mechanics of the swep, however it doesn't seem to lag much at the moment.
Anyways, anyone got any ideas on dealing damage to entities colliding with the player?
Sorry, you need to Log In to post a reply to this thread.