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:
[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
[/code]
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?
[url]http://steamcommunity.com/sharedfiles/filedetails/?id=618418391[/url]
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
[code]if health > 100 then health = health end[/code]
to this
[code]if health > 100 then health = 100 end[/code]
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:
[CODE]
surface.DrawTexturedRect( ScrW() / 11.5, ScrH () / 1.265, health * 3.1, 47)
[/CODE]
To this:
[CODE]
surface.DrawTexturedRect( ScrW() / 11.5, ScrH () / 1.265, math.Clamp( health, 0, LocalPlayer():GetMaxHealth() ) * 3.1, 47)
[/CODE]
[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:
[CODE]
if health > 100 then health = health end
if health < 0 then health = 0 end
[/CODE]
To something like this:
[CODE]
health = math.Clamp( health, 0, LocalPlayer():GetMaxHealth() ) -- a player's max health isn't always over 100
[/CODE]
Now I am getting this lua error in the console:
[code]
[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
[/code]
Where are you setting the red variable as an IMaterial?
Line 80:
[code]
surface.SetMaterial( ourMat )
[/code]
anything to do with ourMat
[code]
local ourMat = Material( "vgui/saber.png")
[/code]
But... in the code you posted you did
[CODE]
surface.SetMaterial( red )
[/CODE]
?
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"
[code]
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
[/code]
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]
[code]
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))
[/code]
: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?
[code]
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
[/code]
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 :P
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. [url]http://steamcommunity.com/sharedfiles/filedetails/?id=666452021[/url]
[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:
[code]
draw.SimpleText(health.." HP","hpfont",ScrW() / 32,ScrH() / 1.227,Color(255,255,255,255))
[/code]
to
[code]
draw.SimpleText(healthp.." HP","hpfont",ScrW() / 32,ScrH() / 1.227,Color(255,255,255,255))
[/code]
created a variable:
[code]
local healthp = LocalPlayer():Health() or 0
[/code]
and it worked sorry for wasting your time gys :(
Sorry, you need to Log In to post a reply to this thread.