ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

if/else in an array

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Craig Kristensen
if/else in an array
on Mar 23, 2020 at 2:35:52 pm

I have 2 slider values "h" and "a" and a range of values in an array "i".
I would like to check if the slider values are included in the array.

What am I missing here?

Stay safe,
Craig

h = thisComp.layer("CONTROLLER").effect("Home")("Slider");
a = thisComp.layer("CONTROLLER").effect("Away")("Slider");
i = [1, 6, 11, 12, 16, 17, 20, 21, 22, 23, 25, 26, 27, 28, 30, 32];

if ( h == i ) {
100
}
else if ( a == i ) {
100
}
else 0;


Return to posts index

Andrei Popa
Re: if/else in an array
on Mar 23, 2020 at 4:14:04 pm

Hi Craig.
If you have AE 2019+, you can use the includes() function:

h = thisComp.layer("CONTROLLER").effect("Home")("Slider");
a = thisComp.layer("CONTROLLER").effect("Away")("Slider");
i = [1, 6, 11, 12, 16, 17, 20, 21, 22, 23, 25, 26, 27, 28, 30, 32];
i.includes(h) || i.includes(a) ? 100 : 0


Older AE don't have includes() defined so you have to make a similar function:

function arrayIncludes(arr,el){
for(var i=0; i<arr.length;i++){
if(el==arr[i]) return true;
}
return false
}

h = thisComp.layer("CONTROLLER").effect("Home")("Slider");
a = thisComp.layer("CONTROLLER").effect("Away")("Slider");
i = [1, 6, 11, 12, 16, 17, 20, 21, 22, 23, 25, 26, 27, 28, 30, 32];

arrayIncludes(i,a)||arrayIncludes(i,h) ? 100 : 0


Andrei
My Envato portfolio.


Return to posts index

Craig Kristensen
Re: if/else in an array
on Mar 23, 2020 at 6:44:30 pm

Andrei to the rescue again!
Thanks Andrei, it worked perfectly. Although the first expression did not work in AE 2020, but the second expression worked like a charm!

Thanks again!
Stay safe.


Return to posts index


Andrei Popa
Re: if/else in an array
on Mar 24, 2020 at 9:46:02 am

It's strange that it did not work in AE 2020. Maybe you had your project expressions engine set to Legacy ExtendScript.
You can check that by going in File>Project Preferences>Expressions and set Engine to Legacy ExtendScript.

I'm telling you this because in case you want to use any of the features introduced after AE 2018, you need to have the engine set to JavaScript.

Andrei
My Envato portfolio.


Return to posts index

Craig Kristensen
Re: if/else in an array
on Mar 24, 2020 at 10:32:39 am

My project settings was set to "Legacy ExtendScript". But the new version still does not work, even after I have set the project to "JavaScript".

But the first expression still works perfectly :)


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2020 CreativeCOW.net All Rights Reserved
[TOP]