,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
Scripts
Hudtxt that shows HP
Hudtxt that shows HP
,
function showhealth(id)
local player(id,'health') = hp
parse('hudtxt2 ' .. id .. ' 1 "Health: '.. hp ..'" 100 200')
end
G3tWr3ck3d: It will not work directly with
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
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.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
mp_hud is way to go. Go through the console and type the following input:mp_hud 126
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
spawn,
hit,
parse, and
leave. There're more complicated ones but you don't need it anyway.
Dousea: why he would do so much work when he can simply use always hook and get the exact value of the HP every moment while ingame?
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.
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
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.addhook("spawn","_s")
function _s(id)
	 parse('hudtxt2 ' .. id .. ' 1 "\169255255255Health: ' .. player(id,"health") .. '" 100 200')
end
addhook("hit","_h")
function _h(id)
	 parse('hudtxt2 ' .. id .. ' 1 "\169255255255Health: ' .. player(id,"health") .. '" 100 200')
end
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?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
DC could somehow optimise the cs2d hooks usage so it will create less lag that would be great
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,
always is overkill and you should consider using
ms100, as there is no reliable way to track a dispenser giving health or money to people.
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
AtomKuh: yep, well just add player(id,'armor') instead of player(id,'health')
G3tWr3ck3d has written
AtomKuh: yep, well just add player(id,'armor') instead of player(id,'health')
always is perfectly ok as long as you only update the hudtxt when HP values change.
player) functions are much more expensive than functions in the lua environment.
AtomKuh: That's because armors have values above 200 for them.
G3tWr3ck3d: You can use a single function on multiple hooks (if they're sharing the same first and probably second parameters).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
AtomKuh: You can use
itemtype to get the armor's name.
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
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.
. UDP uses 32 bits in Network Packet. There is also ping that slows downloading it and other packets outside script.
§2.4 - Use tags sparingly and only when they add value