When a player joins a game in progress, you don’t want them to have to wait as a spectator for a new round to start. Ideally, a new player should be able to spawn and start playing immediately. To handle this, you need to add the new player to TeamMap, subscribe to their elimination event, and grant them their first weapon.
Follow these steps to grant players weapons and subscribe to their player events when they join a game in progress.
Add a new method
OnPlayerAddedto theteam_elimination_gameclass. This method promotes any player that joins the game in progress to the lowest weapon tier of another player on the team.VerseOnPlayerAdded(InPlayer : player) : void = Print("A New Player Joined!")Get the team for the new player using
GetTeam[]and store it in a local variableTeam. Retrieve theFortCharacterfor that player and save it in a variableFortCharacter.VerseOnPlayerAdded(InPlayer : player) : void = Print("A New Player Joined!") if: Team := GetPlayspace().GetTeamCollection().GetTeam[InPlayer] FortCharacter := InPlayer.GetFortCharacter[]To assign the new player to the
TeamMapyou need to access theplayer_mapassociated with the new player'sTeam. Get theplayer_mapassociated with the new player and store it in a local variablePlayerMap.VerseOnPlayerAdded(InPlayer : player) : void = Print("A New Player Joined!") if: Team := GetPlayspace().GetTeamCollection().GetTeam[InPlayer] FortCharacter := InPlayerr.GetFortCharacter[] var PlayerMap : player_map = TeamMap[Team]Set the player's score in
PlayerMapto 0, then updateTeamMapwith your local variablePlayerMap.VerseOnPlayerAdded(InPlayer : player) : void = Print("A New Player Joined!") if: Team := GetPlayspace().GetTeamCollection().GetTeam[InPlayer] FortCharacter := InPlayer.GetFortCharacter[] var PlayerMap : player_map = TeamMap[Team] set PlayerMap[InPlayer] = 0 set TeamMap[Team] = PlayerMapGrant the player their first weapon through a call to
GrantWeapon, and subscribe to the new player’s elimination event. YourOnPlayerAddedcode should look like the code below.VerseOnPlayerAdded(InPlayer : player) : void = Print("A New Player Joined!") if: Team := GetPlayspace().GetTeamCollection().GetTeam[InPlayer] FortCharacter := InPlayer.GetFortCharacter[] var PlayerMap : player_map = TeamMap[Team] set PlayerMap[InPlayer] = 0 set TeamMap[Team] = PlayerMap then: GrantWeapon(option{InPlayer}, 0)In
OnBegin, subscribe to the playspacePlayerAddedEventusingOnPlayerAdded. Now a player joining the game in progress will triggerOnPlayerAdded. AsPlayerAddedEventis an event triggered by the playspace itself, you don’t need a particular device to subscribe to it.VerseOnBegin<override>()<suspends> : void = set Teams = GetPlayspace().GetTeamCollection().GetTeams() set EliminationsToEndGame = WeaponGranters.Length Print("Number of eliminations to end game is {EliminationsToEndGame}") Print("Beginning to assign players") PopulateTeamsAndPlayers() for (Spawner : PlayerSpawners): Spawner.SpawnedEvent.Subscribe(OnPlayerSpawn) # Subscribe to each player spawn pad for (Sentry : Sentries): Sentry.EliminatedEvent.Subscribe(TestPlayerEliminated) # Subscribe to each Sentry
Save the script in Visual Studio Code, compile it, and click Launch Session in the UEFN toolbar to playtest the level. When a new player joins a game in progress, they should spawn with the first weapon. When they or a teammate score an elimination, they should be promoted to the next weapon tier.
Next Step
In the next step of this tutorial, you’ll learn how to test eliminations when playing single player using sentries.