Highlighted

if/else scripting

Community Beginner ,
Aug 27, 2020

Copy link to clipboard

Copied

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!

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

TOPICS
Expressions, How to, Scripting

Views

115

Likes

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

if/else scripting

Community Beginner ,
Aug 27, 2020

Copy link to clipboard

Copied

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!

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

TOPICS
Expressions, How to, Scripting

Views

116

Likes

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
Aug 27, 2020 0
Guide ,
Aug 27, 2020

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Aug 27, 2020 2
Community Beginner ,
Aug 27, 2020

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Aug 27, 2020 0
Adobe Community Professional ,
Aug 27, 2020

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Aug 27, 2020 0
Adobe Community Professional ,
Aug 27, 2020

Copy link to clipboard

Copied

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

 

 

Likes

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
Reply
Loading...
Aug 27, 2020 0
Guide ,
Aug 27, 2020

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Aug 27, 2020 0
Community Beginner ,
Aug 27, 2020

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Aug 27, 2020 0
Adobe Community Professional ,
Aug 27, 2020

Copy link to clipboard

Copied

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.

Likes

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
Reply
Loading...
Aug 27, 2020 0
Adobe Community Professional ,
Aug 28, 2020

Copy link to clipboard

Copied

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.

Likes

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
Reply
Loading...
Aug 28, 2020 0
Adobe Community Professional ,
Aug 28, 2020

Copy link to clipboard

Copied

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".

Likes

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
Reply
Loading...
Aug 28, 2020 0
Adobe Community Professional ,
Aug 28, 2020

Copy link to clipboard

Copied

... and if you want to be as compact as possible you can also skip the == 1 checks:

 

var result = (menu1 && menu2) ? 100:0;

 

Likes

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
Reply
Loading...
Aug 28, 2020 0