Capping the health bar.

Alright so I need some help trying to understand how to make it so the health bar does not go off the screen (that is what its doing) now I went on the gmod wiki and found the mathClamp thing but I could not make sense of it. If someone could explain it or have a different aproach to it please tell me

here is the code:




local health = LocalPlayer():Health() or 0

if health > 100 then health = health end
        if health < 0 then health = 0 end
        if health != 0 then

        surface.SetDrawColor( 255, 255, 255, 255 )
        surface.SetMaterial( red )
        surface.DrawTexturedRect( ScrW() / 11.5, ScrH () / 1.265, health * 3.1, 47)
    end


Now I want it to write down the HP but I don’t want the bar to go off the screen.

What’s your end goal? Can you mock it up in paint or something?

but with the HP and armour being above 100 but the actual red and blue bars being the same so they won’t go off the whole screen

edit this line


if health > 100 then health = health end

to this


if health > 100 then health = 100 end

Hmmm, not quite. It did stop the bar from going off the screen but the HP text is also capped to 100.

[editline]16th April 2016[/editline]

Im wondering if I making a new local variable just for the HP text. Would that work?

[editline]16th April 2016[/editline]

I should expect a bunch of dumb ratings xD

Try changing this:



surface.DrawTexturedRect( ScrW() / 11.5, ScrH () / 1.265, health * 3.1, 47)


To this:



surface.DrawTexturedRect( ScrW() / 11.5, ScrH () / 1.265, math.Clamp( health, 0, LocalPlayer():GetMaxHealth() ) * 3.1, 47)


[editline]16th April 2016[/editline]

That should theoratically clamp the width of the bar so it’s never less than 0 or more than the player’s max health

[editline]16th April 2016[/editline]

Also, please change this bit:



if health > 100 then health = health end
if health < 0 then health = 0 end


To something like this:



health = math.Clamp( health, 0, LocalPlayer():GetMaxHealth() ) -- a player's max health isn't always over 100


Now I am getting this lua error in the console:




[DarkRP] addons/darkrpmodification-master/lua/darkrp_modules/swrphud/cl_battlefronthud.lua:80: bad argument #1 to 'SetMaterial' (IMaterial expected, got nil)
  1. SetMaterial - [C]:-1
   2. unknown - addons/darkrpmodification-master/lua/darkrp_modules/swrphud/cl_battlefronthud.lua:80
    3. doInclude - [C]:-1
     4. loadModules - gamemodes/darkrp/gamemode/libraries/modificationloader.lua:102
      5. Call - gamemodes/darkrp/gamemode/libraries/modificationloader.lua:147
       6. unknown - gamemodes/darkrp/gamemode/cl_init.lua:53


Where are you setting the red variable as an IMaterial?

Line 80:



surface.SetMaterial( ourMat )


anything to do with ourMat



local ourMat = Material( "vgui/saber.png")


But… in the code you posted you did



surface.SetMaterial( red )


?
Also, I meant where are you calling it- as in are you calling it in an ‘if’ statement or anything like that? Or, before or after the bit where you’re drawing the thing?

in the lua error it says line 80

[editline]16th April 2016[/editline]

Here is the “red”



    if health > 100 then health = health end
        if health < 0 then health = 0 end
        if health != 0 then

        surface.SetDrawColor( 255, 255, 255, 255 )
        surface.SetMaterial( red )
        surface.DrawTexturedRect( ScrW() / 11.5, ScrH () / 1.265, math.Clamp( health, 0, LocalPlayer():GetMaxHealth() ) * 3.1, 47)
    end



I know, but that doesn’t tell me if you’re calling it before or after you’re setting the material… could you maybe post a bit more code than you did before, cause I don’t really know why it isn’t working unless you’re doing something wrong in any previous or later code

You asked me to change the if statement to a variable which I did not get.

[editline]16th April 2016[/editline]



local ourMat = Material( "vgui/saber.png")
    local blue = Material( "vgui/blue.png")
    local red = Material( "vgui/red.png")
    local health = LocalPlayer():Health() or 0
    local armor = LocalPlayer():Armor() or 0

    if health > 100 then health = health end
        if health < 0 then health = 0 end
        if health != 0 then

        surface.SetDrawColor( 255, 255, 255, 255 )
        surface.SetMaterial( red )
        surface.DrawTexturedRect( ScrW() / 11.5, ScrH () / 1.265, math.Clamp( health, 0, LocalPlayer():GetMaxHealth() ) * 3.1, 47)
    end

    surface.SetDrawColor( 255, 255, 255, 255 )
    surface.SetMaterial( ourMat )
    surface.DrawTexturedRect( ScrW() / 100, ScrH () / 1.246, 140, 30)

    draw.RoundedBox(8, ScrW() / 38.8, ScrH () / 1.24, 60, 25, Color(0, 0, 0, 255))


:snip:

(Unrelated to your current issue)
This line seems pretty useless [lua]if health > 100 then health = health end[/lua] And again, as MPan1 said, you should really use math.Clamp instead of this line [lua]if health < 0 then health = 0 end[/lua]

So this would work?



health = math.Clamp( health, 0, LocalPlayer():GetMaxHealth() )
    if health != 0 then

        surface.SetDrawColor( 255, 255, 255, 255 )
        surface.SetMaterial( red )
        surface.DrawTexturedRect( ScrW() / 11.5, ScrH () / 1.265, math.Clamp( health, 0, LocalPlayer():GetMaxHealth() ) * 3.1, 47)
    end


That’s exactly what I meant before

Also, I wish I did, but I have no clue what the problem is, so I’ll probably stop posting useless stuff now :stuck_out_tongue:

All right I am trying it right now.

[editline]16th April 2016[/editline]

Did not quite work. The bar is working fine but the number saying how much HP you have is also stuck to 100. http://steamcommunity.com/sharedfiles/filedetails/?id=666452021

[editline]16th April 2016[/editline]

Oh my god I am the dumbest human being alive…

[editline]16th April 2016[/editline]

So I edited this line:



draw.SimpleText(health.." HP","hpfont",ScrW() / 32,ScrH() / 1.227,Color(255,255,255,255))


to



draw.SimpleText(healthp.." HP","hpfont",ScrW() / 32,ScrH() / 1.227,Color(255,255,255,255))


created a variable:



local healthp = LocalPlayer():Health() or 0


and it worked sorry for wasting your time gys :frowning: