So I have a lot of repeat type of code, example below, and I'm just wondering if there is any way to simplify it
This for some reason really bothers me, and if there is anyway to make it into one function please let me know
Code:
[CODE]
if plyHealth != 0 then
draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, Color(255,255,255,50), TEXT_ALIGN_CENTER)
end
if plyHealth <= 75 then
draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, Color(255,112,112,255), TEXT_ALIGN_CENTER)
end
if plyHealth <= 50 then
draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, Color(255,91,91,255), TEXT_ALIGN_CENTER)
end
if plyHealth <= 25 then
draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, Color(255,58,58,255), TEXT_ALIGN_CENTER)
end
if plyHealth <= 10 then
draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, Color(221,17,17,255), TEXT_ALIGN_CENTER)
end
[/CODE]
[lua]
local colors = {Color(221,17,17,255), Color(255,58,58,255), Color(255,91,91,255), Color(255,112,112,255), Color(255,255,255,50)}
local colorToUse = colors[(plyHealth > 10 and math.floor((plyHealth)/25)+2 or 1)]
draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, colorToUse, TEXT_ALIGN_CENTER)
[/lua]
why disagree? the code will do exactly what he's doing in 3 lines
[QUOTE='[FSG] ToXiiC;52701688']So I have a lot of repeat type of code, example below, and I'm just wondering if there is any way to simplify it
This for some reason really bothers me, and if there is anyway to make it into one function please let me know
Code:
[CODE]
if plyHealth != 0 then
draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, Color(255,255,255,50), TEXT_ALIGN_CENTER)
end
if plyHealth <= 75 then
draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, Color(255,112,112,255), TEXT_ALIGN_CENTER)
end
if plyHealth <= 50 then
draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, Color(255,91,91,255), TEXT_ALIGN_CENTER)
end
if plyHealth <= 25 then
draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, Color(255,58,58,255), TEXT_ALIGN_CENTER)
end
if plyHealth <= 10 then
draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, Color(221,17,17,255), TEXT_ALIGN_CENTER)
end
[/CODE][/QUOTE]
Why not do something like this:
[CODE]local boxColor = Color(255,255,255,50)
if plyHealth <= 10
boxColor = Color(221,17,17,255)
elseif plyHealth <= 25 then
boxColor = Color(255,58,58,255)
elseif plyHealth <= 50 then
boxColor = Color(255,91,91,255)
elseif plyHealth <= 75 then
boxColor = Color(255,112,112,255)
end
draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, boxColor, TEXT_ALIGN_CENTER)[/CODE]
FYI, your code is drawing multiple boxes but you can't see it because the dimensions are the same. If the [B]plyHealth [/B]variable is equal to [B]5 [/B]for example, every single one of those conditions is being met (5 != 0, 5 <= 75, etc). Are you purposefully wanting to draw the first transparent box with the others?
[QUOTE=Cruben;52701839]Why not do something like this:
[CODE]local boxColor = Color(255,255,255,50)
if plyHealth <= 10
boxColor = Color(221,17,17,255)
elseif plyHealth <= 25 then
boxColor = Color(255,58,58,255)
elseif plyHealth <= 50 then
boxColor = Color(255,91,91,255)
elseif plyHealth <= 75 then
boxColor = Color(255,112,112,255)
end
draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, boxColor, TEXT_ALIGN_CENTER)[/CODE]
FYI, your code is drawing multiple boxes but you can't see it because the dimensions are the same. If the [B]plyHealth [/B]variable is equal to [B]5 [/B]for example, every single one of those conditions is being met (5 != 0, 5 <= 75, etc). Are you purposefully wanting to draw the first transparent box with the others?[/QUOTE]
Works, thanks m8
[QUOTE=Cruben;52701839]Why not do something like this:
[CODE]local boxColor = Color(255,255,255,50)
if plyHealth <= 10
boxColor = Color(221,17,17,255)
elseif plyHealth <= 25 then
boxColor = Color(255,58,58,255)
elseif plyHealth <= 50 then
boxColor = Color(255,91,91,255)
elseif plyHealth <= 75 then
boxColor = Color(255,112,112,255)
end
draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, boxColor, TEXT_ALIGN_CENTER)[/CODE]
FYI, your code is drawing multiple boxes but you can't see it because the dimensions are the same. If the [B]plyHealth [/B]variable is equal to [B]5 [/B]for example, every single one of those conditions is being met (5 != 0, 5 <= 75, etc). Are you purposefully wanting to draw the first transparent box with the others?[/QUOTE]
That's much messier imo. You can do much cleaner logic with a table of colours. It also prevents having to create Color objects every time.
[code]
if health <= maxHealth then
local i = maxHealth - health
draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, Color( 73 + i, 255 - i, 73, 200 ), TEXT_ALIGN_CENTER )
end
[/code]
I think this way is much smoother and looks nicer imo. If you want it to change much more red much faster, you can put the i's in a bracket and times it by a number.
Example:
[code]
if health <= maxHealth then
local i = maxHealth - health
draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, Color( 73 + ( i*1.5 ), 255 - ( i*2 ), 73, 200 ), TEXT_ALIGN_CENTER )
end
[/code]