Forum

> > CS2D > Scripts > Hudtxt that shows HP
Forums overviewCS2D overview Scripts overviewLog in to reply

English Hudtxt that shows HP

22 replies
Page
To the start Previous 1 2 Next To the start

old Hudtxt that shows HP

AtomKuh
User Off Offline

Quote
Hi us ,

I'm looking for a basic script that gives every players his own hudtxt (hudtxt2) that shows his HP. I want to delete the HP display from the standard hud and for this the HP should be viewable through a hudtxt.

Thanks

old Re: Hudtxt that shows HP

G3tWr3ck3d
User Off Offline

Quote
1
2
3
4
function showhealth(id)
local player(id,'health') = hp
parse('hudtxt2 ' .. id .. ' 1 "Health: '.. hp ..'" 100 200')
end

I advice you to use it with the always hook but hit hook could also work if you use it properly

old Re: Hudtxt that shows HP

GeoB99
Moderator Off Offline

Quote
@user G3tWr3ck3d: It will not work directly with cs2d lua hook always hook if you don't define the ID argument inside the parenthesis parameter. Undefined arguments will always be labeled as nil points thus causing an issue like bad "X" argument, got nil or such. Also, the variable name should be always named at the beginning of assignment, not after. That's my opinion anyways.

• The Code

Since user G3tWr3ck3d's chunk of code triggers two errors, here's the fixed one. Note that the X and Y positions are added up randomly so it's up to you to decide where the HUD position should be placed.
1
2
3
4
5
6
7
8
9
function showhealth()
	for _, id in pairs( player(0, 'table') ) do
		local HP = player(id, 'health')
		parse('hudtxt2 ' .. id .. ' 1 "\169255255255Health: ' .. HP .. '" 100 200')
	end
end


addhook("always","showhealth")

• Hiding the standard Health HUD

To hide the health standard HUD (or any kind of it), cs2d cmd mp_hud is way to go. Go through the console and type the following input:
mp_hud 126

It'll get rid of the specific health HUD straight away.

old Re: Hudtxt that shows HP

Dousea
User Off Offline

Quote
Just a note: don't use cs2d lua hook always hook for the simple HUD text if you're planning to have a server of your script. Try to use hooks when the players' health is changed, such cs2d lua hook spawn, cs2d lua hook hit, cs2d lua hook parse, and cs2d lua hook leave. There're more complicated ones but you don't need it anyway.

old Re: Hudtxt that shows HP

Rainoth
Moderator Off Offline

Quote
@user G3tWr3ck3d: Because a player can be hit about 10 times in 30 seconds (not a fact but around that much). If you use hit hook you're calling the command 10 times in 30 seconds. With the same thing but with always hook, you're calling the command 1500 times.
It's needless computation.

Actually, most of the time you're not even getting hit more than once in 5 frames, so using ms100 here would make more sense if you do decide to use that method.

old Re: Hudtxt that shows HP

G3tWr3ck3d
User Off Offline

Quote
@user Rainoth: so how that can affect the gameplay? Also, using just the always hook means less coding seeing as he must enter through a whole process of hooks to make it work > hit,spawn,leave

old Re: Hudtxt that shows HP

Rainoth
Moderator Off Offline

Quote
@user G3tWr3ck3d: In short, it makes the computer do multiple calculations all the time when it would be enough to do just a couple at certain moments. Basically, it contributes to making the server lag. I highly doubt that just that alone will make the server laggy but if you're using a lot of entities, other scripts and so on, time based hooks (especially always and ms100) add a lot of needless lag.

If you plan to use just this script then it's okay, but it's better practice to optimize scripts if you can.

Also, you're using two hooks:
1
2
3
4
addhook("spawn","_s")
function _s(id)
	 parse('hudtxt2 ' .. id .. ' 1 "\169255255255Health: ' .. player(id,"health") .. '" 100 200')
end
and
1
2
3
4
addhook("hit","_h")
function _h(id)
	 parse('hudtxt2 ' .. id .. ' 1 "\169255255255Health: ' .. player(id,"health") .. '" 100 200')
end
So much work. I can already feel tired from all that copy-pasting

old Re: Hudtxt that shows HP

G3tWr3ck3d
User Off Offline

Quote
@user Rainoth: so lets say I want to display the money as a hudtxt2, without always hook or ms100 and so on, how do you determine the player money when the dispenser gives a player money?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
addhook("spawn","_spawn")
function _spawn(id)
    parse('hudtxt2 '..id..' 1 "Money: '..player(id,"money")..'" 100 200')
end

addhook("die","_die")
function _spawn(id)
    parse('hudtxt2 '..id.. '1'' 0 0')
end

addhook("leave","_leave")
function _leave(id)
    parse('hudtxt2 '..id.. '1'' 0 0')
end

addhook("build","_build")
function _build(id,type,x,y,mode,objectid)
	parse('hudtxt2 '..id..' 1 "Money: '..player(id,"money")..'" 100 200')
end

old Re: Hudtxt that shows HP

Rainoth
Moderator Off Offline

Quote
It won't work with dispenser because there's no hook "dispense" (on dispenser giving money). I would either use time hook (for example seconds) to check if player's money increased or define dispenser's build point as a custom object and check if player is nearby when dispenser gives money.
Both are pretty nasty ways but lua was always a "how can I workaround this with limited hooks and functions I am given" kind of thing.

old Re: Hudtxt that shows HP

G3tWr3ck3d
User Off Offline

Quote
The second hook won't give the precise value just because it has a delay of a second, the always hook is more precisely, but as you said, the always hook generates lag so in fact is better to use the second hook, if user DC could somehow optimise the cs2d hooks usage so it will create less lag that would be great

old Re: Hudtxt that shows HP

EngiN33R
Moderator Off Offline

Quote
It's not so much a question of optimisation for the cs2d lua hook always hook as it is a question of its nature. Any expensive actions that are performed every frame update will generate lag. However, the actions have to be really expensive for the server to be noticeably more laggy. In this case, cs2d lua hook always is overkill and you should consider using cs2d lua hook ms100, as there is no reliable way to track a dispenser giving health or money to people.

old Re: Hudtxt that shows HP

AtomKuh
User Off Offline

Quote
Thanks for your answers

edit: is it possible to make the same thing just with armor?
edited 1×, last 17.05.16 09:41:46 pm

old Re: Hudtxt that shows HP

G3tWr3ck3d
User Off Offline

Quote
@user EngiN33R: I don't know why cs2d does not run smoothly with big codes, but is really annoying because you script until you reach a limit and you cannot complete mods because of lag/crashes, just like weiwen's tibia, people use to play on the original servers of tibia more than the edited ones that has way more features and because it has less lags or not at all. I think that cs2d needs a slightly improvement on how cs2d and lua works, probably blitzmax does not support lua that well or its code engine is simply just not supporting big games with mods and other big stuff

@user AtomKuh: yep, well just add player(id,'armor') instead of player(id,'health')

old Re: Hudtxt that shows HP

VADemon
User Off Offline

Quote
cs2d lua hook always is perfectly ok as long as you only update the hudtxt when HP values change.

PS: You have roughly 18-19ms each frame for computations (1000/50 - (1 or 2)). If that's not enough then your code is shit and it barely has something to do with CS2D's performance. Although in general, native cs2d (e.g. cs2d cmd player) functions are much more expensive than functions in the lua environment.

old Re: Hudtxt that shows HP

Rainoth
Moderator Off Offline

Quote
@user AtomKuh: That's because armors have values above 200 for them.
201-light armor
202-armor
203-heavy armor
204 - medic
205 - super armor
206 - stealth armor

old Re: Hudtxt that shows HP

Dousea
User Off Offline

Quote
@user G3tWr3ck3d: You can use a single function on multiple hooks (if they're sharing the same first and probably second parameters).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
local addhook, parse, player = addhook, parse, player

function addhudtxt(id, attacker)
    id = attacker or id
    
    parse("hudtxt2 " .. id .. " 0 \"Health: " .. player(id, "health") .. "\" 320 320")
end

function freehudtxt(id)
    parse("hudtxt2 " .. id .. " 0 \"\" 0 0")
end

for _, hook in ipairs({"spawn", "hit"}) do
    addhook(hook, " addhudtxt")
end

for _, hook in ipairs({"die", "leave"}) do
    addhook(hook, "freehudtxt")
end

@user AtomKuh: You can use cs2d lua cmd itemtype to get the armor's name.

old Re: Hudtxt that shows HP

G3tWr3ck3d
User Off Offline

Quote
@user VADemon: computation...on a gaming computer where you can host bigger games like cod black ops 3 servers or gta 5, how is it possible that he cannot compute cs2d processes faster? Cs2d is a small game I don't understand how the game still could process stuff slower than a bigger game

old Re: Hudtxt that shows HP

Gaios
Reviewer On Online

Quote
@user G3tWr3ck3d: Every bigger game has many triggers/hooks client-sided. For example they will not calculate any animations or explosions, because of client doing it. Also collisions are calculated by server but first client calculate it, to get the collision soft and without lags. In CS2D you do script that ONLY server calculate it.

If there are 32 players with ALWAYS hook, server generate 32 bits * 32 players = 1024 bits = 1 Kb/s. I guess so . UDP uses 32 bits in Network Packet. There is also ping that slows downloading it and other packets outside script.

More >


EDIT:
Maybe it shouldn't lag, hmm?
To the start Previous 1 2 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview