lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> The topic might sound strange, but what i want to do, is the following :
> I have a luascript like the following :
>
> function blua()
> print("x");
> end
>
> running = true;
> while running do
> print ("s2");
> coroutine.yield(1);
> end;

I would do this as follows:

-- script file
-- immediate execution part
  print "x"

-- deferred execution part
return coroutine.wrap(
  function()
    while running do
      print "s2"
      coroutine.yield(1)
    end
  end)

-------------

In your C code, you load and then execute the script; that does the 
immediate part
and returns (on the stack) the function to execute. If you're at the top 
level,
you can simply leave this function on the stack, and do a lua_pushvalue to
execute it.

It is not completely clear to me why you need the global "running" here. 
Couldn't
you just not resume the function? Perhaps you want to execute some exit 
code when
the loop is finished: in that case, you might want to think about using a
parameter instead of a global, something like this:

return coroutine.wrap(
  function(running)
    while running do
      print "s2"
      running = coroutine.yield(1)
    end
    print "Now I'm done"
  end)