Verse array assignement question

Hey, losing my mind on arrays with verse, I think there’s something big i’m missing.

Let’s use the following code :

var Array1 : []int = array{}
if(set Array1[0] = 5){}
Print(ToString(Array1.Length))

This will print “0”, where I would have expected the value to be 1.
The documentation is not clear as it only build arrays from contructors directly, and doesn’t dynamically assing a value to an Index which doesn’t exist yet.

The end goal is to be able to dynamically set arrays.

I’m especially confused as in the doc, there’s this 4. Tracking Players Using Maps

it’s using this code :

  var PlayerMap : [player]int = map{}

AllPlayers := GetPlayspace().GetPlayers()
 for (Agent : AllPlayers, TeamPlayer := player[Agent], FortCharacter := TeamPlayer.GetFortCharacter[]):
     if(set PlayerMap[TeamPlayer] = 0, WeaponTier := PlayerMap[TeamPlayer]):
         Print("Assigned Player to PlayerMap with Tier {WeaponTier}")
         FortCharacter.EliminatedEvent().Subscribe(OnPlayerEliminated)

To me, it looks liek it’s assigning a value to an index that doesn’t yet exist, but I must be missing something.

If someone could help me figure it out, would be much appreciated :slight_smile:

The statement if(set Array1[0] = 5){} fails because you are trying to access an index in the array that doesn’t exist yet (it’s empty),
so the assignment never happens.
You can add a value dynamically by doing set Array1 += array{ 0 }, where 0 is the value you want to add.

The docs you linked show how a map works, which is different from an array

1 Like

Thanks, I didn’t think about assigning the array like this.