Simple Array Shuffle

Attached Files

The following files have been attached to this tutorial:

.PNG

construct-shuffle.PNG

Download now 14.88 KB

Stats

3,760 visits, 6,462 views

Translations

This tutorial hasn't been translated.

Tools

Here's how to shuffle an array in 1 event. In this example, I fill a simple array with 4 values (0, 1, 2, 3). This represents 4 music tracks in which I want to make a shuffled playlist.

Setup:

Create 2 Arrays with the same Width (in this case 4)

1 as a temporary Array (Array_Temp)

1 as the final shuffled Array (Array_Music)

Create a Local variable (pick = 0)

     Event: System | Repeat 4 times
     Action: Array_Temp | Set value at loopindex to loopindex

(the above event is used to simply fill our empty array with values)

     Event: System | Repeat 4 times
     Action: System | set pick to floor(random(Array_Temp.Width))
     Action: Array_Music | Set value at loopindex to Array_Temp.At(pick)
     Action: Delete index pick from X axis

In a nutshell, "pick" selects a random position in the temp array and copies that value into next sequential position in the shuffled array. Then it removes the array position in the temp array ensuring it's never chosen again.

.PNG

construct-shuffle.PNG

Download now 14.88 KB
  • 0 Comments

  • Order by
Want to leave a comment? Login or Register an account!