lua-users home
lua-l archive

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



On 09/08/2007, at 4:59 AM, John Klimek wrote:

We will have lots scripts running and I'm not sure how to handle a few issues:

1) Should I just have one lua_state or one lua_state for each user?
2) Suppose I want to ask the user a question and get a response in a Lua script?

You are asking about the server end, right?

I have done a considerable amount of work on this very question, as I started writing a MUD in straight Lua, and am collaborating with David Haley on doing a MUD server written in C++ with Lua scripting.

I have also added Lua scripting into an existing SMAUG MUD code-base, see this forum posting for the exact details:

http://www.gammon.com.au/forum/?id=8000


In the MUD written entire in Lua, I used a single Lua state for the whole MUD, as players needed to communicate with each other.

For the Lua add-on to SMAUG, I initially made a state per player, because each state was only concerned with things that player was doing (eg. their own quests). Then I added another per-MUD state to handle things (like resetting an area) that was not directly related to any particular player.

This post describes the scripted reset system:

http://www.gammon.com.au/forum/?id=8027


To make scripts pause and wait I would definitely use coroutines, they are ideal for that purpose. In your example of asking the player a question, you display the question, and yield the coroutine in such a way that the server knows that you are awaiting player input. Then when the player input arrives (or, a timeout occurs), the coroutine is resumed.

I did an example of doing that at the MUD client end, here:

http://www.gammon.com.au/forum/?id=4956

The above example uses a combination of Lua scripting, and timers and triggers implemented in C++ code in the client.

An example of using coroutines in the Lua MUD is this:

    Send (client, "What would you like to be called? ...")
    name = coroutine.yield ()

In this example the player input is returned from the yield, so your script can just be written using natural loops, as if the player had control over the entire server.

You are welcome to take this discussion to my forum, which maintains an active interest in both Lua and MUDs.


- Nick Gammon