-------- 原始邮件 -------- 发件人： Ivo Beltchev <[hidden email]> 日期: 2013-01-08 15:21 (GMT+08:00) 收件人： [hidden email] 主题： Passing expression as a parameter
I am attempting to convert a game engine with its own proprietary scripting language to Lua. One particular construct I’m having trouble with is this:
This will block the thread, evaluate the condition every frame and will continue once it becomes true. For example:
I am trying to find the best way to create something similar in Lua. I am having trouble getting a syntax that is close to the original. Ideally I should be able to pass a chunk as a parameter, but I don’t think that’s possible in Lua.
I have 3 potential solutions so far:
1) wait(“<some condition>”);
The wait function takes a string, and evaluates it as a chunk until it returns true
2) repeat yield() until <some condition>;
3) yield( function return <some condition> end);
yield will return a function, the engine will store it in the registry and keep calling it until it returns true
#1 has the simplest syntax, but requires code to be compiled at runtime (so it may have some syntax error only found at run-time), and also the condition can’t access local variables
#2 is the closest to the behavior I want, but is quite verbose
#3 gives me direct control when and how often to evaluate the function, but I’m not sure how efficient it is, especially if local variables are used
What would you guys recommend? Am I missing a more elegant solution?
On 09/01/2013 17.52, pass86 wrote:
> I have 3 potential solutions so far:
> 1) wait(“<some condition>”);
> The wait function takes a string, and evaluates it as a chunk until it returns true
> 2) repeat yield() until <some condition>;
> 3) yield( function return <some condition> end);
> yield will return a function, the engine will store it in the registry and keep calling it until it returns true
If I remember correctly (it was some time ago) I started Lua game event
handlers as coroutines, passing each of them a unique ID and storing
them in an ID-indexed table.
When a handler needed to wait for something (e.g. for the end of an
animation) the scripting API function (e.g. Walk(character, toPlace))
notified the (C++) engine to start the requested action, then yielded.
At the appropriate time (e.g. animation ended), the engine resumed the
handler using the same ID to identify the coroutine to be resumed.