FORUMS TUTORIALS FEATURES VIDEOS PODCASTS EVENTS SERVICES JOBS NEWSLETTER NEWS BLOGS
 ADOBE AFTER EFFECTS: Home Forum Basics Expressions Tutorials Podcasts AE Techniques Creative Cloud Debate FAQ

# While() loop time out assistance

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
 While() loop time out assistance by James Jeges on Mar 11, 2010 at 1:15:21 pm

Hey everyone, (sorry in advance for the wall of text)

I've been working on this expression now for a few days, and everything works fine until I un-comment the very last few lines where I start a while() loop. I know I've created an infinite loop somewhere, I just can't find the reasoning behind it.

Basically this script generates a random number and compares it to 2 previous values in time to determine whether or not it is the same value at that point in time. If it is the same value, It offsets the seed until the value it generates is unique to the 2 values before it.

Probably there is a better way of achieving this goal, but I am more interested in why the things I write don't work, so I can learn from my mistakes. So any help in that regard would be excellent.

(the following expression goes into a Slider expression control)

```//TIME PARAMATER VARIABLE DECLARATIONS fdur = thisComp.frameDuration; tframe = time / fdur; ftime = tframe * fdur; mlength = 2; hlength = mlength * fdur; //GENERAL VARIABLE DECLARATIONS x = 0; //main output value x2 = 0; //x at -1 test interval for testing x22 = 0; // x at -2 test intervals for testing same = false; // indicatesthat x is the same as x22 or x2 mod = 0; //used to offset the seed //FUNCTION DECLARATIONS //The generate() function uses a seed based on time (seconds) to randomly generate a number between 0 and 10 at frame intervals based on the mlength variable function generate(t) { seed = Math.floor(t / hlength)*(mod+1); seedRandom(seed, true); return Math.floor(random(0,10)); } // The checker() functions comparas x2 and x22 against x to see if they are the same, indicating repeating values function checker(t) { if(x2 == x){ same = true; }else if (x22 == x){ same = true; }else{ same = false; } } //assigns all the x, x2 and x22 values to their appropriate testing values function assigner(t) { x = generate(t); x2 = generate(t-(mlength*fdur)); x22 = generate(t-(2*mlength*fdur)); } //used to increase the seed offset (mod), regenerate x and test it against the original x2 and x22 values at the current time to make the same variable = false in order for the main purpose of this expression to be completed function shuffle(t) { mod++; x=generate(t); checker(t); } //Main Expression assigner(time); checker(time); //problems start when the following loop becomes un-commented. Supposedly it would keep executing the shuffle command until x finds a suitable value where same=false. /* while(same) { shuffle(time); } */ x```

 Re: While() loop time out assistanceby Filip Vandueren on Mar 11, 2010 at 2:13:36 pm

I don't have After Effects on my machine right now,
but the problem is most likely that the "same"-variable is not seen as a global variable by the functions, so they are creating their own variable with the "same" name, the while() loop never sees the updated version of the variable.

try decalring like this, I think that should work:
global same=false;

 Re: While() loop time out assistanceby James Jeges on Mar 12, 2010 at 10:36:42 am

Are you sure that global is a javascript keyword? It wasn't recognised and caused another error.

Still, I found the source of my error, if you're still interested. I found out the hard way that negative seeds will not generate random numbers. So at time = 0, when I was testing the previous values at time= -1, I was reading a negative seed which always returned the same value regardless of time. (-1, -2, -3 etc always returned 2 for example.) Thus causing an infinite loop in the expression.

I'm still experimenting the best way to fix this, but the solution I came up with is to declare my seed as:

seed = Math.abs(...)