Forum

> > CS2D > Scripts > Health HUD
Forums overviewCS2D overview Scripts overviewLog in to reply

English Health HUD

6 replies
To the start Previous 1 Next To the start

old Health HUD

Dousea
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
local screenx, screeny = 640, 480

local health_x, health_y = 98 + 6, screeny - (6 + 4)

function drawhudhealth (id)
	if (not PLAYER.hud[id].health_bar) then
		PLAYER.hud[id].health_bar = image (DIRECTORY.gfx .. "bar.png", health_x, health_y, 2, id)
	end
	
	if (not PLAYER.hud[id].health_overlay) then
		PLAYER.hud[id].health_overlay = image (DIRECTORY.gfx .. "overlay.png", health_x, health_y, 2, id)
	end
	
	updatehudhealth (id)
end

function updatehudhealth (id)
	local scale = player (id, "health") / player (id, "maxhealth")
	
	local green = scale * 255
	local red = 255 - green
	local blue = 0
	
	imagescale (PLAYER.hud[id].health_bar, scale, 1)
	imagecolor (PLAYER.hud[id].health_bar, red, green, blue)
	imagepos (PLAYER.hud[id].health_bar, health_x * scale, health_y, 0)
end


-- HOOKS
function hud_spawn (id)
	drawhudhealth (id)
end

function hud_die (victim)
	freeimage (PLAYER.hud[victim].health_bar)
	freeimage (PLAYER.hud[victim].health_overlay)
end

function hud_always ()
	for key, id in pairs (player (0, "tableliving")) do
		updatehudhealth (id)
	end
end

local hooks = {"spawn", "die", "always", "collect"}

for key, hook in pairs (hooks) do
	addhook (hook, "hud_" .. hook)
end
So, what's exactly wrong here? I just do the normal things in this script. Create image for certain player according to their ID, update when hit, delete when die. Well, the problem is the update is not really functioning well, not executed. Because the "health_bar" is not scaled according to my health and the color as well. Not really understand the issue. Help?

old Re: Health HUD

Rainoth
Moderator Off Offline

Quote
Not really sure cause I'm a bit too lazy (sorry ._.) to look at this but after a quick check I can make a guess.

Might be that you can't change color because your scale screws the values. Like...
HP : 67/100
> scale = 0.67

red = 255*0.67 = 170.85 and maybe cs2d says "no" to that.

Try it. Might work. At least I know that color needs to be written with 0s like 058 or 007 or w/e.

EDIT : oh and may I ask why your health_x/y are calculations instead of writing a number (at least health_x cause it's not influenced by a variable) ?

old Re: Health HUD

Dousea
User Off Offline

Quote
user Rainoth has written
Not really sure cause I'm a bit too lazy (sorry ._.) to look at this but after a quick check I can make a guess.

Might be that you can't change color because your scale screws the values. Like...
HP : 67/100
> scale = 0.67

red = 255*0.67 = 170.85 and maybe cs2d says "no" to that.

Try it. Might work. At least I know that color needs to be written with 0s like 058 or 007 or w/e.

EDIT : oh and may I ask why your health_x/y are calculations instead of writing a number (at least health_x cause it's not influenced by a variable) ?

Should I make a number to be not an integer by using math.floor or math.ceil? And how to make the color with 0s behind them? Well about the dem health_x/y, I hate calculating with my own head, a bit lazy.

old Re: Health HUD

Rainoth
Moderator Off Offline

Quote
1. Yes
2.
1
2
3
4
local rnd1 = math.random(0,255)
local rnd2 = math.random(0,255)
local rnd3 = math.random(0,255)
local color =("0"):rep(3-#rnd1)..rnd1..("0"):rep(3-#rnd2)..rnd2..("0"):rep(3-#rnd3)..rnd3
3. how hard is it to calculate 98+6 ?

old Re: Health HUD

Rainoth
Moderator Off Offline

Quote
Works in my way too For me that is. It's every scripters personal choice how to write stuff.

old Re: Health HUD

KimKat
GAME BANNED Off Offline

Quote
user MikuAuahDark has written
@user Rainoth:
2.
1
local color=string.format("%03d%03d%03d",math.random(0,255),math.random(0,255),math.random(0,255))
1
math.randomseed(os.time());function r()return math.random(0,255)end;local c=string.format("%03d%03d%03d",r(),r(),r());print(c) -- Result: (Random RGB value) in example: 149027110
This will work flawlessly.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview