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

Scripting Question

People's Champ ,
Jul 01, 2017 Jul 01, 2017

Copy link to clipboard

Copied

So I'm following a beginners scripting lesson for AE and my code sent AE into an infinite loop.

When I changed the line myValue = myValue++;   to  myValue = myValue+1;   it fixed the issue and the script ran correctly.

As I learned this operator using MEL which I believe is derived from C+ I thought maybe it's just unique to C+ but

a quick search of JavaScript operators shows me that ++ is a valid operator for increment.

Why did this fail but work when I changed the last line to myValue = myValue+1; ?

//defines selected active composition in active project in active app

var myComp = app.project.activeItem;

//defines layer 1 of "myComp"

var myLayer = myComp.layer(1);

//defines value for conditional loop

var myCounter = 1;

//conditional loop

while(myCounter <= 10)

    {

      //duplicates active layer and defines it as

    var dupLayer = myLayer.duplicate();

    //rotates dupLayer additive 45 degrees

    dupLayer.rotation.setValue(45*myCounter);

    //increases reference value by one

    myCounter = myCounter++;

    }

Thanks,

Paul

~Gutterfish
TOPICS
Scripting

Views

790

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

Advisor , Jul 02, 2017 Jul 02, 2017

I think the example you show above is just using a second variable to try and explain what is going on - but is confusing matters further. Here's Google's explanation, which I think is helpful:

Increment (++) The increment operator increments (adds one to) its operand and returns a value. If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing. If used prefix with operator before operand (for example, ++x), then it returns the value afterincr

...

Votes

Translate

Translate
Advisor ,
Jul 01, 2017 Jul 01, 2017

Copy link to clipboard

Copied

I'm a scripting newby myself - so, don't bet the farm on this, but...

I think this is a case of the increment operator being available for use as a ''prefix' or 'postfix' operation.

Your original code: myValue = myValue++ is a postfix operation. MyValue is set to the value of myValue - then the value is incremented.

See the entry on this page:

Arithmetic operators - JavaScript | MDN

I don't think this is an issue when using the plain addition operator - hence the infinite loop of the original code versus the success of the +1 version.

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
People's Champ ,
Jul 01, 2017 Jul 01, 2017

Copy link to clipboard

Copied

Thanks Mike,

I saw the example you're talking about but I can't say i fully understand.  Meaning I can't think of real world scenario where I would do what that example did.  I simply want to increment the current value of variable(x) by one.  All of those examples define a second variable that I don't understand.

// Postfix
var x = 3;
y = x++; // y = 3, x = 4

// Prefix
var a = 2;
b = ++a; // a = 3, b = 3

I'm sure it's perfectly logical but I cannot see how it applies to what i am trying to do.

I just want to increment the var x by 1.

Would it be   x = ++x;  ​?

Or maybe it's just  x++;  ?

I suppose I'll just stick with  x=x+1;

~Gutterfish

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
Advisor ,
Jul 02, 2017 Jul 02, 2017

Copy link to clipboard

Copied

I think the example you show above is just using a second variable to try and explain what is going on - but is confusing matters further. Here's Google's explanation, which I think is helpful:

Increment (++) The increment operator increments (adds one to) its operand and returns a value. If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing. If used prefix with operator before operand (for example, ++x), then it returns the value afterincrementing.

In place of your code:

myCounter = myCounter++;

I think you could just use:

++myCounter

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
People's Champ ,
Jul 02, 2017 Jul 02, 2017

Copy link to clipboard

Copied

Thanks so much for taking the time to explain it, Mike.

If I may ask one more question...

If postfix returns the value before incrementation than why, in the first example, does x return a value of 4?

/ Postfix

var x = 3;

y = x++; // y = 3, x = 4

In fact in both examples the original variable gets incremented by one. 

~Gutterfish

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
Advisor ,
Jul 03, 2017 Jul 03, 2017

Copy link to clipboard

Copied

This thread needs an expert... : )

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
People's Champ ,
Jul 03, 2017 Jul 03, 2017

Copy link to clipboard

Copied

Mike, for the record your solution was correct.

It's just I just don't understand the example & that bugs me.

The answer will come eventually.

Thanks again for your help.

-Paul

~Gutterfish

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 ,
Jul 03, 2017 Jul 03, 2017

Copy link to clipboard

Copied

This is the order of operations in your two examples:

// post fix

x = 3;

y = x;

x = x+1;

// pre fix

a = 2;

a = a+1;

b = a;

I hope that helps.

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
People's Champ ,
Jul 03, 2017 Jul 03, 2017

Copy link to clipboard

Copied

LATEST

Thanks Dan.

Unfortunately that does not help.

Hopefully you'll indulge me once more.

I've been playing around in Toolkit trying to understand but it makes no sense to me.

////////////////////////////

Example 1:

1. var myNum = 3;

2. myNum;

3. //result 3

4. myNum;

5. //result 3

6. myNum++;

7. //result 3

8. myNum;

9. //result 4???

So on lines 2 & 4 I call "myNum" and it outputs "3".  (this makes sense to me)

On line 6 I use the operator postfix & it outputs "3" (this makes sense to me)

On line 8, when I call my variable again it outputs "4" (I do not understand why)

/////////////////////////////

Thanks for your time,

Paul

Wait....I think it just hit me.

When I use prefix it applies the increment "pre" execution of the statement.

When I use postfix it applies the increment "post" execution of the statement.

So the statement returns the original value of the variable & THEN applies the increment.

So the next time I call the variable the increment has been applied.

Is that right?

~Gutterfish

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