I'm currently working on an engine that relies heavily on the scripting language
Python. Unfortunately, I've discovered major bottlenecks in relying on Python
(mostly because it's interpreted), and so am switching to scripts written in C++
(compiled, but seperate to my engine, for ease of development). One of the
major disadvantages to doing this would be to loose some of the 'cool' features
of Python, such as generators, which allow you to resume functions by using a
'yield' keyword to signal you're ready to quit, and would like to resume from
that spot when the function is called again. Today, I solved the problem (took
me about 15 minutes actually, so I can only assume many others have discovered
this cool feature) by using a bit of ASM (sorry, windows only, though you could
probably do something similar on other systems) and some compiler generosity. I
developed this using VC++ 6.0, though with minimal changes, it should work on
other compilers as well. Well, here's the code. It has two examples, one with
a function with a return value, and the other has a function without one (the
yield command is a macro, and doesn't use up ANY parameter/return value space).
I tried to comment the macros and program as best I could, and one improvement
that might be necessary (I don't need it) is the ability to 'reset' the
function, to execute from the beginning of the function (instead of wherever the
last place you left off is). A cool side-effect of the way I did this was that
the calling function doesn't know or care if the function it's calling is a
'generator' or not, it just calls it more than once (same way) to yield/resume!
Enjoy
Chris PergrossiMy Realm
Email me at only_jolly_roger at hotmail dot com if you have questions, comments,
improvements, or have used the tip in any of your projects. Feel free to use
the code any way you like, I'm just being curious.