i currently trying to get the last Person alive and trying to avoid a loop if there is only 1 Person in the Game.
I got something like this to cound all Players and Players with more than 0.0 Hp.
# Looks for Player with more than 0.0 HP and counts the Total Player Amount
CheckAlive() : void=
AllPlayers := GetPlayspace().GetPlayers()
for (Agent : AllPlayers, Player := player[Agent], FortCharacter := Player.GetFortCharacter[]):
if(set PlayerMap[Player] = 0):
set TotalPlayers += 1
if(PlayerHealth := FortCharacter.GetHealth() > 0.0):
set PlayerAlive += 1
But i need the Agent drom the Last Player and i don’t know exactly without doing that loop again how i get him.
So I’m not a big fan of how structured your code is, your CheckAlive() function returns void but actually writes data, maybe it should return the count of players alive ? or a logic telling if wether or not there’s only one player left ?
Given this, I cannot “repair” your code, so I’m gonna have to edit it a bit :
# Returns true when there's one player (or less) alive
CheckOneAlive() : logic =
var Count : int = 0
for (Player : GetPlayspace().GetPlayers(), FortCharacter := Player.GetFortCharacter[], FortCharacter.IsActive[]):
set Count += 1
return if(Count <= 1) then true else false
Also, a few notes regarding your code :
GetPlayspace().GetPlayers() already returns an array of player
GetFortCharacter[] doesn’t need to be called on a player, an agent will work too, as the signature tells
Thx, i actually just found IsActive just today and tryed to implement it.
The Question is for me still how do i know who is the Last Player alive to trigger the EndGame and what can i do with true or false?
I mean i could run it in a Loop (tryed that already) and count how many Players are Active and if there is only 1 Active activate the EndGame Device.
But if that is the Case we need to Filter out if there is only 1 Player in the Lobby that the EndGame has to be triggered without a Agent.
I think i almost got it but i cann’t bring it to Code fore some Reason.
Woops, sorry, got lost in my code, you’d do this instead :
# Returns true when there's one player (or less) alive
CheckOneAlive() : tuple(logic, ?player) =
var Count : int = 0
var LastIteratedPlayer : ?player = false
for (Player : GetPlayspace().GetPlayers(), FortCharacter := Player.GetFortCharacter[], FortCharacter.IsActive[]):
set Count += 1
set LastIteratedPlayer = option{Player}
return if(Count <= 1) then (true, LastIteratedPlayer) else (false, LastIteratedPlayer)
Then in your function, you’d call :
Result := CheckOneAlive()
IsCountUnderOne := Result(0)
FoundPlayer := Result(1)
if(IsCountUnderOne?):
if(_FoundPlayer := FoundPlayer?):
# Do your logic with the actual last player alive (_FoundPlayer)
else:
# There might be no more player alive ?
There might be cleaner way to do this with multiple functions, but this should work, I hope !
The CheckOneAlive() Function/Methode needs to be executed in a Loop right?
Because nothing of the Code you wrote here will be executed mid Game only at the Start of eatch Round.
Actually you gave this function so I figured out that you was already calling it from somewhere
To answer your question, you either can use a loop in a suspends function or use events that trigger on characters dying, the team_settings_and_inventory_device has one
Yes, but i was not completle shure if it would make sense to just loop it.
Just for performance Resons i better should do it with the Teamsetting Device i guess.
Hey, the player is the _FoundPlayer I gave you in the other part of code.
You should not need to edit the CheckOneAlive() function, I already set it up for you.
In my case I use the Playspace PlayerRemovedEvent to know when a fort_character dies (which doesn’t make much sense because players are not characters but it works)
OnPlayerLeft(Player: player) : void =
EndRoundIfOneAlive()
EndRoundIfOneAlive() : void =
Result := CheckOneAlive()
IsCountUnderOne := Result(0)
FoundPlayer := Result(1)
if(IsCountUnderOne?):
if(_FoundPlayer := FoundPlayer?):
# TODO: Use _FoundPlayer to end the game
else:
# There might be no more players alive, you might want to end the game without passing the agent as the winner
So here you’ll be able to end even if there’s no one alive, the end_game_device doesn’t seem to let you do that, so maybe the team_settings_and_inventory_device.EndRound() can work better. This is what I use personally. Good luck !