ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

maintain the value of Position after updating it with random()

FAQ   •   VIEW ALL
 maintain the value of Position after updating it with random() on Oct 31, 2018 at 5:11:12 am

Hi all! I'm having a bit of a problem in achieving the following - could anyone help?

I'm trying to move the position of a solid by generating a random integer number between 0 and 4, then adding it on top of the previous position values; when another number is generated, it gets added on top of the previous sum again, and so on and on. So the result would be the Position value growing, and the solid layer moving away from its initial position at a random distances at a time. However, i can't seem to keep the generated random() added on the Position - it always goes back to the initial value and loses the generated number, so the solid is only 'wiggling' around the initial position.

My code is underneath. Any input is appreciated! Thank you very much!

```x=0; y=0; valueOut = [0,0]; if (effect("Checkbox Control")("Checkbox")==1) { if (Math.round(random(0,2))==0){ x=random(0, 4); y=random(0,4); valueOut = valueAtTime(time-1/25)+[x,y]; } else { valueOut = valueAtTime(time-1/25); } } valueOut ```

 Re: maintain the value of Position after updating it with random()on Oct 31, 2018 at 6:18:32 pm

Expressions have no access to their own previous results, so when an expression use value or valueAtTime() to reference the property hosting the expression, it will always get the pre-expression result. To work around that, your expression has to re-create everything that has happened in the past by looping through time. Your expression would look something like this (I'm assuming you want the random updating to happen every two frames):

``` s0 = Math.floor(timeToFrames(inPoint)/2); s = Math.floor(timeToFrames(time)/2); xy = [0,0]; while (s0 <= s){ seedRandom(s0,true); xy += random([4,4]) s0++; } value + xy ```

Dan