Skip to main content
Participating Frequently
August 27, 2020
Answered

if/else scripting

  • August 27, 2020
  • 6 replies
  • 16900 views

Hi!

Here is my issue; there are two dropdown menus with 2 sub menus for both and an animated layer. i want to use dropdown menus to show or hide this layer in this way, if i selected, first sub menu of first dropdown menu and first sub menu of second drop down menu, the animated layer will hide (or show).

here is what i use for this. but in this way, i did not get what i want.

 

var menu1 = comp("kj v1").layer("controller").effect("kac tl")("Menu")

var menu2 = comp("kj v1").layer("controller").effect("kacinci soru")("Menu")

 

if (menu1 == 1) {100} else {0}

if (menu2 == 1) {100} else {0}

 

in this way, it is working perfectly for menu 2 but not for menu1. i need to combine if menu1 and if menu2 in the same command. Thank you!

Correct answer Martin_Ritter

First of all, you should start using semicolons 😉

 

Did I get you right, that there a two conditions to trigger opatity: menu1 AND menu2 must be selected?

 

Then you have to write it this way:

 

if (menu1 == 1 && menu2 == 1){

    100;

} else {

    0;

};

 

*Martin

6 replies

rajansAuthor
Participating Frequently
August 28, 2020

now, i am using this script

 

var menu1 = comp("kj v1").layer("controller").effect("yanlis cevap")("Menu")

var menu2 = comp("kj v1").layer("controller").effect("dogru cevap")("Menu")

if(menu1 != 1&&menu2 == 2)100; else 0

 

this works fine. I want to add something new. Once this script has started, one of the layers should be hidden in frame 50th and after in the timeline. it should only be visible before the 50th frame. I tried something but failed.

Martin_Ritter
Legend
August 28, 2020

hideAt = 50 * thisComp.frameDuration;

if (time >= hideAt){

    0;

} else {

    100;

};

 

The other guys will show you another thousend ways how to write an if-condition 😉

 

*Marin

rajansAuthor
Participating Frequently
August 28, 2020

is this a good way to combine both scripts

 

var menu1 = comp("kj v1").layer("controller").effect("joker")("Menu")

hideAt = 150 * thisComp.frameDuration;

if(menu1 == 2&&time >= hideAt)0; else 100

Tomas Sinkunas
Legend
August 28, 2020

There are many ways to skin a cat in Javascript. One of which is this:

 

 

var value1 = comp("kj v1").layer("controller").effect("kac tl")("Menu").value;
var value2 = comp("kj v1").layer("controller").effect("kacinci soru")("Menu").value;

var result = (value1 && value2) * 100;

result;

The logic behind (value1 && value2) * 100 is as follows;

  1. (value1 && value2) is a boolean operator: if both values are true, then this would result in true. If any of the values are false, then the result will also be false.
  2. Taking boolean result from #1 we get (true) * 100, or (false) * 100. When multiplying Boolean with a value, the Boolean is converted into an integer (1 for true, and 0 for false), so we end up with 1 * 100, or 0 * 100;
  3. Therefore, if both values are true, the result is 100; if any of those values is false, then we get 0.

Not saying you have to use such logic, as it might look unfamiliar and weird for beginners but just letting you know that such a thing exists. The more you know, the less you know.

Mathias Moehl
Community Expert
Community Expert
August 28, 2020

Yes, if your goal is to write hard to read code, Tomas example can be optimized even further to

 

var result = Math.min(menu1,menu2) * 100;

or

 

var result = menu1 * menu2 * 100;

 

😄

 

But good code should be easy to read, so I recommend

var result = (menu1 == 1 && menu 2 == 1) ? 100:0;

because it litterally says "Is menu1 equal to 1 and menu2 equal to 1? If so, take 100 otherwise 0".

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
rajansAuthor
Participating Frequently
August 27, 2020

thank you all guys for help. i solved my problem with your help. this is a great place for me.

Mathias Moehl
Community Expert
Community Expert
August 28, 2020

Also note that the ? operator is a good solution for these kinds of if statements that just create a single value. It has the form

CONDITION ? IF_VALUE : ELSE_VALUE

 

if (menu1 == 1 && menu2 == 1){
100;
} else {
0;
};

is the same as

(menu1 == 1 && menu2 == 1) ? 100:0;

 

It also has the advantage that it can be used in variable declarations.

var result = (menu1 == 1 && menu 2 == 1) ? 100:0;

which is not possible with if statements.

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Community Expert
August 27, 2020

If I understand you correctly and both menu's are on a controller layer then you need an if (condition a = 1 && condition b = 1) then, else statement.  If that is correct then the cleanest way to write the expression looks like this:

 

ctrl = thisComp.layer("Controller");
testA = ctrl.effect("Menu 1")("Menu");
testB = ctrl.effect("Menu 2")("Menu");
if (testA==1 && testB == 1)
	0
else
	100

 

 

Martin_Ritter
Legend
August 27, 2020

Sorry Rick, but that's what I posted 3 hours ago and you miss the curly brackets (Expressions might don't need them, but a clean java script code does).

 

*Martin

Justin Taylor-Hyper Brew
Community Expert
Community Expert
August 27, 2020

Yep Martin said it, you can only return 1 value so you have to combine the if/else statements. Here's a reference on more comparison synatx:

https://www.w3schools.com/js/js_comparisons.asp

Martin_Ritter
Martin_RitterCorrect answer
Legend
August 27, 2020

First of all, you should start using semicolons 😉

 

Did I get you right, that there a two conditions to trigger opatity: menu1 AND menu2 must be selected?

 

Then you have to write it this way:

 

if (menu1 == 1 && menu2 == 1){

    100;

} else {

    0;

};

 

*Martin

rajansAuthor
Participating Frequently
August 27, 2020

that's greate. i found 2 answers with yours, and both works perfect.