forcing a coroutine.yield()

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

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 on other sites
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 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 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 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 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 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 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 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 on other sites
How about something like this:

function wait_until(condition, ...) while not condition(unpack(args)) do coroutine.yield() endendTGnew_waypoint("aeris",100,200)wait_until(TGat_waypoint, "aeris")

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 14
• 14
• 45
• 22
• 27
• Forum Statistics

• Total Topics
634044
• Total Posts
3015211
×