How to change player properties for multiple players at once?

I originally had this code to update a health slider when a player interacted with a button which worked just fine:

UpdateHealthSlider(Message: widget_message):void =
    Playspace: fort_playspace = GetPlayspace() # Create a playspace
    AllPlayers: []player = Playspace.GetPlayers() # Get all players and put in an array
    if(FirstPlayer: player = AllPlayers[0]):
        if(PlayerRefOne: fort_character = FirstPlayer.GetFortCharacter[]):
                    TrueHealth := MyUIHealthSlider.GetValue()
                    PlayerRefOne.SetMaxHealth(TrueHealth)
                    PlayerRefOne.SetHealth(TrueHealth)
                    return 

However, I’d like all players in the game to be affected by the slider and I can’t figure out how to do it. I tried this:

UpdateHealthSlider(Message: widget_message):void =
    Playspace: fort_playspace = GetPlayspace() # Create a playspace
    AllPlayers: []player = Playspace.GetPlayers() # Get all players and put in an array
    if((FirstPlayer: player = AllPlayers[0]) or (SecondPlayer: player = AllPlayers[1])):
        if((PlayerRefOne: fort_character = FirstPlayer.GetFortCharacter[]) and (PlayerRefTwo: fort_character = SecondPlayer.GetFortCharacter[])):
                    TrueHealth := MyUIHealthSlider.GetValue()
                    PlayerRefOne.SetMaxHealth(TrueHealth)
                    PlayerRefOne.SetHealth(TrueHealth)
                    PlayerRefTwo.SetMaxHealth(TrueHealth)
                    PlayerRefTwo.SetHealth(TrueHealth)
                    return

The code won’t run without errors which is already an issue but, even if it did work, I’d have to add on lots of ineffiencient code to expand this to fit 6 players instead of 2.
Is there any other way to change the player health/shields of the whole lobby?

I can see two ways of doing this.

The first solution could be to use the class system. Using a class designer device, you can easily change the health and shield for the class itself. If everyone is then assigned to the same class, you can therefore change the health and shield for every player!

The second option would be to just use a loop. The loop would go through all of the players in Playspace.GetPlayers(), reducing the amount of code involved. I don’t have the UEFN editor with me right now, but a quick example is something like…

AllPlayers : []player = Playspace.GetPlayers()

for (Player : AllPlayers):
    if (FortCharacter := Player.GetFortCharacter[]):
        # Do all the health stuff

This would go through all of the players in the AllPlayers array, and do the same action for each one.

1 Like

thanks for the help! I thought of a for loop but have never learnt any fundamentals of C# or verse

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.