Hello FacePunch, I need some help coding a HUD. I released some code that I later decided to scrap because it was a bulkey design. It is now one bar. I am needing to invert or reverse the animation of which the armor bar increces and decreses when the armor value is changed. I am sure that there is another way of drawing the Health and Armor bar because people that post on ScriptFodder have done it. I will provide my stupid code below.
[CODE]
local function Armor()
local DrawArmor = LocalPlayer():Armor() or 0 -- Draw Armor
local EchoArmor = LocalPlayer():Armor() or 0
local x, y = 455, ScrH() - 22
local w, h = ScrW() - 910 , 10
if DrawArmor > 100 then DrawArmor = 100 end
if DrawArmor < 0 then DrawArmor = 0 end
if DrawArmor != 0 then
surface.SetDrawColor(255, 255, 255, 255)
surface.DrawRect(x, y, w * DrawArmor / 100, h)
draw.NoTexture()
end
end
[/CODE]
Thank you.
#Learning Lua :smile:
[QUOTE=Syntax2056;50767585]Hello FacePunch, I need some help coding a HUD. I released some code that I later decided to scrap because it was a bulkey design. It is now one bar. I am needing to invert or reverse the animation of which the armor bar increces and decreses when the armor value is changed. I am sure that there is another way of drawing the Health and Armor bar because people that post on ScriptFodder have done it. I will provide my stupid code below.
:snip:
Thank you.
#Learning Lua :smile:[/QUOTE]
Do you want the armor bar to smoothly interpolate when the armor value changes?
If this is the case, the following code should work.
[lua]
-- move the DrawArmor local outside the function
-- so that it persists across function calls
local DrawArmor
local function Armor()
local RealArmor = LocalPlayer():Armor()
local x, y = 455, ScrH() - 22
local w, h = ScrW() - 910 , 10
-- this will set the bar to be initially at the player's
-- armor level, when the HUD is initialized
-- this will only happen once per script initialization
if (DrawArmor == nil) then
DrawArmor = RealArmor
end
/*
if DrawArmor > 100 then DrawArmor = 100 end
if DrawArmor < 0 then DrawArmor = 0 end
*/
-- the above can be replaced with a single clamp
RealArmor = math.Clamp(RealArmor, 0, 100)
-- here we are going to interpolate the DrawArmor value
-- towards the RealArmor
-- math.Approach is a convenience function
-- in this case, it provides a linear interpolation
-- if you want more complex interpolation (quadratic, etc.)
-- you will need your own algorithm
-- multiplying the approach rate by RealFrameTime() makes it constant, which is a good thing
-- in this case, the armor bar will approach the real armor value
-- at the rate of 10 per second
DrawArmor = math.Approach(DrawArmor, RealArmor, RealFrameTime() * 10)
if DrawArmor > 0 then
surface.SetDrawColor(255, 255, 255, 255)
surface.DrawRect(x, y, w * DrawArmor / 100, h)
draw.NoTexture()
end
end
hook.Add("HUDPaint", "test", Armor)
[/lua]
You can use another interpolation technique if you want it to be 'smoother', but there is a plethora of ways to achieve this, and it depends on whether or not you actually need it.
[QUOTE=typedef state;50767691]Do you want the armor bar to smoothly interpolate when the armor value changes?
If this is the case, the following code should work.
[lua]
-- move the DrawArmor local outside the function
-- so that it persists across function calls
local DrawArmor
local function Armor()
local RealArmor = LocalPlayer():Armor()
local x, y = 455, ScrH() - 22
local w, h = ScrW() - 910 , 10
-- this will set the bar to be initially at the player's
-- armor level, when the HUD is initialized
-- this will only happen once per script initialization
if (DrawArmor == nil) then
DrawArmor = RealArmor
end
/*
if DrawArmor > 100 then DrawArmor = 100 end
if DrawArmor < 0 then DrawArmor = 0 end
*/
-- the above can be replaced with a single clamp
RealArmor = math.Clamp(RealArmor, 0, 100)
-- here we are going to interpolate the DrawArmor value
-- towards the RealArmor
-- math.Approach is a convenience function
-- in this case, it provides a linear interpolation
-- if you want more complex interpolation (quadratic, etc.)
-- you will need your own algorithm
-- multiplying the approach rate by RealFrameTime() makes it constant, which is a good thing
-- in this case, the armor bar will approach the real armor value
-- at the rate of 10 per second
DrawArmor = math.Approach(DrawArmor, RealArmor, RealFrameTime() * 10)
if DrawArmor > 0 then
surface.SetDrawColor(255, 255, 255, 255)
surface.DrawRect(x, y, w * DrawArmor / 100, h)
draw.NoTexture()
end
end
hook.Add("HUDPaint", "test", Armor)
[/lua]
You can use another interpolation technique if you want it to be 'smoother', but there is a plethora of ways to achieve this, and it depends on whether or not you actually need it.[/QUOTE]
Is there any way to reverse the animation so it animates from right to left?
[QUOTE=Syntax2056;50767714]Is there any way to reverse the animation so it animates from right to left?[/QUOTE]
Change the draw function to something like this
[lua]
surface.DrawRect(x + (w * (1 - DrawArmor / 100)), y, w * DrawArmor / 100, h)
[/lua]
[QUOTE=typedef state;50767730]Change the draw function to something like this
[lua]
surface.DrawRect(x + (w * (1 - DrawArmor / 100)), y, w * DrawArmor / 100, h)
[/lua][/QUOTE]
Thank you for helping me. This is what i needed. Is there any way I can learn more lua like this or just keep posting when I need help?
[QUOTE=Syntax2056;50767741]Thank you for helping me. This is what i needed. Is there any way I can learn more lua like this or just keep posting when I need help?[/QUOTE]
This specific thing isn't really related to Lua, or even programming in general. It's arithmetic.
For learning Lua and GMod Lua, however, there is a plethora of resources.
If you wish to gain a better grasp of Lua, you should read [url=https://www.lua.org/pil/contents.html]Programming in Lua[/url]
There are also many tutorials on the GMod wiki, as I suspect you are aware, but I will provide the links anyway.
[url]http://wiki.garrysmod.com/page/Category:Modding_Tutorials[/url]
[url]http://wiki.garrysmod.com/page/Category:Lua_Tutorials[/url]
[b]EDIT:[/b] Another good source of learning material can be from reading other's people code. Popular and well made addons/gamemodes can be a very useful study material, and it will also help you learn how to read other's people code and understand it, which is a very useful skill. This is especially true if you don't know how to do something, but you know that an addon somewhere does it - you can just go and look at it, if you wonder how.
Ok, Thanks for the help.
Sorry, you need to Log In to post a reply to this thread.