Unexpected behaviour within repeat loop

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Alistair
Posts: 33
Joined: Mon Jun 23, 2008 6:45 am

Unexpected behaviour within repeat loop

Post by Alistair » Sat Oct 25, 2008 5:08 am

I am encountering what to me seems strange behaviour within a repeat loop.

In the following script, put theta into fld "theta" does not result on display of theta until the script has exited the repeat loop. Even stranger, moving the line "put phi into fld "phi"" above "end repeat" seems to result in the script hanging. I don't see any reason why using a PUT command within a repeat loop should be illegal. What I would have expected is that the value of theta would have been updated each time the script loops through the outer repeat loop and that a similar result would have been obtained if the line "put phi into fld "phi"". Am i missing something important?



on mouseUp
global p, d, sumx, sumz, ax, az, Ao

put 0.01 into d

repeat with theta = 0 TO pi STEP d
repeat with phi = 0 TO 2*pi STEP d
put COS(theta)^4*SIN(theta) into az
put COS(phi)^2*COS(theta)^2*SIN(theta)^3 into ax
put sumz + az*d*d into sumz
put sumx + ax*d*d into sumx
end repeat
put phi into fld "phi"
Put theta into fld "theta"
end repeat
put (sumz-sumx)/(sumz+2*sumx) into Ao
put Ao into fld "answer"

end mouseUp

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Sat Oct 25, 2008 6:23 am

When you're in a tight loop, the engine may not be getting enough 'breathing room' for repaint events - and this gets worse if you update a field a few thousand times as it can eat up quite a bit of CPU so it may look like the UI is 'frozen'. Try adding a

Code: Select all

wait 0 milliseconds
right after the line

Code: Select all

put theta into fld "theta"
HTH,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Alistair
Posts: 33
Joined: Mon Jun 23, 2008 6:45 am

Post by Alistair » Sat Oct 25, 2008 6:48 am

Thanks, Jan. That made a world of difference. I thought that the explanation may be something like this. However, how does the WAIT command change the situation if the wait time is set to zero?

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Sat Oct 25, 2008 7:18 am

The 'wait' is enough to allow the repaint buffer to kick into action - using 0 milliseconds means your handler will continue to run right after that.

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Alistair
Posts: 33
Joined: Mon Jun 23, 2008 6:45 am

Post by Alistair » Sat Oct 25, 2008 8:07 am

I understand. I am starting to grasp the differences between what happens in the engine and what happens outside the engine. Thanks again for your terrific support.

Post Reply