Interpreter with "suspend" capability
Are there any language interpreters (e.g. a JavaScript interpreter, Lua inter开发者_如何学运维preter, Python interpreter, etc.) that have the ability for the script to suspend itself, and for the host to resume it later?
I'm writing a console-RPG game engine in .NET, and I'd like to be able to write a script for each non-player character on the screen. These high-level scripts would call low-level script functions to do things like "walk one square south", which would be implemented as a loop: walk four pixels, then yield until the next animation frame; walk four pixels, then yield; etc. "Yield" would be some mechanism that lets the script tell the interpreter, "Okay, I'm done for now. Save my state, and my call stack, for later." When the timer fires for the next animation frame, the host would resume the script where it left off. There would be multiple scripts running independently, which probably means multiple instances of the scripting engine so that each one can have its own state.
Of course I could do this with any language, if I was willing to write a thread for each character's script, and resume/suspend every thread for every frame. But I don't want to create potentially dozens or hundreds of threads, which is why I'm exploring the possibility of scripts instead.
I'm not specifically attached to it being an interpreter, but I suspect that would be a requirement. Suspend capability, without threads, would pretty much require that function calls not be implemented using the CLR stack -- they would need to use Stack<T>
or some other sort of heap objects instead -- which might well mean an interpreter built specifically for the purpose of suspendability.
I'm aware of the upcoming await
keyword in C#, which would give me the concurrency I'm looking for without threading, but it's crazy complicated, and requires changing all the calling code to be async
. It's also a compiled language, and scripting is more appealing for game authoring. If possible, I'd rather just provide a script library of "walk" functions and let the script author be blissfully unaware of the underlying complexity.
精彩评论