How can you store update the player list if a player joins the game?

Hi!

I have this code for a Gun Game:

 InitPlayers<public>(Agent : ?agent) : void =
        AllPlayers := GetPlayspace().GetPlayers()
        for (Player : AllPlayers, FortCharacter := Player.GetFortCharacter[]):
            if (set PlayerMap[Player] = 0, WeaponTier := PlayerMap[Player]):
                Print("Storing players")
                FortCharacter.EliminatedEvent().Subscribe(OnPlayerEliminated)
                Print("Respawning Players, disabling old Spawners")
                DisableLobbySpawns()
                EnableGunGameSpawns()
                #Eliminate player to respawn in the new location
                FortCharacter.Damage(9999.00)
                Print("Trying to GrantWeapon()")
                GrantWeapon(Player, WeaponTier)

The Gun Game is working perfectly if all the players are in the pre-lobby, but there is a problem because the players are being stored and subscribing to the event at the begining of the game, so if a player joins in the middle of a game will be not be stored.

There is any function to check if a player joins into the game?

Self.GetPlayspace().PlayerAddedEvent().Subscribe(myfunction)

4 Likes

As mentioned, you need to use events for this. Events are how you make your code react to things happening in the game, I highly suggest reading the Verse docs and some of the Verse tutorials for learning how to use them.

For this particular example - keeping track of all players that are added/removed - this particular UEFN tutorial will be of help: Team Elimination Game

EDIT: Apologies if patronizing, you clearly already know how to code - maybe all you needed to know was that Verse/UEFN is Event-based :slight_smile: if so, do make yourself familiar with all Events - since they’re a pretty major entrypoint for doing your Verse stuff it’s good to keep aware of them. There aren’t so many Events right now but that number will surely grow.

1 Like

Yes, I was trying to find the function that JoeriDukker says <3

I’m curious how you did your “OnPlayerEliminated”. My gun game unintentionally cycles the player to the next weapon if the player eliminates themselves (fall damage for example without anyone else having already damaged them)! By checking if the eliminator is a fortcharacter I thought it would only promote the player to the next level when another actual player was the eliminator.

OnPlayerEliminated(Result : elimination_result) : void=
Eliminator := Result.EliminatingCharacter
If (FortCharacter := Eliminator?, EliminatingAgent := FortCharacter.GetAgent[]):
PromotePlayer(FortCharacter, EliminatingAgent)

I just check if the eliminator is the same as the eliminated agent. It works in my gun game

 OnPlayerEliminated(Result : elimination_result) : void =
        Eliminator := Result.EliminatingCharacter
        Eliminated := Result.EliminatedCharacter

        if (EliminatingFortCharacter := Eliminator?, EliminatingAgent := EliminatingFortCharacter.GetAgent[]):
            if (EliminatedFortCharacter := Eliminated, EliminatedAgent := EliminatedFortCharacter.GetAgent[]):
                    if(EliminatedAgent<>EliminatingAgent):
                        Print("We need to promote player")
                        PromotePlayer(EliminatingAgent)
                        HealPlayer(EliminatingFortCharacter)
                        Print("The Player has been Promoted")
                    else:
                        Print("Error promoting the player: Eliminator and Elimitated are the SAME PLAYER")

@b3rtl3d

1 Like