Core.yield: Difference between revisions
From GiderosMobile
No edit summary |
No edit summary |
||
| Line 26: | Line 26: | ||
'''state''': (number or boolean) number of seconds to yield or boolean to enable/disable auto yield<br/> | '''state''': (number or boolean) number of seconds to yield or boolean to enable/disable auto yield<br/> | ||
=== | === Examples === | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
local txt1_noads = TextField.new(nil, "no ads") | local txt1_noads = TextField.new(nil, "no ads") | ||
| Line 65: | Line 65: | ||
Core.asyncCall(scaleTextsAndBtn) | Core.asyncCall(scaleTextsAndBtn) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
'''A simple function that counts down from a given number''' | |||
<syntaxhighlight lang="lua"> | |||
local function startCountdown(seconds) | |||
Core.asyncCall(function() | |||
print("Timer started!") | |||
for i = seconds, 1, -1 do | |||
print("Time remaining: " .. i .. " seconds") | |||
-- Pause this specific task for exactly 1 second | |||
Core.yield(1) | |||
end | |||
print("Time's up! 🚀") | |||
end) | |||
end | |||
-- Start a 5-second countdown | |||
startCountdown(5) | |||
</syntaxhighlight> | |||
Key Takeaways from this Example | |||
*Sequential execution: The for loop looks like traditional synchronous code, but Core.yield(1) cleanly injects a delay between each iteration. | |||
*No game freezing: While this timer is counting down, your players can still touch buttons, sprites can animate, and physics will continue to update normally. | |||
=== See also === | === See also === | ||
Revision as of 09:44, 29 September 2026
Available since: Gideros 2016.06
Class: Core
Description
Yields the function running as background task.
Core.yield(state)
Core.yield([number of seconds to wait])
Core.yield(true) -- yield for a single frame and disable auto yield
Core.yield(false) -- don't yield now, but reenable autoyield
In Gideros, Core.yield(number) pauses the execution of the current asynchronous task or background thread for number seconds before resuming it. How it Works:
- Time-based Delay: Passing a number like 1 tells the engine to suspend the running coroutine/async function (started via Core.asyncCall or Core.asyncThread) and wait for that many seconds to pass in real time
- Non-blocking: While this specific task is paused, the main game loop and other engine processes continue running smoothly without freezing
Alternative Arguments:
- Core.yield(true) pauses execution until the very next frame
- Core.yield(number) pauses execution for the specified number of seconds
Parameters
state: (number or boolean) number of seconds to yield or boolean to enable/disable auto yield
Examples
local txt1_noads = TextField.new(nil, "no ads")
local txt2_plusincome = TextField.new(nil, "plus income")
local btn_OK = TextField.new(nil, "OK")
-- position
txt1_noads:setPosition(64, 64*1)
txt2_plusincome:setPosition(64, 64*3)
btn_OK:setPosition(64, 64*5)
-- scale
local FontScale = 4
local tempFontScale = 8
txt1_noads:setScale(FontScale)
txt2_plusincome:setScale(FontScale)
btn_OK:setScale(FontScale)
-- order
stage:addChild(txt1_noads)
stage:addChild(txt2_plusincome)
stage:addChild(btn_OK)
local function scaleTextsAndBtn()
Core.yield(2)
txt1_noads:setScale(tempFontScale)
Core.yield(1)
txt1_noads:setScale(FontScale)
Core.yield(1)
txt2_plusincome:setScale(tempFontScale)
Core.yield(1)
txt2_plusincome:setScale(FontScale)
Core.yield(1)
btn_OK:setScale(tempFontScale)
Core.yield(0.5)
btn_OK:setScale(FontScale)
end
Core.asyncCall(scaleTextsAndBtn)
A simple function that counts down from a given number
local function startCountdown(seconds)
Core.asyncCall(function()
print("Timer started!")
for i = seconds, 1, -1 do
print("Time remaining: " .. i .. " seconds")
-- Pause this specific task for exactly 1 second
Core.yield(1)
end
print("Time's up! 🚀")
end)
end
-- Start a 5-second countdown
startCountdown(5)
Key Takeaways from this Example
- Sequential execution: The for loop looks like traditional synchronous code, but Core.yield(1) cleanly injects a delay between each iteration.
- No game freezing: While this timer is counting down, your players can still touch buttons, sprites can animate, and physics will continue to update normally.
See also
- Core
- Core.asyncCall
- Core.asyncThread
- Core.class
- Core.enableAllocationTracking
- Core.fileLoad
- Core.fileSave
- Core.findReferences
- Core.frameStatistics
- Core.getScriptPath
- Core.profilerReport
- Core.profilerReset
- Core.profilerStart
- Core.profilerStop
- Core.random
- Core.randomSeed
- Core.setAutoYield
- Core.signal
- Core.stopping
- Core.yield
- Core.yieldable
- Core.yieldlock