Jump to content

Rick

Members
  • Posts

    7,936
  • Joined

  • Last visited

Blog Comments posted by Rick

  1. Not really. It continues when you tell it to continue not when the called function returns. You tell it to continue with a call to coroutine.resume() passing in the coroutine "handle". Coroutines also store all it's variable data the function had when you left it so when it continues it can call them back on the stack for you to use again once inside the coroutine function again.

  2. Yeah, it can get confusing when working with, but thankfully using it once a system is setup is straight forward. I'll explain:

     

    Lua has these things called coroutines. These allow you to stop execution of a function and continue it at a later time (that time is something you define). There are a few functions tied to them, namely:

    routine = coroutine.create(func) (ties a function to this coroutine that is returned)

    coroutine.yield() (jumps out of the coroutine function are in. when we resume it'll start back up after this statement)

    coroutine.resume(routine, args_to_function) (starts/resumes a coroutine function you defined)

     

    These are the building blocks for how coroutines work. If I want to be able to stop execution of a function I must create a coroutine for that function.

     

    function Test(name)
    end
    
    -- associates the coroutine (cr) to the function (Test)
    cr = coroutine.create(Test)
    

     

    When I have a coroutine named cr that controls the function execution. I can start the function with:

    coroutine.resume(cr, "Rick")

     

    I can also step out of the function with:

    coroutine.yield()

     

    So to show a complete small & simple example. You can run this script using lua.exe.

     

    function Test(name)
      print(name)
      coroutine.yield()  -- This will jump out of the Test() function and continue to do the while loop below
      print("is cool")
    end
    
    cr = coroutine.create(Test)   -- create a coroutine tied to the function Test
    coroutine.resume(cr, "Rick")  -- This actually starts the function Test passing in "Rick" as the parameter
    
    -- will loop for 5 seconds
    i = os.time() + 5
    while os.time() < i do
           -- This is here to show that it truly did leave the Test function.
           -- With this here you can't see that the Test function prints "Rick" so if you comment this print out
           -- you can see it prints "Rick" out on the console.
    print("Looping") 
    end
    
    -- this will resume the Test function and you'll see it starts off where it left off, after the yield
    -- inside the Test function. At this point it really doesn't matter what value I send it as the 2nd parameter
    -- which is the parameter to the Test function, because it'll use whatever I sent it the first time. Which is
    -- kind of a bummer because I thought it would be pretty neat if I could pass different values on every resume
    -- but it's not a big deal really
    coroutine.resume(cr, "Rick")
    

×
×
  • Create New...