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

Get help using Construct 2

Post » Thu Apr 10, 2014 12:27 pm

Couldn't find anything about that with search so I want to ask someone how this can be done.

I have something like this
Code: Select all
Array: [
   [["name0"],[0]],
   [["name1"],[0]],
   [["name2"],[0]],
   [["name3"],[0]],
   ...
   [["name9"],[0]]
   ]
and
Code: Select all
>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


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: Select all
Array: [
   [["name0"],[0]],
   [["name1"],[0]],
   [["name2"],[0]],
   [["name3"],[1]],
   ...
   [["name9"],[0]]
   ]


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.
Last edited by shinkan on Thu Apr 10, 2014 8:03 pm, edited 1 time in total.
ImageImageImageImage
B
158
S
67
G
43
Posts: 2,603
Reputation: 35,968

Post » Thu Apr 10, 2014 1:34 pm

Why not tell it to pick random array and if value = 0, replace with 1, else pick another one ?
Using R243 for all examples
B
69
S
20
G
6
Posts: 379
Reputation: 9,701

Post » Thu Apr 10, 2014 2:09 pm

Not sure what you mean, there only one array.
ImageImageImageImage
B
158
S
67
G
43
Posts: 2,603
Reputation: 35,968

Post » Thu Apr 10, 2014 3:12 pm

Something like this?
You do not have the required permissions to view the files attached to this post.
B
21
S
8
G
7
Posts: 49
Reputation: 5,274

Post » Thu Apr 10, 2014 4:02 pm

You could use the RandomArray to generate your indices into your main array. Using a While loop is very inefficient.
ImageImageImage
B
71
S
22
G
249
Posts: 3,763
Reputation: 138,577

Post » Thu Apr 10, 2014 4:19 pm

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.
You do not have the required permissions to view the files attached to this post.
B
75
S
31
G
27
Posts: 1,000
Reputation: 20,735

Post » Thu Apr 10, 2014 5:47 pm

@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.
ImageImageImageImage
B
158
S
67
G
43
Posts: 2,603
Reputation: 35,968


Return to How do I....?

Who is online

Users browsing this forum: tarek2 and 15 guests