JohnnyStarr
Wizard


Group: Members
Posts: 823
Joined: Feb 14, 2009
|
#1 id:31896 Posted Aug 19, 2009, 7:11 pm
|
Ok, with all this Lua talk, I am hoping someone could suggest a quick example of it
embedded into a mud. I know that Nick Gammon has a thread about it on his site, but
it is for SMAUG, and from previous discussions on this forum, he has chosen his own way
of doing it. Personally, I have a few ideas of my own, but am wondering what anyone out there
would do with it. I have read up on the topic on several sites, and it dosn't seem too difficult (in theory)
but if anyone could present an actual code example of the simplest step, that might help in figuring
out if where to take it next.
|
.........................
"It's not the daily increase but daily decrease. Hack away at the unessential." - Bruce Lee
Southlake Window Cleaner
|
|
tphegley
Sorcerer


Group: Moderators
Posts: 363
Joined: Aug 13, 2007
|
#2 id:31898 Posted Aug 19, 2009, 7:17 pm
|
Well, what nick has done with it is quite neat in that you can have quests and area resets using Lua. What exactly do you want to use it with?
What exactly is your question? Do you intend to write your own lua code to input into a mud and asking how to do that or are you looking for how to use Nick's lua example?
|
......................... Admin of Legends of Old
Currently still in testing and design stage.
|
|
|
|
David Haley
Wizard


Group: Members
Posts: 6,907
Joined: Jun 30, 2007
|
#4 id:31923 Posted Aug 19, 2009, 9:49 pm
|
I can't suggest a practical way to embed Lua into ROM specifically as I am not aware of ROM's internals. Nick's examples are good for embedding into SMAUG although his approach has some issues if you want to do certain things, in my opinion.
My code creates a userdatum wrapper for each object I want to "reveal" in Lua. The C++ object associates a Lua reference to that object. When the C++ object is destroyed, that reference is released, letting Lua garbage collect its version of the object (assuming nobody else is referring to it). A Lua reference to an invalid C++ object is detected when dereferenced, to avoid crashes (but a Lua error is still generated, which can be caught from within Lua).
Each exposed class is given a metatable with various methods, and each "revealed" object's userdatum is given the appropriate metatable.
A lot of this assumes familiarity with Lua's metatable model; you need to understand metatables before understanding how to embed structures (in C or C++) in Lua.
At some point I will write more about all of this and maybe even give some kind of sample code.
|
|
|
|
|
David Haley
Wizard


Group: Members
Posts: 6,907
Joined: Jun 30, 2007
|
#6 id:32221 Posted Aug 23, 2009, 5:49 pm
|
The first thing to understand is metatables in general (they define, among other things, what to do when a field doesn't exist in a table, or how to get fields from userdata), and then understand what userdata are, and finally how one can make a userdatum appear to have fields by using metatables.
It's not easy to given an example for an entire working MUD because there's an awful lot of stuff involved and the example quickly becomes overwhelming. You might want to take a look at item 21 of the Lua Programming Gems; if you want the full text you'll have to buy the book, but the provided sample code gives an example of things existing in C++ and/or Lua and being visible across the scripting/host language divide.
|
|
|
Erok
Magician

Group: Members
Posts: 64
Joined: Jul 27, 2009
|
#7 id:32224 Posted Aug 23, 2009, 6:18 pm
|
Suggest you also look at SWIG and its support for Lua. You can jump right in with it, play around, and then go back into the detailed aspects David has suggested.
|
Last edited Aug 23, 2009, 6:21 pm by Erok
|
|
JohnnyStarr
Wizard


Group: Members
Posts: 823
Joined: Feb 14, 2009
|
#8 id:37084 Posted Nov 3, 2009, 2:17 pm
|
David Haley said:
At some point I will write more about all of this and maybe even give some kind of sample code.
Would you still be willing to do this?
Also, we have gone over Nick's method a bit, and your view of a global Lua state.
Would it be possible to create a lua_State in char_data to handle mob scripting, and then
to add a lua_State to the overall system to be able to add additional sub systems? For example,
I would prefer to advance ROMs update system to an event handler, and would prefer to use
Lua.
I just bought "Programming in Lua 2nd ed", so perhaps I will be able to answer these questions myself
eventually. It seems the challenge is utilizing the API more so than say the scripting itself. As far as
I can tell so far, it is just as friendly as Ruby :)
|
.........................
"It's not the daily increase but daily decrease. Hack away at the unessential." - Bruce Lee
Southlake Window Cleaner
|
|
David Haley
Wizard


Group: Members
Posts: 6,907
Joined: Jun 30, 2007
|
#9 id:37085 Posted Nov 3, 2009, 2:32 pm
|
Quote:Would you still be willing to do this?
Sure, but I'm not sure how much time I have to come up with a complete example of embedding user data into Lua -- I'll see what I can do, though, since I have a lot of code lying around, I just need to get around to cleaning it up and packaging into a nice form.
Quote:Also, we have gone over Nick's method a bit, and your view of a global Lua state.
Would it be possible to create a lua_State in char_data to handle mob scripting, and then
to add a lua_State to the overall system to be able to add additional sub systems?
If you're going to have a global state that can treat characters as proper values, you don't really need the per-character states. (Note that having one of those for every single mob can get a little hairy: Lua is small, but not that small. )
Quote:As far as
I can tell so far, it is just as friendly as Ruby :)
Lua doesn't have a standard library anything like Ruby, which is a big problem or a nice feature, depending on what you're doing. I find the syntax to be clearer, but that is probably mostly just habit. (I've written a fair bit of Ruby code, but never really liked its usage of funky symbols; I find that to be an unfortunate holdover from Perl.) I think that Lua is exceedingly nice for writing small, targeted scripting, and it is very easy to embed into a larger application. I will have an example up soon of how I used Lua to parse tile descriptions almost for free.
|
|
|
elanthis
Wizard

Group: Members
Posts: 772
Joined: Feb 26, 2008
|
#10 id:37086 Posted Nov 3, 2009, 3:53 pm
|
staryavsky said:Ok, with all this Lua talk, I am hoping someone could suggest a quick example of it
embedded into a mud.
I've got an initial article on the basics of integrating Lua with a C++ game object at http://gamedevstudent.com/journal/2009/10/safe-and-efficient-lua-integration/ and I've got an expanded "Part 2" in the works that goes over higher performance bindings and best practices for binding methods to objects. I'll eventually get to the Part 3 on message and property binding as well. (I've had midterms last week and our project's Engine Proof and TDD finalization this week, so I'm a little swamped atm.)
I do not generally recommend binding generators. I know I'm in a small minority with that opinion, but in my experience they tend to take away a lot of control, introduce a lot of unnecessary overhead, and still require a ton of manual code to make safe... and the manual code is more complex because you're wrestling not only with your C++ app and the Lua API, but now also the bindings API. Ideally C++ would have compile-time reflection and meta-programming support and make binding to a script engine with custom control dead easy, but alas, it does not. Binding a method to Lua is very easy though. The hardest part that most people get wrong is the safe handling of the game object reference, which the above article covers.
A good game object design has very few object methods anyway, since you generally want to aim towards generic components and messages, http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/, so manual binding of methods is not nearly as much work as it sounds. Especially if your C++ messages are generic containers (I've seen game engines that just attach a type ID and a Lua table to a generic Message object) and your C++ game objects have property setters and getters (which you desperately want for game editing, debug introspection, and behavior tweaking even if you're not using a script engine at all).
Quote:I think that Lua is exceedingly nice for writing small, targeted scripting, and it is very easy to embed into a larger application.
That's because that's what is was designed for. Lua was primarily a data file format, and a scripting engine second. Ruby is designed as a Real Programming Language, not a little extension language. You can use either language for either purpose, but frankly, if you want to write a large application in a dynamic language (e.g., and entire MUD engine), I'd opt for Ruby over Lua. However, if I'm going to extend a large C/C++ application, I'd use Lua over Ruby.
No one tool solves all problems optimally.
|
|
......................... Cutting corners to keep your line count down is just sad.
|
|
David Haley
Wizard


Group: Members
Posts: 6,907
Joined: Jun 30, 2007
|
#11 id:37087 Posted Nov 3, 2009, 4:37 pm
|
elanthis said:Binding a method to Lua is very easy though. The hardest part that most people get wrong is the safe handling of the game object reference, which the above article covers.
Yes, this is my experience as well. It's far more difficult to deal with cross-language references, objects and memory management than it is to expose functions.
elanthis said:Lua was primarily a data file format, and a scripting engine second. Ruby is designed as a Real Programming Language, not a little extension language.
The historical argument for Lua about not being a "Real Programming Language" is about a decade out of date. Lua's problems do not come from its semantics, syntax or other such things: they come quite simply from not having the scaffolding to make large application development easier (e.g., standard libraries, OO framework, etc.). But what's interesting is that the OO framework can be built; you could pretty much replicate Ruby (or Python or ...)'s semantics in Lua if you felt like it when it comes to how they model object orientation. That flexibility is, of course, a double-edged sword, in that you have to write a lot of this code yourself (or find somebody who has done it already).
Anyhow, it's not appropriate to say that Lua is just "a little extension language" if you mean anything other than "it has a small standard library" (like what I just said). I'm not sure you meant anything more, but I wanted to clear this up: a lot of people tend to dismiss Lua as a toy language that isn't as "powerful" in terms of semantics because of its very early history.
|
|
|
elanthis
Wizard

Group: Members
Posts: 772
Joined: Feb 26, 2008
|
#12 id:37096 Posted Nov 4, 2009, 12:23 am
|
Quote:Anyhow, it's not appropriate to say that Lua is just "a little extension language" if you mean anything other than "it has a small standard library" (like what I just said). I'm not sure you meant anything more, but I wanted to clear this up: a lot of people tend to dismiss Lua as a toy language that isn't as "powerful" in terms of semantics because of its very early history.
Lua is powerful. It is a complete language with every feature a language needs to write real software.
It's still unoptimal to use it for general programming when you can use Python, Ruby, PHP, Perl, Java, C#, or any number of other languages that are far, far, far better for such things. It is far more than just the lack of a standard library. The lack of a defined object model is actually a huge WEAKNESS, not an advantage, because out in the real world people don't want to have to write custom object models, they don't want to have to integrate multiple libraries using incompatible object systems, and they do want to have a ton of documentation on how the object model works with examples and libraries and ready-to-go software. People don't want to "roll their own" for general programming. That's something you want when you're writing an extension language. It's not something you want when you're writing a big application comprised of many components and third-party libraries. Not even a little.
You like Lua. I like Lua. The majority of the game industry likes Lua. That doesn't change the fact that Lua has severe deficiencies as a general programming language, and we WANT those deficiencies between "fixing" them would make it a less appealing extension language. :)
|
|
......................... Cutting corners to keep your line count down is just sad.
|
|
KaVir
Wizard


Group: Members
Posts: 1,636
Joined: Jun 19, 2006
|
#13 id:37100 Posted Nov 4, 2009, 6:29 am
|
elanthis said:Lua is powerful. It is a complete language with every feature a language needs to write real software.
A little off-topic, but has anyone tried using Lua for player-written scripts? They'd need to be run in a sandbox, which would obviously have to handle things like infinite loops and such. It'd be pretty cool for player-generated content though.
|
......................... KaVir at God Wars II: godwars2.org 3000 Roomless world. Manual combat. Endless possibilities.
Last edited Nov 4, 2009, 6:29 am by KaVir
|
|
David Haley
Wizard


Group: Members
Posts: 6,907
Joined: Jun 30, 2007
|
#14 id:37103 Posted Nov 4, 2009, 9:16 am
|
OK Elanthis, so you're saying basically the same thing I've been saying re: stdlibs and OO (although you phrase it as a disagreement, which I think is a little weird). When you said it wasn't a "Real Programming Language" you made it sound like there were things it simply couldn't do that other languages can. I've never advocated using it as a general purpose language; in fact, in many threads, I've said that people shouldn't use it to write full applications precisely because its stdlib is so small and lack of standard OOP scaffolding.
KaVir said:A little off-topic, but has anyone tried using Lua for player-written scripts? They'd need to be run in a sandbox, which would obviously have to handle things like infinite loops and such.
I've toyed with the idea but haven't let it loose in the wild. Sandboxing by removing functions is extraordinarily easy as you can define the global environment for functions. Dealing with infinite loops is a little more interesting but still possible, as you have hooks into the interpreter for instruction count. You can therefore keep an eye on how long the script has been running, and cut it off after so many instructions have been executed.
|
|
|
JohnnyStarr
Wizard


Group: Members
Posts: 823
Joined: Feb 14, 2009
|
#15 id:37105 Posted Nov 4, 2009, 10:00 am
|
KaVir said:elanthis said:Lua is powerful. It is a complete language with every feature a language needs to write real software.
A little off-topic, but has anyone tried using Lua for player-written scripts? They'd need to be run in a sandbox, which would obviously have to handle things like infinite loops and such. It'd be pretty cool for player-generated content though.
Aardwolf does here. Not sure if they've opened up building to the public.
|
.........................
"It's not the daily increase but daily decrease. Hack away at the unessential." - Bruce Lee
Southlake Window Cleaner
|
|