ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Learning Extendscript / Keep getting NaN

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Anthony Hairgrow
Learning Extendscript / Keep getting NaN
on Jan 12, 2017 at 6:58:49 pm

Hey there. I'm brand new to the Creative Cow scene as well as Javascript so I apologize for what may seem like a simple question. I'm teaching myself Javascript so I'm having problems understanding how to implement functions. So far I understand the very basics, so now I'm creating a test script to understand the usage and implementation of functions.

Currently, the problem I'm running into is that I keep getting NAN in my alert. I know it means "Not A Number", which is crazy, because if I put the alert inside the testFunction it clearly say "2". But I wanted to put the alert for the results of the testFunction inside of another function so I could see how the value being returned from testFunction could be used in other parts of the code. This test is also a way for me to understand how to write multiple functions, have them execute in a specific order, and pass the data around. I'm currently reading the AE Scripting Guides, looking through several script examples, but it seems I can't find the answer for something so trivial. Any help or explanation with this is greatly appreciated.

var myComp = app.project.activeItem;
if (myComp == null || (myComp instanceof CompItem) == false ){
alert("Please select a Composition");
}else{
testFunction(1,1);
alertReturn();
};
function testFunction(a,b){
var results = a + b;
return(results);
};
function alertReturn(){
var a = testFunction();
alert(a);
};


Return to posts index

Dan Ebberts
Re: Learning Extendscript / Keep getting NaN
on Jan 12, 2017 at 7:19:11 pm

For one thing, when your alertReturn function calls your testFunction, it doesn't pass it any parameters to work with, so testFunction won't be able to come up with a number for results.

Also, your return statement doesn't need the parentheses (although I don't think it hurts anything), just:

return results;


Dan



Return to posts index

Anthony Hairgrow
Re: Learning Extendscript / Keep getting NaN
on Jan 12, 2017 at 8:07:50 pm

Thanks for your response Dan, but please forgive me for my lack of understanding. After reading your comment, I've tried a few different ways and now I'm getting "undefined". Are you saying the parameters that need to be passed to testFunction(), should be passed in by alertReturn(). I saw a Javascript example, non AE related, from like 2013, where the returned value from one function could be used in another.

Here's the example:

function firstFunction()
{
//Do something;
return "testing 123";
}

function testCase()
{
var test=firstFunction();
alert(test);
}

This is basically the format I was following. I thought I could pass the parameters, testFunction(1,1), at line 5 and then everything would proceed as expected. I was thinking the order of operation would be:

1. testFunction() is called at line 5 with parameters being passed to it.
2. testFunction does it calculation
3. testFunction returns the result
4. alertReturn() is then called right after (at line 6)
5. testFunction is assigned as the value for variable a, in alertReturn, essentially making its value the returned value.
6. alert variable a.

I'm still lost.


Return to posts index


Dan Ebberts
Re: Learning Extendscript / Keep getting NaN
on Jan 12, 2017 at 8:23:24 pm

The difference is that in your JavaScript example, neither of the functions require any parameters.

>1. testFunction() is called at line 5 with parameters being passed to it.

Yes, but the calculated value (2) isn't stored anywhere.

>5. testFunction is assigned as the value for variable a, in alertReturn, essentially making its value the returned value.

I think this is where you're going astray. The value previously calculated by testFunction() no longer exists. alertReturn() calls test function again, but passes it no parameters.

Dan



Return to posts index

Anthony Hairgrow
Re: Learning Extendscript / Keep getting NaN
on Jan 12, 2017 at 8:28:33 pm

That's funny. As soon as I got it, thats when you posted your response. Thanks for explanation. There's still lots for me to understand.


Return to posts index

Anthony Hairgrow
Re: Learning Extendscript / Keep getting NaN
on Jan 12, 2017 at 8:25:14 pm

Never mind. I tried what you said again and got it to work. I guess calling and passing information into the function first doesn't work if it's being called again from somewhere else.

var myComp = app.project.activeItem;
if (myComp == null || (myComp instanceof CompItem) == false ){
alert("Please select a Composition");
}else{
alertReturn();
};
function testFunction(a,b){
var results = a + b;
return results;
};
function alertReturn(){
var c = testFunction(1,1);
alert(c);
};


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]