Circle obj visibility

Hi guys! Im trying to randomize value in array of obj
Typicaly its work, But, can get how to disable previous obj visibility

function start() {
  running = true

  var delayedEvent = script.createEvent('DelayedCallbackEvent')
  delayedEvent.bind(function() {
    var rand = Math.floor(Math.random() * all);
    script.randomImageMaterial[rand].enabled = true;

    if (!stop) {
      delayedEvent.reset(1)
      print('random ' + rand);
     }
  })
  delayedEvent.reset(0)
}

You can either track the previous index outside of the event callback, and use that to disable the previous object, or you can use brute force like this:

script.randomImageMaterial.forEach(function(mat){mat.enabled = false})

1 Like

but, how can I use the random value if its inside DelayedCallbackEvent
I’ve try, but has error

is this disable all object before enable new one?

dots work somehow =(

did I do something wrong?

function start() {
  running = true

  var delayedEvent = script.createEvent('DelayedCallbackEvent')
  delayedEvent.bind(function() {
    var rand = Math.floor(Math.random() * all);
    script.randomImageMaterial[rand].enabled = true;
    script.randomImageMaterial.forEach(function(mat){mat.enabled = false})

    if (!stop) {
      delayedEvent.reset(1)
      print('random ' + rand);
     }
  })
  delayedEvent.reset(0)
}

That line will disable all of them, so you should do that before enabling the new one :slight_smile:

it must to be in callback function? cuz i try and its doesn’t help

I think you just need to switch the order of them. Disable all, then enable the new one.

script.randomImageMaterial.forEach(function(mat){mat.enabled = false})
script.randomImageMaterial[rand].enabled = true;

nope, doesn’t work =(

im trying to implement it on another way

var stop = false
var running = false
var n = script.randomImageMaterial.length;
script.randomImageMaterial.enabled = false;
var activeIndex = 0;
script.HairEnable.enabled = false;

function start() {
  running = true
  script.HairEnable.enabled = true;
  var delayedEvent = script.createEvent('DelayedCallbackEvent')
  delayedEvent.bind(function() {

var rand = Math.floor(Math.random() * n);
        
var randomIndex = rand
script.randomImageMaterial[activeIndex].enabled = false
        while (randomIndex === activeIndex){ 
                randomIndex = rand        
        }
activeIndex = randomIndex
script.randomImageMaterial[activeIndex].enabled = true

    
    if (!stop) {
      delayedEvent.reset(0.5)
      print('random ' + activeIndex + ' ' + randomIndex);
               
     }
  })
  delayedEvent.reset(0)
}

but it work a while (5-6 itteration) and then stop to work and errored on expression '‘while’

may be is something wrong with the whole script

//@input SceneObject[] randomImageMaterial
//@input SceneObject HairEnable
//@input float changeDelay = 0.005 {"widget":"slider", "min":0.0, "max":0.4, "step":0.005}
//@input float hintShowTime = 1 {"widget":"slider", "min":0.0, "max":3, "step":0.1}
//@input string stopTriggerName
//@input string startTriggerName

var stop = false
var running = false
var n = script.randomImageMaterial.length;
//script.randomImageMaterial.enabled = false;
var activeIndex = 0;
script.HairEnable.enabled = false;

function start() {
 running = true
 script.HairEnable.enabled = true;
 var delayedEvent = script.createEvent('DelayedCallbackEvent')
 delayedEvent.bind(function() {

var rand = Math.floor(Math.random() * n);
       
var randomIndex = rand
script.randomImageMaterial[activeIndex].enabled = false
       while (randomIndex === activeIndex){ 
               randomIndex = rand        
       }
activeIndex = randomIndex
script.randomImageMaterial[activeIndex].enabled = true

     if (!stop) {
     delayedEvent.reset(0.5)
     print('random ' + activeIndex + ' ' + randomIndex);
              
    }
 })
 delayedEvent.reset(0)
}

//start(script.changeDelay)


function onStopTrigger() {
 stop = true
 running = false

}

global.behaviorSystem.addCustomTriggerResponse(
 script.stopTriggerName,
 onStopTrigger
)
function onStartTrigger() {
 stop = false

 if (!running) {
   start(script.randomImageMaterial, script.randomImageMaterial, script.changeDelay)
 }
}
global.behaviorSystem.addCustomTriggerResponse(
 script.startTriggerName,
 onStartTrigger
)

logger says

I’m not sure what you are trying to do with the while loop, but you already set the random index to rand, so setting it to rand again won’t change anything. Currently, if you get randomIndex === activeIndex, it will get stuck in the while loop. Do you mean to re-randomize in the while loop?

Generally, I’d suggest adding comments to your scripts. They make it easier to understand the goal of the code. Also, adding print statements is a good way to understand what the script is actually doing, and why it’s not behaving like you think it should

I’m trying to collect generated random numbers, then when a new random number is coming disable the previous object with the previous number and enable it with the new.

Yeah, so in that case you should re-randomize when you check if the active index is the same as the new one.

var randomIndex = Math.floor(Math.random() * n);
script.randomImageMaterial[activeIndex].enabled = false
while (randomIndex === activeIndex){ 
  randomIndex = Math.floor(Math.random() * n);
}
1 Like