lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi

 

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:

wait(<some condition>);

 

This will block the thread, evaluate the condition every frame and will continue once it becomes true. For example:

wait(GetPlayerHealth(playerIndex)<50);

 

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?

 

Regards

Ivo