self:sleep

Shared

Suspends the thread for the specified duration, then resumes automatically


Syntax

local status = self:sleep(
    duration
)

This function must be called from within a running thread. It cannot be used outside a thread context, while already sleeping, or while awaiting a util.promise.

  • Available — the thread yields for the given duration and resumes automatically when the timer expires.
  • Outside a thread — the call is a no-op and returns false.
  • Sleeping or awaiting — the call is ignored and returns false.

Parameters

TypeNameDescription
intdurationSleep duration in milliseconds, must be >= 0

Returns

TypeNameDescription
boolstatustrue on successful execution, false otherwise

Examples

Sleep a thread for 5 seconds then continue
local entity = util.thread.create(function(self)
    core.engine.print("info", "Sleeping for 5 seconds")
    self:sleep(5000)
    core.engine.print("info", "Awake now")
end)

entity:resume()
Sleep repeatedly inside a loop
local entity = util.thread.create(function(self)
    for i = 1, 3 do
        core.engine.print("info", "Tick", i)
        self:sleep(1000)
    end
end)

entity:resume()

On this page