I’ve created a custom player class for my UEFN project that is supposed to store class information for a player (currently this is just max shields). It looks like this:
using { /Verse.org/Simulation }
using {/Verse.org/Assets}
using { /Fortnite.com/Devices }
using {/Fortnite.com/Game}
using {/Fortnite.com/UI}
using { /Fortnite.com/Characters }
using {/UnrealEngine.com/Temporary/UI}
# Custom Class to Manage Players
custom_player := class<unique>():
Player : player
var PlayerCanvas : canvas = canvas{}
var CurrentElims<private> : int = 0
var CurrentClass<private> : string = "None"
var MaxShield<private> : float = 100.0
# Method to Create Canvas and Display on Screen
SetUI(NewCanvas : canvas) : void=
set PlayerCanvas = NewCanvas
if (PlayerUI := GetPlayerUI[Player]):
PlayerUI.AddWidget(PlayerCanvas, player_ui_slot{InputMode := ui_input_mode.All})
# Method to Remove Canvas from Screen and Update Class
SetCurrentClass(NewClass : string) : void=
if (PlayerUI := GetPlayerUI[Player]):
PlayerUI.RemoveWidget(PlayerCanvas)
set CurrentClass = NewClass
if:
Agent := agent[Player]
Fort := Agent.GetFortCharacter[]
then:
# Update Max Shield
if (NewClass = "Assault" or NewClass = "Berserker"):
set MaxShield = 100.0
else if (NewClass = "Scout"):
set MaxShield = 50.0
else if (NewClass = "Heavy"):
set MaxShield = 150.0
else if (NewClass = "Technician"):
set MaxShield = 125.0
else if (NewClass = "Rogue"):
set MaxShield = 75.0
# Update Current Shield
SetCurrentShield()
# Method to Update Shield on Elimination Based on Current Class
CheckEliminated(Result : elimination_result): void=
if:
DeadAgent := Result.EliminatedCharacter.GetAgent[]
ActualAgent := agent[Player]
then:
if (DeadAgent = ActualAgent):
SetCurrentShield()
# Method to Set Current Shield
SetCurrentShield() : void=
if:
Agent := agent[Player]
Fort := Agent.GetFortCharacter[]
then:
Fort.SetMaxShield(MaxShield)
Fort.SetShield(MaxShield)
Init():void=
if:
Agent := agent[Player]
Fort := Agent.GetFortCharacter[]
then:
# Set Up Listener for Updating Max Shields
Fort.EliminatedEvent().Subscribe(CheckEliminated)
The custom player instances are initialized and stored in a map in the game manager class. One instance of the class should exist for each player as defined by the spawners:
# Subscribe all Spawners to the OnPlayerSpawned event
InitSpawners():void=
Spawners := GetCreativeObjectsWithTag(spawner{})
for (Obj : Spawners):
if (Spawner := player_spawner_device[Obj]):
Spawner.SpawnedEvent.Subscribe(OnPlayerSpawned)
# Create Class UI for Player on Button Press
HandleButtonPress( Agent : agent) : void =
if:
Player := player[Agent]
CustomPlayer := CustomPlayers[Player]
then:
set PlayerCanvas = CreateUI()
CustomPlayer.SetUI(PlayerCanvas)
# Initialize Player with Custom Class on Spawn
OnPlayerSpawned(Agent : agent):void=
if (Player := player[Agent]):
InitPlayer(Player)
InitPlayer(Player : player):void=
if:
Agent := agent[Player]
Fort := Agent.GetFortCharacter[]
then:
# Initialize New Player Spawns with Custom Player CLass
if (Existing := CustomPlayers[Player]){}
else:
CustomPlayer := custom_player{Player := Player}
CustomPlayer.Init()
if (set CustomPlayers[Player] = CustomPlayer) {}
Currently, I have it so different buttons call the CustomPlayer.SetCurrentClass() with a string parameter like “Assault”. It properly works in changing the max shield at first. However, if the player dies, during their death animation you can see the current shield be refreshed to the proper value (probably the work of CheckEliminated method), but when they respawn they are back to the default 100 shields.
Note: I added a Print() statement in the initplayer() method to check to see if when the player dies, it creates a new custom player instance for them, but it doesn’t. How can I get it so that the max shield is remembered between respawns for this custom_player class instance?