ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Save previous variable value - expression

COW Forums : Adobe After Effects

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Patrik Fröhler
Save previous variable value - expression
on May 18, 2015 at 8:10:48 pm
Last Edited By Patrik Fröhler on May 18, 2015 at 8:38:21 pm

Hi, how can I store a previous value in a variable, the problem I have If I do: "var vPre = 0;" then for every frame it will re-defines it to 0, I would want to make is so if vRan is bigger than vPre then make vPre the value of vRan, so if it works vPre should only be able to get bigger over time.

And I think If i define the variable inside an "if statement" it wont be a global variable?

Alternatively is it possible to write the result of a variable on to a expression control so It can be used to compare against next frame, when i try I get "it is read-only"

Hope it makes sense.

Version 1:

var vPre;
var vRan = (random()*100);

if (vPre == undefined){ vPre = vRan};
if (vRan > vPre ){ vPre = vRan};

vPre;


Version 2:

var vRan = (random()*100);

if (vPre == undefined){ var vPre = 0};
if (vRan > vPre ){ vPre = vRan};

vPre;


Thanks.


Return to posts index

Walter Soyka
Re: Save previous variable value - expression
on May 18, 2015 at 8:33:48 pm

Expressions are stateless. They do not hold a value going from one frame to the next. You can think of them as functions that take time as an input and return a single value. They are evaluated anew each frame; they are not programs that run serially from the first frame onward.

If you want access to values that would have been computed on previous frames, you must re-compute them on the current frame.

Walter Soyka
Designer & Mad Scientist at Keen Live [link]
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
@keenlive [twitter]   |   RenderBreak [blog]   |   Profile [LinkedIn]


Return to posts index

Patrik Fröhler
Re: Save previous variable value - expression
on May 18, 2015 at 8:40:58 pm

Ok, would it be possible to write the result of a variable on to a expression control so It can be used to compare against next frame, (when i try I get "it is read-only")


Return to posts index


Walter Soyka
Re: Save previous variable value - expression
on May 18, 2015 at 8:45:48 pm

[Patrik Fröhler] "Ok, would it be possible to write the result of a variable on to a expression control so It can be used to compare against next frame, (when i try I get "it is read-only")"

You don't write to an expression control from another expression. Expressions may only return a value to the property which they are applied to. You'd have to put an expression on the control itself, but of course, this expression is evaluated anew each frame...

Since you're familiar with scripting, you might consider writing a script that moves forward in time, one frame at at time, evaluating your logic and storing the results in keyframes.

Otherwise, your expression will have to compute all the values for every frame which preceded it before it can return a value for the current frame.

Walter Soyka
Designer & Mad Scientist at Keen Live [link]
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
@keenlive [twitter]   |   RenderBreak [blog]   |   Profile [LinkedIn]


Return to posts index

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