Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Save previous variable value – expression

  • Save previous variable value – expression

    Posted by Patrik Fröhler on May 18, 2015 at 8:10 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.

    Walter Soyka replied 11 years, 3 months ago 2 Members · 3 Replies
  • 3 Replies
  • Walter Soyka

    May 18, 2015 at 8:33 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   |   RenderBreak [blog]   |   Profile [LinkedIn]

  • Patrik Fröhler

    May 18, 2015 at 8:40 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”)

  • Walter Soyka

    May 18, 2015 at 8:45 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   |   RenderBreak [blog]   |   Profile [LinkedIn]

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy