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

if/else scripting

Explorer ,
Aug 27, 2020 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!

TOPICS
Expressions , How to , Scripting

Views

8.1K

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

correct answers 1 Correct answer

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

Votes

Translate

Translate
Mentor ,
Aug 27, 2020 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

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

Copy link to clipboard

Copied

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

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

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

 

 

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

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
Explorer ,
Aug 27, 2020 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.

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

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects

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
Advocate ,
Aug 28, 2020 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.

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 ,
Aug 28, 2020 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".

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects

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 ,
Aug 28, 2020 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;

 

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects

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
Explorer ,
Aug 28, 2020 Aug 28, 2020

Copy link to clipboard

Copied

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.

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
Mentor ,
Aug 28, 2020 Aug 28, 2020

Copy link to clipboard

Copied

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

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
Explorer ,
Aug 28, 2020 Aug 28, 2020

Copy link to clipboard

Copied

LATEST

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

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