• Health bar
    11 replies, posted
so im creating a hud, and i dont know how to do a health bar that doesnt go out of your screen when its over 100 Like is there a code for a armor ar & health bar that doesnt overflow? Also, when i did my health bar it faded thro my hud, can see it but its like in the back
Use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/GetMaxHealth]Entity:GetMaxHealth[/url] instead of assuming the max is 100. Just in case, limit the total width of the bar using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/math/Clamp]math.Clamp[/url] In regard to it 'fading' through your HUD, make sure you're drawing things in reverse order that you want them to be displayed. [lua]drawBackground() drawForeground() drawText()[/lua] As an example of your order.
[QUOTE=Internet1001;51039037]Use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/GetMaxHealth]Entity:GetMaxHealth[/url] instead of assuming the max is 100. Just in case, limit the total width of the bar using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/math/Clamp]math.Clamp[/url] In regard to it 'fading' through your HUD, make sure you're drawing things in reverse order that you want them to be displayed. [lua]drawBackground() drawForeground() drawText()[/lua] As an example of your order.[/QUOTE] how do i use the healthbar thing? My health bar right now : [QUOTE]local function Healthforeground() local Health = 0 local function hudPaint() local x, y = 1, ScrH() - 1 local localplayer = LocalPlayer() Health = math.min(100, (Health == localplayer:Health() and Health) or Lerp(0.1, Health, localplayer:Health())) local DrawHealth = math.Min(Health / GAMEMODE.Config.startinghealth, 1) local Border = math.Min(6, math.pow(2, math.Round(3*DrawHealth))) draw.RoundedBox(Border, x + 4, y - 30, 200 - 10, 20, Color(0,0,0,200)) draw.RoundedBox(Border, x + 5, y - 29, (200 - 9) * DrawHealth, 18, Color(140,0,0,180)) draw.DrawText(math.Max(0, math.Round(localplayer:Health())), "DarkRPHUD2", x + 4 + (200 - 8)/2, y - 32, Color(255,255,255,200), 1) -- Armor local armor = localplayer:Armor() if armor ~= 0 then draw.RoundedBox(2, x + 5, y - 15, (200 - 8) * armor / 100, 5, Color(0, 0, 255, 255)) end end end hook.Add("HUDPaint", "DarkRP_Mod_HUDPaint", hudPaint)[/QUOTE] (the Foreground,background,text)
can you put code in [QUOTE] [CODE][/ CODE] [/QUOTE] ? also what is this? [lua]local function Healthforeground()[/lua]
[QUOTE=kem008;51040619]- code tags -[/QUOTE] This is off-topic but just FYI, if it's lua you should use [lua] tags. There's no difference right now, but possibly at some point in the future there will be - and old posts will be retroactively affected.
Post you're whole code because as of now looks like you have extra ends for no reason. also why are you creating a function inside another function?
[CODE]local function Base() -- Left to right bar draw.RoundedBox(6, ScrW() - 2000 - 150, ScrH () - 70 - 10, 2700, 20, Color(0, 204, 0, 220)) -- The bars (up to down) draw.RoundedBox(1, ScrW() - 1500 - 50, ScrH () - 1 - 59, 20, 200, Color(0, 204, 0, 220)) draw.RoundedBox(1, ScrW() - 2000 - 50, ScrH () - 1 - 59, 20, 200, Color(0, 204, 0, 220)) draw.RoundedBox(1, ScrW() - 1000 - 50, ScrH () - 1 - 59, 20, 200, Color(0, 204, 0, 220)) -- dunno what this is draw.RoundedBox(6, ScrW() - 500 - 50, ScrH () - 1 - 80, 20, 200, Color(0, 204, 0, 220)) -- White boxes draw.RoundedBox(1, ScrW() - 1900 - 50, ScrH () - 50 - 10, 400, 100, Color(255, 255, 255, 60)) draw.RoundedBox(1, ScrW() - 1480 - 50, ScrH () - 50 - 10, 480, 100, Color(255, 255, 255, 60)) draw.RoundedBox(1, ScrW() - 980 - 50, ScrH () - 50 - 10, 480, 100, Color(255, 255, 255, 60)) draw.RoundedBox(1, ScrW() - 480 - 50, ScrH () - 50 - 10, 530, 100, Color(255, 255, 255, 60)) end local function healthbar() local Health = 0 local function hudPaint() local x, y = 1, ScrH() - 1 local localplayer = LocalPlayer() Health = math.min(100, (Health == localplayer:Health() and Health) or Lerp(0.1, Health, localplayer:Health())) local DrawHealth = math.Min(Health / GAMEMODE.Config.startinghealth, 1) local Border = math.Min(6, math.pow(2, math.Round(3*DrawHealth))) draw.RoundedBox(Border, x + 4, y - 30, 200 - 10, 20, Color(0,0,0,200)) draw.RoundedBox(Border, x + 5, y - 29, (200 - 9) * DrawHealth, 18, Color(140,0,0,180)) draw.DrawText(math.Max(0, math.Round(localplayer:Health())), "DarkRPHUD2", x + 4 + (200 - 8)/2, y - 32, Color(255,255,255,200), 1) -- Armor local armor = localplayer:Armor() if armor ~= 0 then draw.RoundedBox(2, x + 5, y - 15, (200 - 8) * armor / 100, 5, Color(0, 0, 255, 255)) end end end hook.Add("HUDPaint", "DarkRP_Mod_HUDPaint", hudPaint) /*--------------------------------------------------------------------------- HUD ConVars ---------------------------------------------------------------------------*/ local ConVars = {} local HUDWidth local HUDHeight local Color = Color local cvars = cvars local DarkRP = DarkRP local CurTime = CurTime local draw = draw local GetConVar = GetConVar local IsValid = IsValid local Lerp = Lerp local localplayer local math = math local pairs = pairs local ScrW, ScrH = ScrW, ScrH local SortedPairs = SortedPairs local string = string local surface = surface local table = table local timer = timer local tostring = tostring CreateClientConVar("weaponhud", 0, true, false) local colors = {} colors.black = Color(0, 0, 0, 255) colors.blue = Color(0, 0, 255, 255) colors.brightred = Color(200, 30, 30, 255) colors.darkred = Color(0, 0, 70, 100) colors.darkblack = Color(0, 0, 0, 200) colors.gray1 = Color(0, 0, 0, 155) colors.gray2 = Color(51, 58, 51,100) colors.red = Color(255, 0, 0, 255) colors.white = Color(255, 255, 255, 255) colors.white1 = Color(255, 255, 255, 200) local function ReloadConVars() ConVars = { background = {0,0,0,100}, Healthbackground = {0,0,0,200}, Healthforeground = {140,0,0,180}, HealthText = {255,255,255,200}, Job1 = {0,0,150,200}, Job2 = {0,0,0,255}, salary1 = {0,150,0,200}, salary2 = {0,0,0,255} } for name, Colour in pairs(ConVars) do ConVars[name] = {} for num, rgb in SortedPairs(Colour) do local CVar = GetConVar(name..num) or CreateClientConVar(name..num, rgb, true, false) table.insert(ConVars[name], CVar:GetInt()) if not cvars.GetConVarCallbacks(name..num, false) then cvars.AddChangeCallback(name..num, function() timer.Simple(0,ReloadConVars) end) end end ConVars[name] = Color(unpack(ConVars[name])) end HUDWidth = (GetConVar("HudW") or CreateClientConVar("HudW", 240, true, false)):GetInt() HUDHeight = (GetConVar("HudH") or CreateClientConVar("HudH", 115, true, false)):GetInt() if not cvars.GetConVarCallbacks("HudW", false) and not cvars.GetConVarCallbacks("HudH", false) then cvars.AddChangeCallback("HudW", function() timer.Simple(0,ReloadConVars) end) cvars.AddChangeCallback("HudH", function() timer.Simple(0,ReloadConVars) end) end end ReloadConVars() local Scrw, Scrh, RelativeX, RelativeY /*--------------------------------------------------------------------------- HUD Seperate Elements ---------------------------------------------------------------------------*/ local Page = Material("icon16/page_white_text.png") local function GunLicense() if localplayer:getDarkRPVar("HasGunlicense") then surface.SetMaterial(Page) surface.SetDrawColor(255, 255, 255, 255) surface.DrawTexturedRect(RelativeX + HUDWidth, ScrH() - 34, 32, 32) end end local function Agenda() local ply = LocalPlayer() local agenda = ply:getAgendaTable() if not agenda then return end draw.RoundedBox(10, 10, 10, 460, 110, colors.gray1) draw.RoundedBox(10, 12, 12, 456, 106, colors.gray2) draw.RoundedBox(10, 12, 12, 456, 20, colors.darkred) draw.DrawNonParsedText(agenda.Title, "DarkRPHUD1", 30, 12, colors.red, 0) local text = ply:getDarkRPVar("agenda") or "" text = text:gsub("//", "\n"):gsub("\\n", "\n") text = DarkRP.textWrap(text, "DarkRPHUD1", 440) draw.DrawNonParsedText(text, "DarkRPHUD1", 30, 35, colors.white, 0) end local VoiceChatTexture = surface.GetTextureID("voice/icntlk_pl") local function DrawVoiceChat() if localplayer.DRPIsTalking then local chbxX, chboxY = chat.GetChatBoxPos() local Rotating = math.sin(CurTime()*3) local backwards = 0 if Rotating < 0 then Rotating = 1-(1+Rotating) backwards = 180 end surface.SetTexture(VoiceChatTexture) surface.SetDrawColor(ConVars.Healthforeground) surface.DrawTexturedRectRotated(ScrW() - 100, chboxY, Rotating*96, 96, backwards) end end CreateConVar("DarkRP_LockDown", 0, {FCVAR_REPLICATED, FCVAR_SERVER_CAN_EXECUTE}) local function LockDown() local chbxX, chboxY = chat.GetChatBoxPos() if util.tobool(GetConVarNumber("DarkRP_LockDown")) then local cin = (math.sin(CurTime()) + 1) / 2 local chatBoxSize = math.floor(ScrH() / 4) draw.DrawNonParsedText(DarkRP.getPhrase("lockdown_started"), "ScoreboardSubtitle", chbxX, chboxY + chatBoxSize, Color(cin * 255, 0, 255 - (cin * 255), 255), TEXT_ALIGN_LEFT) end end local Arrested = function() end usermessage.Hook("GotArrested", function(msg) local StartArrested = CurTime() local ArrestedUntil = msg:ReadFloat() Arrested = function() if CurTime() - StartArrested <= ArrestedUntil and localplayer:getDarkRPVar("Arrested") then draw.DrawNonParsedText(DarkRP.getPhrase("youre_arrested", math.ceil(ArrestedUntil - (CurTime() - StartArrested))), "DarkRPHUD1", ScrW()/2, ScrH() - ScrH()/12, colors.white, 1) elseif not localplayer:getDarkRPVar("Arrested") then Arrested = function() end end end end) local AdminTell = function() end usermessage.Hook("AdminTell", function(msg) timer.Destroy("DarkRP_AdminTell") local Message = msg:ReadString() AdminTell = function() draw.RoundedBox(4, 10, 10, ScrW() - 20, 100, colors.darkblack) draw.DrawNonParsedText(DarkRP.getPhrase("listen_up"), "GModToolName", ScrW() / 2 + 10, 10, colors.white, 1) draw.DrawNonParsedText(Message, "ChatFont", ScrW() / 2 + 10, 80, colors.brightred, 1) end timer.Create("DarkRP_AdminTell", 10, 1, function() AdminTell = function() end end) end) /*--------------------------------------------------------------------------- Entity HUDPaint things ---------------------------------------------------------------------------*/ local function DrawPlayerInfo(ply) local pos = ply:EyePos() pos.z = pos.z + 10 -- The position we want is a bit above the position of the eyes pos = pos:ToScreen() pos.y = pos.y - 50 -- Move the text up a few pixels to compensate for the height of the text if GAMEMODE.Config.showname and not ply:getDarkRPVar("wanted") then draw.DrawNonParsedText(ply:Nick(), "DarkRPHUD2", pos.x + 1, pos.y + 1, colors.black, 1) draw.DrawNonParsedText(ply:Nick(), "DarkRPHUD2", pos.x, pos.y, team.GetColor(ply:Team()), 1) end
Method without math.Clamp [code] LocalPlayer():GetMaxHealth / 100 LocalPlayer():Health / 100 // You can divide cur health by max health to get the percentage [/code] Works with health values over 100, as long as the max health isn't passed
[QUOTE=Skere_;51043650]Method without math.Clamp [code] LocalPlayer():GetMaxHealth / 100 LocalPlayer():Health / 100 // You can divide cur health by max health to get the percentage [/code] Works with health values over 100, as long as the max health isn't passed[/QUOTE] Nvm to all, i give up on devving. i suck at it
[QUOTE=Jason138;51043828]Nvm to all, i give up on devving. i suck at it[/QUOTE] You should try something simpler and work your way up and try to understand what things mean. Hell my sweps entity doesn't work but i'm not going to give up.
[QUOTE=Keosan;51045582]You should try something simpler and work your way up and try to understand what things mean. Hell my sweps entity doesn't work but i'm not going to give up.[/QUOTE] ye im starting with Derma :)
[code] function getHealth (ply) if (ply:MaxHealth() > 100 or ply:MaxHealth() < 100) and ply:Health > 0 then return ((ply:Health() / ply:MaxHealth())*100) elseif !(ply:Alive()) then return 0 else return ply:Health() end //in your draw section for your health bar draw.RoundedBox (r,x,y,getHealth (ply),12,Color (255,0,0,255)) [/code] This algorithm should always give you the width of the health bar as a percentage, the value will always be between 0 and 100. If it's under 100, it just returns the value given (you will have to add to this code if you use a max health less than 100). If the player is dead then it'll return zero... Granted.. The health bar width in this case is always 0 through 100, of you wanted it bigger or smaller you'd have to play with it. Mine is just a basic example of getting a bar working from 0 to 100 Edit Panels build from back to front, whatever is rendered first will be further back. There might be a z index value to change this, but I'm not sure.
Sorry, you need to Log In to post a reply to this thread.