Why does my "For each" keep going

0 favourites
  • 10 posts
  • I am trying to make an array which will generate random numbers for all of it's values and display those values one at a time in a text box, one second between showing each. My array is a 4X9. At first, I used:

    For each XY Element

            >set value at(Array.CurX, Array.CurY) to random(100)

    To show the results, I used:

    For each XY Element

            >Set text to Array.CurValue

            >wait 1 second

    What I got was a text box which flashed with random numbers, as if it was simply running the loop over and over, ignoring the "wait." I might just be dong something obviously wrong and stupid, but my understanding is that the "for each" loop will only run once for every value in an array... if anyone would be so kind as to impart their knowledge upon a lesser being, it would be greatly appreciated :)

  • The 'wait' has to come before the 'set text' I believe, and you should replace the 'for each' with 'for each element', an array-only expression

  • alas, I tried what you suggested and yet it still spit out numbers at lightning speed... and you're right I meant to say "for each element"

  • Any loop will run its full course in a single tick (discarding the action wait).

    What you could do would be to use global variable to keep the index you currently are (X and Y for the array), and in an event "Every 1 second" display the current value.

  • hmm, that's a good idea and I'm sure that would work if my whole purpose was to create a bunch of random numbers, yet it defeats the whole purpose of an array... I guess i'm really trying to put numbers into an array and having them remain constant without imputing each as separate random numbers (for that would also defeat the point of an array)... yet that might be what I end up having to do...

  • You asked a timed display of the values

    That's what I proposed.

    The array is filled once on start of the layout.

    And then you use the events "Every X second" to display the values that are stored in the array at timed intervals.

    From what I understood in your first post that's what you were asking.

    Running an event "For each elements" without any other condition/filter will just run continuously over time. This is how events work.

    Else I may have misunderstood what your question really is. Please forgive me and please reformulate it so I finaly get it <img src="smileys/smiley1.gif" border="0" align="middle">

  • See How to use the System 'Wait' action. 'Wait' does not suspend the entire event sheet - it would really not be at all useful if it did. So the events carry on running as normal (e.g. loops finish instantly), it just schedules the rest of the event to be run after a delay.

    Usually what you really want to do is something like

    + For each Sprite

    -> Wait loopindex seconds

    -> Do something with Sprite

    This spreads out each iteration one second apart. If you do this:

    + For each Sprite

    -> Do something with Sprite

    -> Wait 1 second

    It means "run the loop instantly, wait 1 second, then do nothing".

  • Thank you guys so much for helping me out!!! To figure out what was going on, I tried:

    +For each XY element

       -> Set value at(Array.CurX,Array.CurY)to random (100)

    +Every 1 second

       -> Set Text to Array.at(1,1)

    yet I was still frustrated because it kept giving me different numbers for the same place in the array. then I added that pesky little "while current value is zero" after the "for each element" and instantly it gave me the same number for the same position (1,1) over and over!!!

    Ashley, thank you so much for clearing that up for me, It was a really big help!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • you have to

    System: Start of layout
    System: Foreach XY element
      -> Array: set value at(Array.CurX,Array.CurY) to random (100)
    Every 1 second
      -> Text: set Text to Array.At(1,1)

    if you don't limit your foreach to a start of layout, it will run each tick, thus reseting your value to another random(100) multiple times each seconds

  • Ahh thank you very much!

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