Lua: Script glue resources

Oldtimer
Posts: 834
Joined: 2002.09
Post: #1
I'm wracking my head over how to implement a decent scripting system for my upcoming game. However, I find it quite hard to figure out a way to create good glue code between the engine and script. I'm planning to use Lua.

What I'm looking for specifically is ideas, articles and stuff on what methods to expose, how to organize data transfer formats... you know, the pseudo-technical stuff. Building a message system, or communicating by passing pointers back and forth?

Perhaps this could become an interesting discussion? I'd love to hear everyone else's thoughts and experiences on this.
Quote this message in a reply
Member
Posts: 184
Joined: 2004.07
Post: #2
My udg entry currently does this. Some notes that I probably will copy to the postmortem provided my entry doesn't suck ass during the voting phase:


FUNCTION INTERFACE:

What I found to be the easiest (possibly not the most efficient) way to do the interface is have my Lua procedures each take in one argument, which is a table- this way I can quickly change my interface and allow default values. For example, I might have:

render_billboard( [x=50, y=20, texture="mytex.png"] )

and then if I decide to allow a color, I can make the default color white if it is unspecified and the user could also specify it:

render_billboard( [x=50, y=20, texture="mytex.png", color=[1.0,0.0,0.0] )

There is still some annoying manual labor you have to do to implement this. For each argument you obviously have to try to look up the string in the table, and if it's there, use it instead of the default argument.

I would NOT use Luabind (http://luabind.sourceforge.net/) - it makes your compliation time incredibly slow, and if you start increasing the maximum number of arguments in your interface, it starts running into too-deep template errors. I tried it out and wasted a lot of time trying to use it out.


VECTORS:

I found it useful (though it's somewhat slow) to represent vectors with tables. You can see in my previous code example how this works. The way to do this is really easy- in lua, you push a table, then push the number 1, push the first value, push the number 2, push the second value, then the third value. I use vectors of size 2 through 4. All my vector math is implemented in lua. If this is too slow you can of course push it up into the engine layer, but again you still have this sort of overhead by converting to a table. But again, I found it incredibly easy and flexible to do it this way.


HANDLES:

The way my system works is that when you ask for a new object, you get an integer handle to the object. You can then refer to it by the handle:

local ob = create_object( [model="test.model", loc=[0,1,0])
...
set_object( [index=ob, loc = [1,1,1] )

In my system, objects are the only things with handles. You could do a lower-level version of things where textures have handles, models have handles, etc. I find though you may just want to always specify what you want each time and just use a cache. So for example instead of passing a texture to a handle, then using that handle, you just mention the name of the texture every time, and the system is smart enough to internally cache that texture so that it doesn't have to load it each time the draw texture is called.


There are of course a lot of programs that use LUA, and I have actually not looked at any other ones (which is probably to my disadvantage... but my engine is working fine so far, except being somewhat slow on older systems.) One recent game I am aware of because I was looking at rpgdx.net is Gia (http://luaforge.net/projects/gia/) though I haven't looked at how he does things.

p.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #3
I've used luabind with success, and although it does have its limitations, and compile times are indeed painfully slow, it does work, and does so well. Problem is it only works with C++.

If you plan to do scripting, my suggestion is to do as much in script as possible, and write glue code for OS interfacing, and create compiled implementations for performance intensive stuff.
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #4
I learned to implement Lua from "Game Scripting Mastery" by Alex Varanese. It's an awesome book if you have the time, it really is. Well worth every penny IMO. The Lua section is just a chapter but it has pretty much all of the info you'll need for it. I found his description on how to use it very thorough even though he wrote all the examples for the PC. Regardless of it's PC-ness, I think he still did a great job trying to stay platform neutral and it wasn't too difficult for me to make it happen on the Mac. Lua has got to be the easiest scripting language to understand and incorporate into a game but it's not easy to describe in a forum post. I would highly recommend the book if you can swing it.

[edit]Hey, if you give me an email address I'll send a 728k sitx with all the Lua stuff and a small demo I hacked from the book if you're interested.
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #5
phydeaux, first off: your avatar is the coolest in the history of avatars. God, I so wanted to be able to shoot that dog.

Thanks for your ideas - representing parameter lists with key tables was new to me. I really believe I'll be doing that in the future. That also rids me of the problem of binding bunches of methods just to set/get different properties.

I looked at luabind, and I don't think that's for me though. I'm not aiming to create a fully scriptable engine, but rather enable scripting to create special scenes, levels and character behaviours.

As for handling objects, I pass pointers around freely. Instead of going with ID:s and the added complexity, I just return the address of an object in memory, pass it out to the script, and when the script wants to do something with the object, it passes the pointer back to the application. Even though it feels a bit unsafe (I can't relocate objects in memory without some fuzz), I haven't stumbled upon any problems with that approach.

Jake, I've leafed through the book in the bookstore, but it felt a bit too centered around creating a compiler and constructing a separate language, which is a bit too inventing-the-wheel to me. How much of it is dedicated to actual scripting rather than compiler design? I'm thinking about picking it up tomorrow, but I'd like your opinion on it. Wink

Thanks,
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #6
You're right, it is very centered around creating your own fully compiled scripting language, but that's not why I would recommend it to you. The next time you make it to the bookstore, look at chapter "6: Integration: Using Existing Scripting Systems", that's the one you're interested in. Within that there are 57 pages specifically on Lua, pages 185-242. He also covers Tcl and Python in that chapter. The whole chapter is 160 pages long and might very well be worth the weight of the entire book for some. You really don't need to read any other chapters to do just that one, it could easily stand on its own. I found it an easy introduction to Lua, but I think a person could just as well learn it through the Lua docs if that works for you. At the time I learned it there wasn't that new book available online about programming with Lua either, so that might also be another route, although I haven't read it myself to know.

The Lua distribution compiles without issue on OS X as I recall. The sitx file I was talking about has my OS X port of his little (136 lines of C, and two example scripts) Lua demo along with the latest Lua distro and compiled libraries and manual. The distro also compiled the luac command-line compiler and the lua interactive interpreter, so you'd be ready to go without having to think too much about it at first. That and the book are all I needed to get rockin with it myself.

BTW, I've read half of the book so far and got as far as implementing the assembly language along with it's virtual machine. I'll do the rest later when I get more time. For me, the point of doing this was not to re-invent the wheel (I use Lua currently) but to learn a little about compiler theory. This has been very helpful to me since I've never studied computer science in college and never plan to. My programming skills are self-taught all the way and I've found that this book helped fill a large hole in my general understanding of it. It's good stuff for the amateur if anyone else is interested in it.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Running a .APP from within a shell script (osx) Jamie W 3 3,406 Dec 8, 2009 03:06 AM
Last Post: Jamie W
  java script using UNITY imaumac 6 6,210 May 16, 2009 08:07 AM
Last Post: imaumac
  What do you think about this free cute doggie script :) Polycount Productions 1 2,403 Apr 7, 2006 04:04 AM
Last Post: Taxxodium
  Running a lua script multiple times... errors on 2nd,3rd etc Joseph Duchesne 10 5,427 Jul 19, 2005 05:01 AM
Last Post: unknown
  Lua: Script help hyperzoanoid 5 4,989 Dec 15, 2002 12:49 PM
Last Post: OneSadCookie