Jump to content
  • Advertisement
Sign in to follow this  
Genjix

forcing a coroutine.yield()

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

if lua calls one of my external functions, how can I force it to do a coroutine.yield()? what is the python equivalent of a couroutine.yield() and how can I force one when it calls my external functions? of course these are both embedded in C++, and by external functions I mean the C++ functions which are hooked into lua/python. Thanks for your time.

Share this post


Link to post
Share on other sites
Advertisement
The python equivalent would either be to write your function as a generator, or to use Stackless Python.

I don't think Python nor Lua can do anything while the control is in your C++ code.

Share this post


Link to post
Share on other sites

int main()
{
while(1)
{
// update

foreach(it , scripts)
it->Update();

// ...
}

return 0;
}





TGnew_waypoint("aeris",100,200)

while not TGat_waypoint("aeris"):
pass

# perform some other action




so in the above TGnew_waypoint would call the game and perform its changes.
game gets updated because of coroutine.yield() (or whatever it is)
then spinlock is entered, and it keeps testing for the condition. therefore it doesn't just hang the game and keep looping and the game gets the chance to update at every external function call (assuming all other operations in script are negligible).

otherwise i would need this all the time in the scripting language

TGnew_waypoint("aeris",100,200)
coroutine.yield()

while not TGat_waypoint("aeris"):
coroutine.yield()

# perform some other action




which would put people off and is very annoying (not to mention potential for bugs).

thanks anyway for your time Fruny.

Share this post


Link to post
Share on other sites
Coroutines cooperate to pass control back and forth. If you want to be able to take over from a routine at arbitrary points, you need threads.

Share this post


Link to post
Share on other sites

int Print(lua_State *lua_vm)
{
}


so is there no way with the above example I do a coroutine.yield() on the state machine? :(

Share this post


Link to post
Share on other sites
The docs say that for coroutine.yield, "The coroutine cannot be running neither a C function, nor a metamethod, nor an iterator." You can only yield from Lua script, via the yield function. You could make your Print function return a value that signals to a wrapping Lua function that it's time to yield though. You can't yield part way through a C function though. That would require that C supported coroutines, which it doesn't.

Share this post


Link to post
Share on other sites
ok thank you :(

tell me what you think of what I'm going to do.
use assembly style scripting language and rip a parser from lua (or find one for python) and have it convert to my internal assembly language, so I can add a breakpoint between each instruction.

Does anyone have a parser lying around?[smile]

Share this post


Link to post
Share on other sites
I'd ask, where's the benefit there over using threads? Any time you abandon a process that is executing, you have to give some consideration to its current context, and that applies whether it's your own ASM-like language, an existing scripting language, or any other language.

Share this post


Link to post
Share on other sites
because I am building a resource tree, whereby each different trigger event consists of a spinlock and a script.

somehow 30+ threads doesn't cut it.

(logic is hard coded, but events are scripted)

Share this post


Link to post
Share on other sites
How about something like this:

function wait_until(condition, ...)
while not condition(unpack(args)) do coroutine.yield() end
end

TGnew_waypoint("aeris",100,200)

wait_until(TGat_waypoint, "aeris")

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!