Skip to main content
Participant
February 13, 2024
Question

making a random value everytime i check a checkbox

  • February 13, 2024
  • 3 replies
  • 601 views

Hi,

I'm trying to make my slider control have a random value when you check the checkbox control and when you uncheck the value to use is that of the slider control.

 

 

 

Here is one of the code that i try but my slider doesn't change anything anymore only the seed random (the one suppose to choose a number randomly) 

 

seedRandom(index, timeless = true);
var randomizedValue = random(0, thisComp.layer("Control_Snowflakes").effect("Change_Shape_MID")("Slider"));
if(thisComp.layer("Control_Snowflakes").effect("Randomizer")("Checkbox") == 1){
randomizedValue
}else{
thisComp.layer("Control_Snowflakes").effect("Change_Shape_MID")("Slider")
}


var storedRandomValue = thisComp.layer("Control_Snowflakes").effect("Seed Random")("Slider");
if(thisComp.layer("Control_Snowflakes").effect("Randomizer")("Checkbox") == 1){
seedRandom(index, timeless = true);
var newRandomValue = random(0, thisComp.layer("Control_Snowflakes").effect("Change_Shape_MID")("Slider"));
value + newRandomValue - storedRandomValue;
newRandomValue;
}else{
storedRandomValue;
}

 

Thank you for helping me 

 

This topic has been closed for replies.

3 replies

Mylenium
Legend
February 13, 2024

There are no real buttons in expressions and I think that's where your whole plan falls apart. You seem to assume that checkboxes work this way and controls associated with them somehow store the various states they are in. This is never going to work, no matter whether it's animated or a static control. If at all I would imagine you could write some complex auxiliary expression that e.g. populates an array or stores values as strings in a text layer do you have some way of not having to recalculating everything all the time, but even do you would need another expression to fetch and convert the values back as well as having code to check whether a specific random seed isn't used twice. Once again: None of this makes any sense from the "How AE expressions work" POV and I'm pretty sure even if you could get it to work natively in AE it would still break with Essential Graphics or Dynamic Link. Sorry, but this just feels wrong on so many levels. So from where I sit a much simpler approach is needed like having two or three sliders whose combined value drives the random seed or such. This would sufficiently reduce the probability of producing the same results and you could "harden" it further by tying the seeds to the effects numerical induced as part of the formula.

 

Mylenium 

Participant
February 13, 2024

I know you are right that my plan will fail, I know the checkbox doesn't work that way for this utility. But I also want to try what is offered to me, who perhaps knows that it will lead me to an interesting result. but at least through this discussion I would have learned a lot.
Thank you for your feedback

Dan Ebberts
Community Expert
Community Expert
February 13, 2024

Not sure exactly what you're after, but it's probably going to be close to this:

cb = effect("Randomizer")("Checkbox");
val = value;
if (cb.numKeys > 0){
  for (i = cb.numKeys; i > 0; i--){
    if (cb.key(i).time <= time){
      if (cb.key(i).value){
        seedRandom(i,true);
        val = random(value);
      }
      break;
    }
  }
}
val
Participant
February 13, 2024

I'm trying to create a snowflake preset and I'm trying to add a random function which will make it possible to never have the same snowflake with each time differents values and i don't want its value to change with the timeline. If we uncheck the random option, people will be able to modify the snowflake with the slider controls.
I've tried several things but I often come back to the same result and I don't really know how to go about it.

Here a screenshot : 

Dan Ebberts
Community Expert
Community Expert
February 13, 2024

I don't think the expression I posted is a perfect fit, but I think the technique should work. That is, the expression checks to find the state of the most recent checkbox keyframe (most recent = at or before the current time). If that keyframe turns the checkbox on, it uses that keyframe's index for the random seed and gets a random value, otherwise it uses the slider's actual value. That way the random value changes at each checkbox "on" keyframe.

Mylenium
Legend
February 13, 2024

You need to run it in a loop and evaluate it for every frame. AE does not store persistent variables and values so despite your convoluted code the result is basically the same all the time. That and of course it makes no sense in the first place. If a slider is already involved, you'd simply keyframe that because there is no way to carry your checkbox states, either, other than e.g. counting the keyframes on it. Again, your approach just doesn't make sense.

 

Mylenium

Participant
February 13, 2024

Thank you for your advice, i didn't know about that.  So just to know is it possible to create a random that changes the values ​​of the slider controls without changing them in the timeline? either by expression or by a script (like the MindStorm script from AECartographer) or as you told me it cannot be done