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

**URL:** <https://forums.unrealengine.com/t/problem-with-my-code-how-do-i-swap-two-players-of-different-classes-when-one-of-them-takes-damage/2149203>\
**Category:** Verse\
**Tags:** fortnite-creative, unreal-editor-for-fortnite, question, fortnite\
**Created:** [November 24, 2024, 11:29pm UTC](https://forums.unrealengine.com/t/problem-with-my-code-how-do-i-swap-two-players-of-different-classes-when-one-of-them-takes-damage/2149203 "2024-11-24T23:29:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cathyroyc](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Cathyroyc](https://forums.unrealengine.com/u/Cathyroyc)\
**Post date:** [November 24, 2024, 11:29pm UTC](https://forums.unrealengine.com/t/problem-with-my-code-how-do-i-swap-two-players-of-different-classes-when-one-of-them-takes-damage/2149203/1 "2024-11-24T23:29:24Z")

</div>

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.

```auto
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

---

<div class="post-metadata">

**Author:** ![TheOnlyBobo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TheOnlyBobo](https://forums.unrealengine.com/u/TheOnlyBobo)\
**Post date:** [November 26, 2024, 6:53am UTC](https://forums.unrealengine.com/t/problem-with-my-code-how-do-i-swap-two-players-of-different-classes-when-one-of-them-takes-damage/2149203/2 "2024-11-26T06:53:58Z")

</div>

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 🙂 😉

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

> **[Map | Unreal Editor for Fortnite Documentation | Epic Developer Community](https://dev.epicgames.com/documentation/en-us/uefn/map-in-verse)**
>
> A map is a container where you can store values associated with other values, called key-value pairs, and access the elements by their unique keys.

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

> **[ToString function | Unreal Editor for Fortnite Documentation | Epic Developer...](https://dev.epicgames.com/documentation/en-us/uefn/verse-api/versedotorg/verse/tostring)**
>
> Learn technical details about the ToString function.

> **[String | Unreal Editor for Fortnite Documentation | Epic Developer Community](https://dev.epicgames.com/documentation/en-us/uefn/strings-in-verse#stringinterpolation)**
>
> The string type represents non-numerical values like words, names, sentences, and other collections of characters.

> **[Type Casting and Conversion | Unreal Editor for Fortnite Documentation | Epic...](https://dev.epicgames.com/documentation/en-us/uefn/type-casting-and-conversion-in-verse)**
>
> Use type casting to convert from one data type to another.

Good Luck!

---

<div class="post-metadata">

**Author:** ![Cathyroyc](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Cathyroyc](https://forums.unrealengine.com/u/Cathyroyc)\
**Post date:** [November 26, 2024, 4:41pm UTC](https://forums.unrealengine.com/t/problem-with-my-code-how-do-i-swap-two-players-of-different-classes-when-one-of-them-takes-damage/2149203/3 "2024-11-26T16:41:36Z")

</div>

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. 🙂
