Answers.Body: <p>You can loop over the range of frames from the start of the comp to the present and calculate the value along the way.</p>
  
  <pre class="lang-js prettyprint-override"><code>function myFuncf(theTime){
   //whatever the function you're using to create the value, e.g.
   t=theTime + s.valueAtTime(theTime); //s is the slider
   return t
  }
  
  firstFrame = timeToFrames(inPoint); //if you want it to calculate
                                      //from the beginning of the comp
                                      //change this to 0, or maybe 1?
                                      //I'd check but I'm rendering ATM
  
  mytime=0; //initialise the mytime variable
  
  currentFrame = timeToFrames(time);
  for (int i=firstFrame; i &lt;= currentFrame; i++){
    mytime += myFunc(framesToTime(i)); //need to convert frames back into seconds
  }
  </code></pre>
  
  <p>after the loop myTime should be the sum of the results of function myFunc from all the previous frames up to and including the present one (I'm not 100% sure if the loop should end when <code>i &lt; cf</code> or <code>i &lt;= cf</code>, you might want to test).</p>
  
  <p><strong>note</strong> first time I posted this there was a mistake. Where I called the function I was giving it frames (from <code>i</code> the loop variable), but it uses <code>valueAtTime</code> which expects time in seconds, so you need to convert: <code>mytime += myFunc(framesToTime(i));</code></p>
  
  <p>If you have a long comp or this is applied to lots of layers, this expression may become slow to evaluate and cause lagginess. If it becomes a problem convert the expression to keyframes.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-10T05:08:49.557Z
Answers.LastActivityDate: 2017-03-31T00:35:10.437Z
Answers.LastEditDate: 2017-03-31T00:35:10.437Z
Answers.UserDisplayName: stib
Answers.Body: <p>If anyone is still looking for a solution that wont blow up computation times, I found this workaround using environment variables which will store values outside of the script context.</p>
  <p>The if statement will define the Env-Variable at frame 0 and afterwards increase and use the variable each frame. Note that <code>x</code> could be any value. In my case it was an audio amplitude effect slider value.</p>
  <pre><code>if ($.myEnvVariable == null){
      $.myEnvVariable = 0
  } 
  // x = value from slider
  $.myEnvVariable = $.myEnvVariable + x
  $.myEnvVariable
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-09-10T19:42:56.223Z
Answers.LastActivityDate: 2022-09-10T19:45:02.740Z
Answers.LastEditDate: 2022-09-10T19:45:02.740Z
Answers.UserDisplayName: DieseKartoffel
Body: <p>I have following problem:
  I am trying to tweak the speed of a Math.sin function. Therefore I need a variable that incerements over time with a combination of a keyframed speed variable.</p>
  
  <p>Sounds complicated, I know. </p>
  
  <p>Let's say I am using the global time variable and convert it with the timeToFrames() function I get an integer value for each frame.</p>
  
  <pre><code>myTime = timeToFrames(time);
  </code></pre>
  
  <p>This leads me to a value of 1 for Frame 1, a value of 2 for Frame 2, and so on.
  But what if I want to manipulate the increment. For example:</p>
  
  <pre><code>//speed (gets tweened from a certain value range,for example: 10 - 1)
  s = (some connected slider here)
  
  myTime += s;
  </code></pre>
  
  <p>If I have a speed of 10 at Frame 1 I would get a value of 11. If I have a speed of 8 at Frame 2, I would expect a value of 11 + 2 + 8 = 21.</p>
  
  <p>What I get is only 2 + 8;</p>
  
  <p>So I need to store the value at every frame. Is this possible? </p>
  
  <p>Please help.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-09-22T14:09:54.373Z
Comments.Text: You can calculate the value by creating a new variable `var x = 10;` or dynamic: `var x = value` and use the addition assignment operator `+=`  (`x +=  5`), but this is just the shorthand of `x = x + y`.
Comments.UserDisplayName: p2or
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-09-22T14:12:41.507Z
Comments.Text: yeah thats what I am doing at the moment. The problem is, if I add 10 to x at frame 1 I get for example 11. On frame to it should automatically incremnt by 1 again + the value of my choice. But on frame 2 after effects already forgot about my 11 and it simply goes on wiht a value of 2 plus my variable. You know what I mean?
Comments.UserDisplayName: Marten Zander
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-09-22T14:22:01.073Z
Comments.Text: Can't test it at the moment... but what about creating an additional `var lastframe = yourCalcValue;` *or* calculate both values from the current and the last frame at once?
Comments.UserDisplayName: p2or
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-09-22T14:23:13.530Z
Comments.Text: @poor sounds interesting
Comments.UserDisplayName: Marten Zander
Tags: <after-effects><after-effects-expressions>
Title: After Effects: Increment a Variable over time
UserDisplayName: Marten Zander
ThreadText: # After Effects: Increment a Variable over time
  
  (Asked by: username_0 on 2015-09-22)
  
  I have following problem:
  I am trying to tweak the speed of a Math.sin function. Therefore I need a variable that incerements over time with a combination of a keyframed speed variable.
  
  Sounds complicated, I know.
  
  Let's say I am using the global time variable and convert it with the timeToFrames() function I get an integer value for each frame.
  
  ```
  myTime = timeToFrames(time);
  
  ```
  
  This leads me to a value of 1 for Frame 1, a value of 2 for Frame 2, and so on.
  But what if I want to manipulate the increment. For example:
  
  ```
  //speed (gets tweened from a certain value range,for example: 10 - 1)
  s = (some connected slider here)
  
  myTime += s;
  
  ```
  
  If I have a speed of 10 at Frame 1 I would get a value of 11. If I have a speed of 8 at Frame 2, I would expect a value of 11 + 2 + 8 = 21.
  
  What I get is only 2 + 8;
  
  So I need to store the value at every frame. Is this possible?
  
  Please help.
  
  **Tags:** after-effects, after-effects-expressions
  
  > **Comments:**
  > **username_1**: You can calculate the value by creating a new variable `var x = 10;` or dynamic: `var x = value` and use the addition assignment operator `+=`  (`x +=  5`), but this is just the shorthand of `x = x + y`.  
  > **username_0**: yeah thats what I am doing at the moment. The problem is, if I add 10 to x at frame 1 I get for example 11. On frame to it should automatically incremnt by 1 again + the value of my choice. But on frame 2 after effects already forgot about my 11 and it simply goes on wiht a value of 2 plus my variable. You know what I mean?  
  > **username_1**: Can't test it at the moment... but what about creating an additional `var lastframe = yourCalcValue;` *or* calculate both values from the current and the last frame at once?  
  > **username_0**: @poor sounds interesting  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2015-11-10. Score: 2
  
  You can loop over the range of frames from the start of the comp to the present and calculate the value along the way.
  
  ```js
  function myFuncf(theTime){
   //whatever the function you're using to create the value, e.g.
   t=theTime + s.valueAtTime(theTime); //s is the slider
   return t
  }
  
  firstFrame = timeToFrames(inPoint); //if you want it to calculate
                                      //from the beginning of the comp
                                      //change this to 0, or maybe 1?
                                      //I'd check but I'm rendering ATM
  
  mytime=0; //initialise the mytime variable
  
  currentFrame = timeToFrames(time);
  for (int i=firstFrame; i <= currentFrame; i++){
    mytime += myFunc(framesToTime(i)); //need to convert frames back into seconds
  }
  
  ```
  
  after the loop myTime should be the sum of the results of function myFunc from all the previous frames up to and including the present one (I'm not 100% sure if the loop should end when `i < cf` or `i <= cf`, you might want to test).
  
  **note** first time I posted this there was a mistake. Where I called the function I was giving it frames (from `i` the loop variable), but it uses `valueAtTime` which expects time in seconds, so you need to convert: `mytime += myFunc(framesToTime(i));`
  
  If you have a long comp or this is applied to lots of layers, this expression may become slow to evaluate and cause lagginess. If it becomes a problem convert the expression to keyframes.
  
  
  ---
  
  ### Answer by username_3 on 2022-09-10. Score: 0
  
  If anyone is still looking for a solution that wont blow up computation times, I found this workaround using environment variables which will store values outside of the script context.
  
  The if statement will define the Env-Variable at frame 0 and afterwards increase and use the variable each frame. Note that `x` could be any value. In my case it was an audio amplitude effect slider value.
  
  ```
  if ($.myEnvVariable == null){
      $.myEnvVariable = 0
  } 
  // x = value from slider
  $.myEnvVariable = $.myEnvVariable + x
  $.myEnvVariable
  
  ```
  
  
  ---
  
  