# After Effects: Increment a Variable over time

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?

• 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`.
– p2or
Sep 22, 2015 at 14:09
• 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? Sep 22, 2015 at 14:12
• 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?
– p2or
Sep 22, 2015 at 14:22
• @poor sounds interesting Sep 22, 2015 at 14:23

You can loop over the range of frames from the start of the comp to the present and calculate the value along the way.

``````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.