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

Beginner question with Expressions-- How to set a if/else inside of a variable

Explorer ,
Apr 12, 2020 Apr 12, 2020

Copy link to clipboard

Copied

Trying to add functionality to a basic MOGRT and I'm really rough with expressions.

On top of this graphic is a line of text (master property, in this case layer XXX). 

 

This code is on a white box that will expand/contract it's width based on length of text in XXX.

Found out that this would work: 

 

L= thisComp.layer("XXX");
X= L.sourceRectAtTime().width*.7;  /*as the string of text in XXX gets longer, needed a multiplier to tighten up the padding... 0.7 works fro the range of possible text*/
Longer=X+8; /*needed to give some padding when XXX is just a single "i"*/
Y=100;
[Longer,Y];

So, this works great... except when there is NO text in the XXX layer.

It returns the 8. Which creates a thin white backgorund box. 

 

What I think would work; add another variable "P" for padding. 

 

P= if(L.sourceRectAtTime().width)>0){8}else{0}; /*this is saying 'illegal use of if'*/

and then I would change this line Longer=X+P; 

 

TOPICS
Expressions , How to

Views

850

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

Community Expert , Apr 12, 2020 Apr 12, 2020

Try replacing your second line with:

 

P = L.sourceRectAtTime().width > 0 ? 8 : 0;

 

 

Dan

Votes

Translate

Translate
LEGEND ,
Apr 12, 2020 Apr 12, 2020

Copy link to clipboard

Copied

Nothing wrong with the code per se, you just threw in one too many closing parentheses. Remove the one ) after .width and everything will work.

 

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
Explorer ,
Apr 12, 2020 Apr 12, 2020

Copy link to clipboard

Copied

Error: Illegal use of reserved word 'if' 

Nick_Schale_0-1586708952840.png

 

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 ,
Apr 12, 2020 Apr 12, 2020

Copy link to clipboard

Copied

Is there something else I need to typ to start the if/else math? ...seems to get hun up on "illegal use of 'if' "

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
LEGEND ,
Apr 12, 2020 Apr 12, 2020

Copy link to clipboard

Copied

That's funky, but perhaps simply filling in the relevant paramters to sourceRectAtTime() might fix it. Also, now that I have a second look, you are also of course creating a cyclic condition. I mean, if the rectangle is zero, anyway, then there's no reason to double-zero your variable P. Or in other words: You are using the testing condition also for the output value, which makes no real sense. In fact that could be the actual error. A source rectangle of zero width cannot exist, anyway.

 

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 Expert ,
Apr 12, 2020 Apr 12, 2020

Copy link to clipboard

Copied

Try replacing your second line with:

 

P = L.sourceRectAtTime().width > 0 ? 8 : 0;

 

 

Dan

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 ,
Apr 12, 2020 Apr 12, 2020

Copy link to clipboard

Copied

LATEST

THANK YOU

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