• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Trouble with If statements with AE expressions

Community Beginner ,
May 04, 2024 May 04, 2024

Copy link to clipboard

Copied

Hi

 

I'm very new to After Effects and am trying to make a scoreboard that runs off a .csv file. I want to move a cursor from an inital start position to the current score position and then, when the scores are updated, have it more to the new cursor position. I've attached an animation of the basic concept of what I've trying to do. Apologies for the ugly cloud overlay, they have been removed already.

 

The problem i'm having is constructing the if statements to direct the positon transform to the 3 locations. If I write a statement like so:

 

if(time > 5){
[currentCoordinates,708.5]
}

 

It will be nullified by adding the following:

 

if(time > 8){
[newCoordinates,708.5]
}

 

The result is the cursor only moves on the last if statement in the sequence.

 

Maybe i'm approaching this all wrong but any help would be greatly appreciated. 

 

Many thanks ATB

 

Concept video:

 

TOPICS
Expressions

Views

468

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 04, 2024 May 04, 2024

Copy link to clipboard

Copied

You may want to look into JavaScript basics. This isn't even AE specific. You need to define the conditions mathematically exact or use nested conditional statements so the engine actually looks at the second statement in the inner branch. It's also good practice to always have a fallback if none of the conditions are met.

 

if(time > 5 && time < 8){
[currentCoordinates,708.5]
}
else if(time > 8){
[newCoordinates,708.5]
}
else{
[0,0]
}

 

Mylenium

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 05, 2024 May 05, 2024

Copy link to clipboard

Copied

LATEST

Thanks for your help!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 04, 2024 May 04, 2024

Copy link to clipboard

Copied

You need an "and" argument to define the middle section. It's also a good practice to set up some variables. Something like this would give you three moves:

 

t = time - inPoint;
s = value[0];
m1 = 5; //time in second from in point
m2 = 8; // time in seconds from in point
p1 = 200; // number of pixels to jump
p2 = 300; // number of pixels to jump
if (t < m1){
	x = s;
}
if (t >= m1 && t <= m2){
	x = s + p1;
}
if (t > m2){
	x = s + p1 + p2;
}
[x, value[1]]

 

 There are other ways to do this, including setting up times and easing interpolation moves that are added to each other or using layer markers or even audio levels. I almost always define time as time - inPoint so I can drag a layer around in the timeline to change the timing instead of rewriting the expression.

 

Mylenium's expression only changes position after 8 seconds. It puts the layer in the upper left corner between 0 and 5 seconds and bounces back and forth to the upper left corner because it's missing an = sign, but it gives you another approach.

 

You'll need a different approach if you want a move that increases the position each time a new score is reached. The timing is included in your CSV file. For that, you'll need an accumulator function instead of a series of if/else statements.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 05, 2024 May 05, 2024

Copy link to clipboard

Copied

Okay nice, got it working nicely now. Thanks Rick, appreciate your help

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 04, 2024 May 04, 2024

Copy link to clipboard

Copied

The trick is to set up your tests in the correct order, use a chain of else if statements (instead of just if) and have a default else at the end in case all your tests fail. So for your example it would be:

if(time > 8){
  [newCoordinates,708.5];
}else if (time > 5){
  [currentCoordinates,708.5];
}else{
  value;
}

That way the expression will solve, no matter what the input conditions are, and as it progresses through the chain, as soon as it encounters a matching condition it won't even process the rest of the chain. So in this case, if time > 8, that's as far as it gets and it will skip the rest of the chain. Hope that makes sense.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 05, 2024 May 05, 2024

Copy link to clipboard

Copied

Thank you so much. This worked perfectly. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines