The yield function¶
| Syntax: | yield() |
|---|---|
| Returns: | nil |
Suspends the execution of the script until the next frame, producing the same result
as a call to sleep(0).
This is useful as Iris Engine can’t redraw the screen and execute a Lua script at the same time, so having a loop in your script that doesn’t call any blocking functions will make the game appear to be frozen.
With the yield function you can tell the engine to update the state of the objects and redraw the screen, allowing you to extend the game loop with your own custom code. This is most useful for custom minigames and such.
It can also be used to hide the framerate drops that occur when loading new images by forcing the game to wait one frame before starting any animations.
Examples¶
-- Create a sprite
ball = Sprite.new("ball.png", 1)
ball:show()
while true do
-- Place the sprite in a random position
ball:setPosition(math.random(10, 50), math.random(10, 50))
-- Let the engine render the screen
yield()
end