#tick delay in for/while loops

1 messages · Page 1 of 1 (latest)

stray path
#

By triggering via /scriptcommand, I wanted a loop to run a function, briefly pause, and then continue the loop again.

while (pNumber < sPspawn.length) {
                pNumber++
                projectileSpread(sourceEntity, sbEntity, pNumber, sPspawn, view, radius, speed)
                system.runTimeout(() => { }, 5)
            }```



Instead, the code works, but goes through the loop instantly, as if the timeout isn't even there.
How do you put a delay inbetween each loop?
wooden bloom
#

while (...) {
...

await new Promise(res => {
    system.runTimeout( res, 5 )
})

}

stray path
quaint oak
# stray path `await` is a new term for me, how does it work?

await is waiting for the line to complete its task before going to next line but you need to add an async keyword before the function keyword

something like

async function test(){
  await new Promise(() => {})
}

or

world.beforeEvents.whateverEvent.subscribe(async (v) => {})
wooden bloom
stray path
#

ah. I haven't gotten to learn the asynchronous stuff yet, no wonder I'm lost

quaint oak
stray path
#

seeing an example might help, yeah

quaint oak
#

usage

export async function tp(player, pos, name, dimension = "overworld") {
  const dis = player.onScreenDisplay
  const cam = player.camera
  cam.clear
  dis.setHudVisibility(0)
  dis.setTitle("§aTeleporting...", { stayDuration: 100, fadeInDuration: 2, fadeOutDuration: 4, subtitle: `§eTo ${name}`})
  cam.fade({ fadeTime: { fadeInTime: 0.5, holdTime: 1.5, fadeOutTime: 0.5}})
  await sleep(40) //sleep for 2 seconds
  player.teleport(pos, { dimension: world.getDimension(dimension) })
  dis.setTitle("§aTeleporting", { stayDuration: 20, fadeInDuration: 0, fadeOutDuration: 40, subtitle: `§eTo ${name}`})
  dis.setHudVisibility(1)
  player.playSound("random.levelup")
}

quaint oak
quaint oak
stray path
#

I'll try to see what I can do, thanks

wooden bloom
quaint oak
#
async function sleep(tick = 20) {
  return new Promise((resolve) => {
    system.runTimeout(() => resolve, tick)
  })
}
stray path
#

What exactly am I doing wrong here?

async function test() {
for (let i = 0; i < 5; i++) {
        // code
        return new Promise((resolve) => {
            system.runTimeout(resolve, 2)
        })
    }
}```
humble depot
wooden bloom
#

^

quaint oak
#

yoo it is:v