This step shows how to assign a weapon to a player at the start of the game.
When a player spawns for the first time, grant them the first weapon in the WeaponGranters array. To do this, you’ll need to subscribe functions to each player spawn pad. See the Coding Device Interactions page for more details on subscribable events.
Follow these steps to subscribe to player spawn events and to assign their first weapon.
Add a new method
OnPlayerSpawn()to theteam_elimination_gameclass. This method makes sure that you assign the player the right weapon when they spawn for the first time, and when they respawn.VerseOnPlayerSpawn(InPlayer : agent) : void = Print("A Player just spawned!")To subscribe the functions you just set up to their associated events, you need to add new code to
OnBegin(). Create aforloop to subscribe to each player spawner’sSpawnedEventusingOnPlayerSpawn.VerseOnBegin<override>()<suspends> : void = # Get all the players set Teams = GetPlayspace().GetTeamCollection().GetTeams() set EliminationsToEndGame = WeaponGranters.Length Print("Number of eliminations to end game is {EliminationsToEndGame}") for (Spawner : PlayerSpawners): Spawner.SpawnedEvent.Subscribe(OnPlayerSpawn) # Subscribe to each player spawn padTo grant players the first weapon, you need to access the first item granter in the
WeaponGrantersarray. You’ll do this through a new method.Add a new method
GrantWeaponto theteam_elimination_gameclass. This method takes an agentoptionand assigns them a weapon based on the providedWeaponTier.VerseGrantWeapon(InPlayer : ?agent, WeaponTier : int) : void= Print("Granting Player a weapon of Tier {WeaponTier}")Inside
GrantWeapon, access the appropriate item granter from theWeaponGrantersarray usingWeaponTieras the index. Access the value insideInPlayerand store it in a variableGrantedPlayer.VerseGrantWeapon(InPlayer : ?agent, WeaponTier : int) : void= Print("Granting Player a weapon of Tier {WeaponTier}") if(ItemGranter := WeaponGranters[WeaponTier], GrantedPlayer := InPlayer?):Grant the player the appropriate weapon using
ItemGranter.GrantItem. You can verify which weapon was granted by logging theWeaponTier. YourGrantWeaponcode should look like the code below.VerseGrantWeapon(InPlayer : ?agent, WeaponTier : int) : void= Print("Granting Player a weapon of Tier {WeaponTier}") if(ItemGranter := WeaponGranters[WeaponTier], GrantedPlayer := InPlayer?): ItemGranter.GrantItem(GrantedPlayer)
Modify
OnPlayerSpawnto callGrantWeapon. When a player spawns, initialize an integerWeaponTierto zero to reflect the index of the first item granter in theWeaponGrantersarray, then callGrantWeaponpassingWeaponTierand a reference to the player. YourOnPlayerSpawncode should look like the code below.VerseOnPlayerSpawn(InPlayer : agent) : void= Print("A player just spawned!") WeaponTier : int = 0 GrantWeapon(option{InPlayer}, WeaponTier) Print("Spawned Player was granted a gun of tier {WeaponTier}")Save the script in Visual Studio Code, compile it, and click Launch Session in the UEFN toolbar to playtest the level. When you playtest your level, you should spawn with the first weapon in the
WeaponGrantersarray. Verify this behavior with the log.
Next Step
In the next step of this tutorial, you’ll learn how to track players using maps and how to populate those maps when the game starts.