forcing a coroutine.yield()

Started by
12 comments, last by Genjix 18 years, 10 months ago
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.
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
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.
int Print(lua_State *lua_vm){}

so is there no way with the above example I do a coroutine.yield() on the state machine? :(
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.
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]
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.
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)
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")

This topic is closed to new replies.

Advertisement