Problem with my code: How do I swap two players of different classes when one of them takes damage?

Hi! I have a problem with my code. I want the class 2 player to change class when dealing damage to a class 1 player, but I have a problem with the line: var PlayerClasses: map<player, int> := map{} which says : vErr:S71: Expected expression , got “=” after “>” (3100). I’m new to coding with Verse.

using { /Fortnite.com/Devices }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /Verse.org/Simulation }
using { /Fortnite.com/Characters }
using { /Fortnite.com/Game }
using { /Verse.org/Random }

GameManager := class(creative_device):

    # PlayerClasses maps each player to their class (e.g., 1 or 2)
    var PlayerClasses: map<player, int> := map{}

    HandlePlayerHit(DamageResult: damage_result): void =
        Print("Player got hit")

        # Check if the target is a valid Fortnite character
        if (FortCharacterWhoWasHit := fort_character[DamageResult.Target]?):
            Print("Was hit for " + DamageResult.Amount.ToString())

            # Check the instigator details
            if (Instigator := DamageResult.Instigator?):
                if (Agent := Instigator.GetInstigatorAgent[]?):
                    if (FortCharacterInstigator := fort_character[Agent]?):
                        Print("Was hit by another player")

                        # Validate both instigator and target classes
                        if (PlayerClasses.HasKey(FortCharacterInstigator) and PlayerClasses.HasKey(FortCharacterWhoWasHit)):
                            if (PlayerClasses[FortCharacterInstigator] = 2 and PlayerClasses[FortCharacterWhoWasHit] = 1):
                                Print("Class swap triggered")

                                # Swap the classes
                                TempClass := PlayerClasses[FortCharacterInstigator]
                                set PlayerClasses[FortCharacterInstigator] = PlayerClasses[FortCharacterWhoWasHit]
                                set PlayerClasses[FortCharacterWhoWasHit] = TempClass

                                Print("Classes have been swapped")
                                Print("Instigator new class: " + PlayerClasses[FortCharacterInstigator].ToString())
                                Print("Target new class: " + PlayerClasses[FortCharacterWhoWasHit].ToString())

    OnBegin<override>()<suspends>: void =
        # Initialize PlayerClasses map
        PlayerClasses = map{}

        # Get all players in the playspace
        Players := GetPlayspace().GetPlayers()

        # Assign random classes to players
        for (Player: Players):
            set PlayerClasses[Player] = Random.GetRandomInt(1, 2)

        # Subscribe to damage events for each player's Fortnite character
        for (Player: Players):
            if (FortCharacter := Player.GetFortCharacter[]?):
                FortCharacter.DamagedEvent().Subscribe(HandlePlayerHit)

Thanks

I appreciate that its obvious you realize the map is similar to a dictionary / list/ etc
I tried your code and there might be more to unpack here but I think you have a decent code background and will be able work through it. That can be an exercise for the reader. Once you get past the “top of the file” compile error of course :slight_smile: :wink:

Your declaration is off, try:
var PlayerClasses : [player]int = map{}

Also maybe these come in handy for the next few errors:

Good Luck!

I’ll check. I didn’t write this code myself. I had to copy code similar to what I wanted to do. I also used ChatGPT UEFN to try to add code to get the result I wanted, but I got a lot of errors. Thanks for your answer. :slightly_smiling_face:

1 Like