Jump to content
  • Advertisement
Sign in to follow this  
mwmey1

Coroutines dont 'resume' at 'yield' (Lua)

This topic is 3564 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm new to coroutines and am having some issues. I have a Lua function which runs each time you click the mouse. This function resumes a coroutine, but the coroutine starts from the beginning every time instead of where it yielded. I'm not sure if I'm missing something fundamental.
function onClick()
	coroutine.resume(co)
end

co = coroutine.create( function ()

	Say( "1" )
	coroutine.yield()
	Say( "2" )
	coroutine.yield()
	Say( "3" )

end)
Every mouse click results in an output of '1'. The happens over a number of script executions, perhaps my coroutine re-initialises each execution? Thanks M

Share this post


Link to post
Share on other sites
Advertisement
you say "it happens over a number of script executions".

Are you running the script again and again via dofile or dostring?

Share this post


Link to post
Share on other sites
Thanks for the reply mystb.

Everytime the mouse is clicked, the script is executed using
luaL_dofile(L, scriptname);
The lua-state is never closed until the program exits, so I thought the coroutine would persist through multiple executions.

Share this post


Link to post
Share on other sites
And that's your problem; each time you run the script it sets everything up again from the start to the end.

What you need to do is load the script and execute it ONCE at start up to set it up and then, on each click event, call the OnClick function, not execute the script again.

Share this post


Link to post
Share on other sites
Thanks phantom, I felt that could be the issue.

To increase the complexity a bit, the script that is run depends on the NPC clicked. Am I correct that I can only have one script executing in a Lua-state at a time?

How would I handle the script changes, but also avoid the re-initialisation?

Perhaps I am handling the entire 'individual npc script' issue the wrong way.

Share this post


Link to post
Share on other sites
Ah I solved it.

Im sorry, I thought coroutines had to be defined in global space, but no. It was then a simple matter of rearranging my code and calling an Init Function.

Thanks for the help, it was greatly appreciated.
M

Share this post


Link to post
Share on other sites
simple:

instead of having scriptA with function onClick(), scriptB with function onClick()......, you can have each script run once and with different function definitions inside: onClickA(), onClickB(), etc.

In the end, instead of calling dofile with diferent scripts, just call diferent functions.

Hope it Helps,

DC

Share this post


Link to post
Share on other sites
The other method involves using tables to store per entity information, that way you can load a script once and then use these tables to store functions and per entity data which you can then call/use from the C/C++ side.

Share this post


Link to post
Share on other sites
Thankyou again phantom and mystb for all your help.

Edit: I answered my own question, so I've removed it.

[Edited by - mwmey1 on December 6, 2008 7:19:37 PM]

Share this post


Link to post
Share on other sites
Hi,

What happens if we never resume a coroutine ? How will it get freed runtime ? Is there a way to say, "I am not going to resume you anymore...just go and die", to the coroutine ??

-madan

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!