[Solved] Array: loop with random() and exclude

0 favourites
  • 7 posts
From the Asset Store
SynthWave Loop Pack includes 68 seamless loops, founded on 11 original melodies.
  • Couldn't find anything about that with search so I want to ask someone how this can be done.

    I have something like this

    Array: [
       [["name0"],[0]],
       [["name1"],[0]],
       [["name2"],[0]],
       [["name3"],[0]],
       ...
       [["name9"],[0]]
       ][/code:2dye1v6a]and
    [code:2dye1v6a]
    Keyboard: On Space pressed
      [Local text TempName = ""]
          +[empty sub-event] -> System: Set TempName to Array.At(random(Array.Width))
          
          +Array: For each XY element              
          +Array: Current value = TempName -> System: Create object Sprite at mouseX, mouseY  
                                              Sprite: Set name to Array.At(Array.CurX)
                                              Array:  Set value at (Array.CurX, 1) to 1[/code:2dye1v6a]
    
    What it does is:
    After pressing space key it generates random value from ArrayX and then sets this value as a Sprite "name" variable and changing (Array.CurX, 1) to 1
    For example after pressing a key if random generate number 3 it will look like this
    [code:2dye1v6a]Array: [
       [["name0"],[0]],
       [["name1"],[0]],
       [["name2"],[0]],
       [["name3"],[1]],
       ...
       [["name9"],[0]]
       ][/code:2dye1v6a]
    
    It all basic stuff but my question is:
    How can I set random() event to select only from Array with value (Array.CurX, 1) = 0 ?
    
    At start I'm selecting from ["name0","name1","name2","name3","name4","name5","name6","name7","name8","name9"]
    
    After pressing space couple of times I get "name2" and "name5". 
    And now on pressing space I would like to select from ["name0","name1","name3","name4","name6","name7","name8","name9"]
    
    For now I have event Array: Value at (Array.CurX, 1) = 0 -> Create sprite....
    
    But then while pressing space it will still go through all array X objects, and after some time nothing will spawn but values in random will keep changing until it hits the one with (Array.CurX, 1) = 0.
    
    In conclusion, my question.
    How can I set Array.At(random(Array.Width)) with exclusion every key pressed?
    
    Can't do simple "Array.At(random(Array.Width)-1") or "Array.Delete index "x" from X axis" because all values in Array are hardcoded and other stuff depends on them.
  • Why not tell it to pick random array and if value = 0, replace with 1, else pick another one ?

  • Not sure what you mean, there only one array.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Something like this?

  • You could use the RandomArray to generate your indices into your main array. Using a While loop is very inefficient.

  • You need a separate list of indices. You can either generate it every time you want to fetch a new index or just keep it synced with the data in your array. It shouldn't be that costly to generate a new list every time, unless your array has thousands of values !

    edit : I edited Seasky's example to illustrate both methods.

  • blackhornet RandomArray looks nice but not sure how it could help. Can't see a lot of options to choose from, there's only scramble, set size and get at / startvalue / width. Scrambling works nicely for a randomization but still there is no way to exclude indices that been already used.

    Seasky Magistross

    Thanks guys. I'll go with Magistross example. Don't like "While" and I'm trying to avoid it whenever I can.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)