Sign in to follow this  

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

This topic is 3287 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
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
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

This topic is 3287 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this