Skip to main content
gamedev.net
🔒 Locked

stopping LUA execution

Started by tonydubya 5 replies 1.5k views
Original Post
tonydubya
tonydubya
hi everyone - and happy new year! Just a quick LUA question from me. I want to call C functions from LUA scripts that may or may not halt execution of the LUA script. Either this is fantastically easy and i've not seen it somehow, or it's totally not the way things work. Imagine a LUA script - script script ... Do_animation() -- this calls C code n number of times. The LUA script is executed each game cycle. Do_animation would do it's thing until it's finished - at which point it says it's done and allows the LUA script to continue. I suppose it could look like this - do n = Do_animation() if n==0 return; until n>0 perhaps? If there was a hard Quit-processing-lua-script command, the i could wrap my C calls into Lua functions which ran a loop as above. I've always coded up my own script/interpretters in the past and put this functionality right at the heart of the interpretter, previously. Can I make LUA do it? tony
TheQ
TheQ
I'm looking into this as well.

So far what I've been focusing on are Lua coroutines. I've ben able to hault execution within Lua by having a function in Lua code create a coroutine, then have that coroutine yield processing, however this only seems to yield processing to the original calling coroutine. Here is the example:

[Lua script begin]
function main()
print( "ENTER main\n" )

co = coroutine.create( intro )

coroutine.resume( co )

print( "EXIT main\n" )
end

function intro()
print( "ENTER intro\n" )

SetBackground( "Title" )

BeginGammaFade( "BlackToColor", "5seconds" )

while Not GammaReady() do
print( "fading\n" )
coroutine.yield()
end

print( "EXIT intro\n" )
end


[Lua script end]

Out:
ENTER main
ENTER intro
fading
EXIT main

Close but no cigars. The expected result is that we will end up in a loop until the GammaReady function returns true. This code -works- as it set the background and activates a gamma fade, the only issue is in yielding the process yields to the main function of the script which continues processing until it returns/exits at the end. This happens before the 5 second fade finishes (or even begins as the script is exited before the fade can actually take place. I understand why this happens, and I really think this is a dead end for coroutines in this respect. I have looked into creating coroutines directly from C/C++, however I imagine the same results (that is creating a function simply to create the coroutine, yet when the function that creates the coroutine goes out of scope (flagged for garbage collection) so will the coroutine.

I am hoping for something -yield like- that will relinquish processing by lua, return from the command (in this case I'm calling luaL_dostring( L, "main()" )), and allow me some sort of resuming method. I WILL NOT pass routine flow into Lua, the only other alternative:

function main()
StartGame()
while EndGame() do
ProcessGame()
RenderGame()
end
ShutdownGame()
end

I imagine it possible to create this functionality within Lua, but the popularity of Lua suggests this sort of functionality already exists in some capacity.

Tony, I'll let you know if I find anything, please afford me the same favor. Any one else's help is very welcome.

-Q
Sneftel
Sneftel
WRT how TheQ is doing things: Just put the coroutine in the global table (as you are already doing). Your code, modificationized:

function setup()  print( "ENTER main\n" )  co = coroutine.create( intro )  coroutine.resume( co )  print( "EXIT main\n" )endfunction perframe()  coroutine.resume(co)  if ~ coroutine.running(co)    print( "EXIT main\n" )  endendfunction intro()  print( "ENTER intro\n" )  SetBackground( "Title" )  BeginGammaFade( "BlackToColor", "5seconds" )  while Not GammaReady() do    print( "fading\n" )    coroutine.yield()  end  print( "EXIT intro\n" )end[Lua script end]Then, in your C++ code, you call setup() once and perframe() each frame. I've written it in a slightly spaghetti-ish form in order to show the modification; the way it would actually work would be much clearer. But it gets even easier than that: You can simply use lua_resume() from the C++ side, instead of lua_call().
TheQ
TheQ
Ah! Looks like I was one step away! Cool, that makes perfect sense and leaves the implementation on the Lua side so the C++ code maintance is low - thanks!
tonydubya
tonydubya
Actually, some digging around has given me the following taken from a LucasArts game script -

while not done it yet
break_here()
end

but what is break_here() ? It looks like something built into LUA in previous versions, but not the version 5 I am looking at? The script in question was coded in 1999

Anyone know anything about break_here?
Sneftel
Sneftel
LucasArts used a modified version of the Lua interpreter. It's likely that they implemented their own version of coroutines in it (since they weren't available in 4.0).
TheQ
TheQ
I've implemented said changes an all works fine, thanks again.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.