Hi guys,
Just creating the end game screen for my gamemode, and I want to give players some feedback yada yada yada.
But I've come acropper and need some advice/guidance, maybe a little help with some code.
I want to show the player with the most HP left at the end of a game.
I don't mind if you just want to help with one of these things. Anything would be great.
[code]
--I know for the player with the most HP I will need to for loop through all the players
for _, pl in pairs(player.GetAll()) do
--Something to get the player with the most health?
end
--I will need a var to store it in right?
local endgamehealth = some thing calculated in the for loop above?
So I can then do some thing like
draw.SimpleText( " "..Players name who has the most HP.." Has survived with "..endgamehealth .."HP! " , "ZSHUDFont", w * 0.5, h * 0.6, Color( 255,255,255,255), TEXT_ALIGN_CENTER )
[/code]
This is the first time in 12 months I've asked for helping with Lua coding. It seems that I'm in a right rut and no one I know seems to have any indication. Nor does google.
If anyone could help me out that would be greatly appreciated.
Thanks in advance,
Duby
You simply make a DPanel, with no mouse input enabled, show it, and then print the player there, and then call with timer.Simple "dpanel:Remove()".
[QUOTE=whitestar;50075569]You simply make a DPanel, with no mouse input enabled, show it, and then print the player there, and then call with timer.Simple "dpanel:Remove()".[/QUOTE]
I'm not having issues with the D-Panel. I'm having issues with the code I've noted above with the comments on it.
I've got all my graphics made and displaying. I've just cut all that out as it's easier for you guys to see the core issue I'm having
loop through a table, add the player name + health value in it, and then check which one is the biggest with > and <
[QUOTE=whitestar;50075699]loop through a table, add the player name + health value in it, and then check which one is the biggest with > and <[/QUOTE]
Yea I know I need to do that, I'm just a little stuck on how the syntax should look. Not asking for a spoon feed. But maybe a nibble would be nice haha
[code]
local plys = {}
for k, v in pairs(player.GetAll()) do
plys[k] = {
Name = v.Nick(),
Health = v.Health()
}
end
table.SortByMember(plys, "Health")
print(plys[1])
[/code]
Untested.
This returns a table of the healthiest players, to avoid conflicting players with the same health.
[CODE]
local function GetHealthiestPlayers()
local tblWinners = {} -- initalize a list of winners
tblWinners.intMin = 0 -- set minimum requirement for health
for _, ply in pairs( player.GetAll() ) do
if ply:Health() >= tblWinners.intMin then
if ply:Health() > tblWinners.intMin then -- recheck the table when player's health is larger
for intSteamID, lply in pairs( tblWinners ) do
if intSteamID ~= "intMin" and lply:Health() < ply:Health() then
tblWinners[ intSteamID ] = nil
end
end
end
tblWinners.intMin = ply:Health()
tblWinners[ ply:SteamID() ] = ply
end
end
tblWinners.intMin = nil -- We don't need this data
return tblWinners
end
[/CODE]
[QUOTE=Potatofactory;50075956]This returns a table of the healthiest players, to avoid conflicting players with the same health.
[CODE]
local function GetHealthiestPlayers()
local tblWinners = {} -- initalize a list of winners
tblWinners.intMin = 0 -- set minimum requirement for health
for _, ply in pairs( player.GetAll() ) do
if ply:Health() >= tblWinners.intMin then
if ply:Health() > tblWinners.intMin then -- recheck the table when player's health is larger
for intSteamID, lply in pairs(tblWinners) do
if intSteamID ~= "intMin" and lply:Health() < ply:Health() then
tblWinners[intSteamID] = nil
end
end
end
tblWinners.intMin = ply:Health()
tblWinners[ ply:SteamID() ] = ply
end
end
tblWinners.intMin = nil -- We don't need this data
return tblWinners
end
PrintTable(GetHealthiestPlayers())
[/CODE][/QUOTE]
Thats.. way more complicated? Whats better than the easy small code?
This table only contains the healthiest players with the same amount of health
[QUOTE=Potatofactory;50075983]This table only contains the healthiest players with the same amount of health[/QUOTE]
Mine should have the first one with the highest health amount automaticly :o
Yeah, however if there are more than 1 player containing the same exact health, then that would mean that more than 1 slot of data would have the highest players.
In order to include all the healthiest players we need to only include those and exclude the others.
For instance,
[CODE]
local plys = {}
local winningplys = {}
for k, v in pairs(player.GetAll()) do
plys[k] = {
Name = v.Nick(),
Health = v.Health()
}
end
for k, v in pairs(player.GetAll()) do
if v:Health() >= plys[1]:Health() then
winningplys[k] = v
end
end
PrintTable(winningplys)
[/CODE]
[QUOTE=Potatofactory;50076023]Yeah, however if there are more than 1 player containing the same exact health, then that would mean that more than 1 slot of data would have the highest players.
In order to include all the healthiest players we need to only include those and exclude the others.
For instance,
[CODE]
local plys = {}
local winningplys = {}
for k, v in pairs(player.GetAll()) do
plys[k] = {
Name = v.Nick(),
Health = v.Health()
}
end
for k, v in pairs(player.GetAll()) do
if v:Health() >= plys[1]:Health() then
winningplys[k] = v
end
end
PrintTable(winningplys)
[/CODE][/QUOTE]
the winning plys would contain the same value(s) as the player.GetAll table, and he only wants "THE" player, not "THE" player[B]s[/B] with the highest value of health, so my solution would be the simplest.
Hmmm I see, so you would do a section for loop to check for those with the same amounts of health and to exclude one of them.
Thanks guys this really helped me out, and I appreciate it. I think I can manage this and produce something cool.
If I get it working I'll link a pic for you guys to see my project ^^
[QUOTE=Potatofactory;50076023]:snip:[/QUOTE]
I get what you're doing, I really do, but this is not the place to do it. You are overcomplicating an otherwise simple problem. Yes, your solution is more inclusive and in some ways more correct, but it should only be mentioned as an afterthought, not presented as the right way to do this (even if it is). You'll just end up confusing people.
Unrelatedly, your code is a bit overcomplicated too. I get that too, actually - the first time I code something it ends up bloated and terrible. But the second time it's much cleaner, more more optimized, much more readable... That's the kind of code you should be putting up on forums ;)
Here's my take on your approach, but bear in mind I'm on mobile so I may miss some errors:
[Lua]local plys = player.GetAll()
local maxhp = 0
for _, ply in pairs(plys) do
maxhp = math.max( maxhp, ply:Health() )
end
local winners = {}
for _, ply in pairs(plys) do
if ply:Health() == maxhp then table.insert(winners, ply) end
end
return winners[/Lua]
Possibly more optimized but more confusing:
[Lua]local plys = player.GetAll()
local maxhp = 0
local hps = {}
for _, ply in pairs(plys) do
local plyhp = ply:Health()
maxhp = math.max( maxhp, plyhp )
hps[plyhp] = hps[plyhp] or {}
table.insert(hps[plyhp], ply)
end
return hps[maxhp][/Lua]
Lastly, if you're helping people out it's usually best to throw your own conventions aside (intMin) and use more commonly accepted conventions (e.g. minhealth).
[code]
local plys = player.GetAll()
table.sort(plys, function(a, b) return a:Health() > b:Health() end)
local plyWithMaxHealth = plys[1]
[/code]
:science101:
I' pm from mobile too. Probably needs check for player alive status (in the sorting function)
Sorry, you need to Log In to post a reply to this thread.