Stop Loop doesn't work on a For Each Array Element loop

Bugs will be moved here once resolved.

Post » Mon Apr 06, 2015 2:05 am

Message: DrDeleto can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
Problem Description
Attempting to Stop Loop on a For each element loop on an array, but it continues the rest of the loop.

Attach a Capx

Description of Capx
I've made a simple array with a list of Thingees and a Weight for each. I'm trying to randomly pick one based on the weights. (In my example the weights are all identical.) When you click the GO button, it loops through all the weights and adds them up to get the total, then picks a random number between 0 and that total. Then it loops back through the array and subtracts each weight from the random number until the number is negative. If it is, the loop should stop and return the Thingee associated with the current weight's row.

(Note: I think I've found a work-around that is probably a better way to achieve my goal, but this still seemed like a bug. Also, I know selecting things with weights is a common programming problem and I don't claim to be an expert, so there might be a much better approach. I wouldn't mind advice on optimizing my approach if it comes to mind when reviewing my code.)

Steps to Reproduce Bug
  • Click the button and view the output.
  • If the random number picked is greater than 20, then the loop will stop properly on the last item. If this happens, click GO again to retry.
  • When the random number is low enough that the loop should stop on the first or second item (because it becomes negative), the loop is supposed to stop. The text display will say "Stop Loop!"

Observed Result
Even if a Stop Loop action is fired early in the loop, the loop will continue through all three elements.

Expected Result
When the random number is decremented below zero, and the Stop Loop action is fired, I expect that the rest of the actions in the set will fire, but the loop will not continue to the next element.

Affected Browsers
Tested on Chrome

Operating System and Service Pack
Windows Vista (latest service pack)

Construct 2 Version ID
Release 200, 32-bit, Steam version
Posts: 12
Reputation: 772

Post » Mon Apr 13, 2015 11:27 am

It's not a bug, the System 'Stop loop' action is only designed to work with System loops. Perhaps you could iterate the array using a system loop.
Scirra Founder
Posts: 24,376
Reputation: 193,842

Return to Closed bugs

Who is online

Users browsing this forum: No registered users and 13 guests