I've just completed the foundation of embedding Lua in my MUD, and I'd like to release the implementation as is. I will be adding more to it, but my MUD differs greatly from stock and much of what I plan to implement won't be applicable to other developers, so I am just going to release this basic foundation.
Here is an example Lua script.
– do_lua_test, function to test the lua implementation
function send(char, …) send_to_char(char.id, table.concat {…} .. "\n\r") return end – send
function do_lua_test(ch, argument) if argument then send(ch, "&RYou typed: " .. argument) end send(ch, "&GYour name is " .. ch.name .. ", and you are a " .. ch.gender .. " " .. ch.race .. ".") och = get_char_world(ch, "tech") if och then send(ch, "&YWe found: " .. och.name) end ch:say("Testing") return end – do_lua_test
And here it is in action:
Force:0/0 Align:0 [Hp:500/500] [Mv:1000/1000] (Align:0) lua_test Some args You typed: Some args Your name is Kasji, and you are a male Noghri. We found: a skittish verpine tech You say, "Testing"
How does it work? Characters, objects, and rooms are setup as metatables. On the C side of things, each entity holds a unique ID it inherits from a base class called "tObject". This ID is looked up in a map to verify the entity still exists at access time. Many thanks to Nick Gammon for that solution. This allowed me to use metatables to not have to store a clone of the entity being used in Lua, but rather access the C entity in real time by overriding the __index operator. Further more, specific identifiers can be added to the entity's table before the metamethods, and they will be accessible normally, such as the "say" function being a member of ch.
Here is an example Lua script.
And here it is in action:
This is on a heavily modified SWFotE FUSS.
How does it work? Characters, objects, and rooms are setup as metatables. On the C side of things, each entity holds a unique ID it inherits from a base class called "tObject". This ID is looked up in a map to verify the entity still exists at access time. Many thanks to Nick Gammon for that solution. This allowed me to use metatables to not have to store a clone of the entity being used in Lua, but rather access the C entity in real time by overriding the __index operator. Further more, specific identifiers can be added to the entity's table before the metamethods, and they will be accessible normally, such as the "say" function being a member of ch.
Link to the topic on Nick's forum: http://www.gammon.com.au/forum/?id=11779