Skip to main content
Phi.Def
Inspiring
December 19, 2022
Answered

"while" in expression causing crash - alternate suggestions?

  • December 19, 2022
  • 1 reply
  • 1532 views

Hi, I'm trying to have my text type on, but I want it to seem more natural with the appearance of each next character taking a different amount of time. I want this same effect across many different versions and pieces of text (of varying length) in a large project, so I want to do this using an expression rather than keyframes.

I wrote the following expression on the start range selector of an opacity text animator (set to character index rather than %), but as soon as I leave the expression editor, After Effects crashes.


I've got a slider with a wiggling speed value, and the idea was that an additional character would appear on screen if the speed value at any time would require it. I think the problem is the while command. Unfortunately, I can't just multiply the speed value by time, because then as the wiggling speed value decreases, some characters that have already appeared disappear.

Any suggestions? Thanks!

This topic has been closed for replies.
Correct answer Dan Ebberts

Try this:

minSpeed = 2;
maxSpeed = 7;
seedRandom(index,true);

t = 0;
count = 0;
while (t < time){
  cps = random(minSpeed,maxSpeed);
  sec = 1/cps;
  t += sec;
  count++;
}
count

1 reply

Phi.Def
Phi.DefAuthor
Inspiring
December 19, 2022

I thought maybe this would work (getting the value from the previous frame and adding one), but it doesn't. The resulting character index never gets above 1.

Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
December 19, 2022

Try this:

minSpeed = 2;
maxSpeed = 7;
seedRandom(index,true);

t = 0;
count = 0;
while (t < time){
  cps = random(minSpeed,maxSpeed);
  sec = 1/cps;
  t += sec;
  count++;
}
count
Phi.Def
Phi.DefAuthor
Inspiring
December 20, 2022

Hey Dan, Thanks for that! It's great.

I've just been analyzing it to figure out how it works, and it appears that the expression must remember the count value from the previous frame, is that right? So, it doesn't re-execute the count = 0 with each new frame, right? I'm surprised by this. Is there a simple way of understanding which parts of the code are run fresh at each new frame, and which aren't?

Thanks again!