How do I Properly setup a Loop - my loops are only iterating once.

0 favourites
  • 8 posts
From the Asset Store
Change the size and position of everything without calculating anything!
  • Hi,

    I am trying to make a unique random number picker for a project.

    The issue I seem to be having is that the loops I created to carry out this process only seem to be iterating one time and not "looping." I am wondering if I am not setting the loops up properly (meaning physically, nesting the condition and sub-events properly in the event sheet).

    My intentions are to repeatedly pick a random number, compare it to the members of a picked-number-log array. If it matches a member of the array, the program will pick another number and try again. If it does not match anything in the array, the program will add that number to the array and return that value as "thisIssue."

    (I am using the result of this as an Index to pick some JSON data.)

    Currently, I created a function ("issuePicker") that contains a Loop (I've tried both WHILE and REPEAT with the same result) that should repeat until a "unique" condition is satisfied (unique < 1).

    When the WHILE/REPEAT loop starts, it picks a random number (assigns it to the var "thisIssue").

    I then run a ForEach loop on a "issueLog" array and compare each member to "thisIssue".

    If a match is found, it adds 1 to the variable "unique" (unique = unique+1).

    If no matches are found, "unique" remains at 0.

    In the next condition, I check If unique < 1 and if so, Stop Loop.

    What I believe should be happening is that for each iteration of the WHILE/REPEAT loop, the ForEach Loop inside of it should execute for each number of elements in the "issueLog" array. If any of the ForEach checks fail, the WHILE/REPETE loop should iterate again and so on until an actual unique random number is found.

    What seems to be happening, however, is that the For Each loop is only executing one time and the WHILE/REPEAT loop is also executing one time. Even if I screwed up the exit process for the WHILE/REPEAT loop, (I think) I should be seeing the For Each loop iterate for at least the number of times there are elements in the "issueLog" array. Correct?

    (I added a test text box and have some "append texts" that should be appending the LoopIndex for the ForEach and the counting and displaying the loops for each WHILE/REPEAT iteration. Both of these text boxes only show a single entry for each of these every time I run the "issuePicker" function.

    When I look at debug, I can see that the random numbers ("thisIssue") is getting added to the "issueLog" array. (In addition I put some dummy entries in the "issueLog" array to start with just for testing... so the For Each should be running multiple times, but it is not. I even tested my WHILE loop by adding the condition "unique < 10000" (just to see if it would hang up my program for 10000 loops) there was no difference.

    Does anyone have any ideas? Does it have something do with my putting the loop inside of a function? Am I just not nesting the conditions and sub-events properly?

    Any help would be greatly appreciated.

    Thank you,


  • you are doing a number of things that could cause the problem - without seeing the code it is impossible to say.

    you should be using the Advanced Random to generate a unique list.

    or if you want to do it manually, use IndexOf to find if thisIssue is already in the array...

  • It would probably better to use a dictionnary, since the entries are indexed, you only need to check if the key "str(randomnumber)" exists instead of looping like that

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • thanks for the suggestion... I just switched from C2 to C3 last night so I didn’t even know there were any new random number tools to try. I will try that.

    (The only reason I didn’t post my code is because it’s very much a prototype at this point with lots of other issues that I was afraid would cloud the issue I was trying to get help with :-). I’m going to try some other options and will fall back to this if I’m not successful. I will post a cleaned up version of my non-working event sheet)

    Thanks again for your input!


  • Hi, thanks for your help... do you mean use a dictionary to store my “issueLog” instead of using an array? Just for clarification as I have done a lot with C2 but haven’t used dictionaries that much, the “if exists” functionality that you’re referring to is a dictionary-specific function as an alternative to my looping over the array and attempting to compare each value? That definitely sounds like a cleaner way to do things. I will look at that later.

    Thank you very much!


  • As you can see in the Dictionary manual article there is a dedicated condition "Has key" (that already existed in Construct 2 as well).

    Use this condition and the provided suggestion of checking that this key's name is "str(randomnumber)".

    You can also check the Advanced Random plugin manual article.

    It contains useful examples to check out. As the names indicates though, it is an advanced feature.

  • I think I am going to go with a Dictionary - seems like a simpler concept for what I'm trying to do. In essence what I am creating is like a "trivia game." My goal is to simply randomize the questions ("issues" in my case) without repeating the questions.

    If I am understanding, what I am going to do is create a "issueLog" dictionary of keys with no values and just store my "already used numbers" (the "issue" indexes) as the Keys of each new dictionary entry. Then I will use the Has Key condition for the dictionary to determine if that index has already been added to the dictionary and perform the correction actions based upon that result.

    Gonna work on this now - thanks again for the advice!

  • Hi, Went with dictionary. Worked like a charm. Simplified my unique-randomizer function, too. Thank you very much for the help and suggestion!


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