Cospaces Script

Hi everybody,
I want to use wait or sleep in the script, which statement do I declare? The Time.schedule function only suspends executing the function by the start time.

1 Like

Time.schedule is the only way I know how to do it. You can put Time.schedule inside the function of Time.schedule’s return for more paused timing. I haven’t tried it before, but you could probably use Time.currentTime + (seconds needed to pause) somehow with schedule?

1 Like

Thanks for help but i don’t know how to use Time.currentTime in loop. If I use this way the program will be slow, the device will hang in about 1 minute after playing. How to fix it? I can’t replace “delay” with Time.currentTime
//Create item auto scale
var rocKet=Scene.createItem(“LP_Rocket”,0,0,0);
//Create var delay
var delay=0
//Loop endless with delay = 0
Time.scheduleRepeating(() => {
//Auto scale lager repeat 100 times.
for (let i = 0; i <= 100; i++) {
Time.schedule(() => {
rocKet.transform.scale +=0.001;
}, delay+=0.01);
};
//Auto scale smaller repeat 100 times.
for (let i = 0; i <= 100; i++) {
Time.schedule(() => {
rocKet.transform.scale -=0.001;
}, delay+=0.01);
};
},0);

In your example you have repeating loop where you create new scheduled loops. That leads to lots of unplanned calls and unexpected behaviour.

You could rewrite it in the following way (I have added two functions to do something repeatedly and then two function with normal flow):

//Create item auto scale
var rocKet=Scene.createItem('LP_Rocket',0,0,0)
//Create var delay
var delay=0

//repeated variable to control number of runs
let repeated = 0
let timer = Time.scheduleRepeating (()=>{
    rocKet.transform.scale +=0.001;
    repeated += 1
    if (repeated >= 100) {
        //cancel current cycle and run next function
        timer.dispose()
        rocketGoSmaller()
    }
}, 0.01)

function rocketGoSmaller() {
    repeated = 0
    timer = Time.scheduleRepeating (()=>{
        rocKet.transform.scale -=0.001;
        repeated += 1
        if (repeated >= 100) {
            timer.dispose()
            say()
        }
    }, 0.01)
}

function say () {
    Scene.getItem('Casual girl').speech = "Scale ended. Wait for launch"
    Time.schedule(rocketFly, 3);
}

function rocketFly() {
    rocKet.transition.moveTo(new Vector3(0,0,10), 3)
}      

You can explore this code by remixing the following sample:

Hope that helps

3 Likes

I’m done. Thank you very much

Many thanks for sharing this project!! Now to dive into the code from it!