I can't seem to add a value to the map type variable.

So I have an verse script that is supposed to keep track of which checkpoint a player is on, and later respawn him in that checkpoint. But no matter what I do the map keeps being empty and no value is stored in it, so I can’t proceed with the respawn logic.


team1_manager := class(creative_device):

    @editable
    RaceCheckpoints : []race_checkpoint_device = array{}

    var PlayersRef : [player]int = map{}

    var Player1Checkpoint : []int = array{0}

    #var PTranslation :  (vector3, vector3) = vector3.zero

    # Runs when the device is started in a running game
    OnBegin<override>()<suspends>:void=
        Sleep(1.0)
        Print("Team1 Manager Started")
        AllPlayers := GetPlayspace().GetPlayers()
        for (TeamPlayer : AllPlayers, CurrentTeam := GetPlayspace().GetTeamCollection().GetTeam[TeamPlayer]):
            if(CurrentTeam = 1):
                if(set PlayersRef[TeamPlayer] = 0, Checkpoint := PlayersRef[TeamPlayer]):
                   Print("Player checkpoint is {Checkpoint}")

        for (players := 0..PlayersRef.Length):
            Print("Player {players} is on Team 1")
            Print("Player map size: {PlayersRef.Length}")

        for (Checkpoint : RaceCheckpoints):
            Checkpoint.CheckpointCompletedEvent.Subscribe(ClearedCheckpoint)
        #end for

    
    ClearedCheckpoint<private> (Agent : agent):void=
        #Print("Cleared Checkpoint")
        if(value := PlayersRef[Agent]):
            Print("Player Cleared  {value} Checkpoints")
        else:
            Print("Player not in map, map size: {PlayersRef.Length}")
        for(Key -> Value : PlayersRef):
            Print("Key: player0 Value: {Value}")

            if (Key = player[Agent]):
                Translation := vector3
                index := 0
                if:
                    set Player1Checkpoint[Value] = Player1Checkpoint[Value] + 1
                    index = Player1Checkpoint[Value]
                    #set Translation = RaceCheckpoints[Player1Checkpoint[Index]].GetTransform().Translation
                    Rotation := RaceCheckpoints[Player1Checkpoint[Value]].GetTransform().Rotation
                #Respawn(Players, Translation, Rotation)
                Print("Checkpoint saved: {index}")
            #end if

When the game starts, sometimes it does’t even print the requested prints and when it prints, it says that a player is in team 1 but the Player map size is 0.

You’re checking “if(CurrentTeam = 1)” which will never succeed, because CurrentTeam is an instance of the team class, not an integer like “1”. If you want to find the index of the team (and btw the first team is 0, not 1), after getting the CurrentTeam you’ll need to do TeamIndex := GetPlayspace().GetTeamCollection().GetTeams().Find[CurrentTeam].

I recommend writing a line at the top of the OnBegin that’s something like TC := GetPlayspace.GetTeamCollection() so your code is easier to read and write.

1 Like