Lua threading?aa


#1

I have taken some intro courses to coding and want to learn lua for a game that I am playing.
The code I will be referencing is from forest jogging and is as follows:

--Your pet can use pet:moveXY()

local onSpawn = function(event) 
    while true do
        --First, move to the left X mark:
        pet:moveXY(9, 24)
        --Next, move to the top X mark:
        pet:moveXY(30, 43)
        --Move your pet to the right X mark:
        pet:moveXY(51, 24)
        --Move your pet to the bottom X mark:
        pet:moveXY(30, 5)
    end
end

--Use pet:on() to handle the "spawn" event with onSpawn


--Cheer on your pet!
--Don't remove the commands below.
pet:on("spawn",onSpawn)
while true do
    hero:say("Good dog!")
    hero:say("You can do it!")
    hero:say("Run Run Run!")
    hero:say("Almost!")
    hero:say("One more lap!")
end

to my understanding on other languages everything in onSpawn should run an infinite time without ever saying any hero commands. Why is it that it alternates saying one pet thing then one hero thing? Is it sometime of threading, or coroutine, that I just read about? Any explanation of how this works would be appreciated. Thank you in advance