Lua looks pretty cool…

Lua is an open-source scripting language that’s designed to be embedded into larger apps. I know it’s used in World of Warcraft and SciTE. There are examples out there of using Lua from Delphi apps. And through another open-source project, LuaInterface, you can embed Lua scripting into a .NET app, so easily it’s almost silly.

I’m writing a video-game engine in .NET, and I looked into Lua because I want to be able to have sandboxed game packages. The idea is that if someone already has the game engine installed, they could download game packages from the Internet and run them as fully-functional games without fear of viruses or Trojans. So I wanted a scripting environment that could run in a sandbox, with, for example, no access to the filesystem. It turns out this is trivial with LuaInterface:

Lua lua = new Lua();
// Don't let scripts use the "io" or "debug" packages.
lua["io"] = null;
lua["debug"] = null;
// Don't let them call the "dofile" method either.
lua["dofile"] = null;
// etc.

Classes1 and methods are both reference types, and to make them go away, you just need to nil out the global variable that holds the reference. There are only a few dozen of these globals when you spin up Lua, so it’s not hard to go through them and decide which ones are safe for a sandbox and which aren’t.

It’s easy to push values into, and read values out of, the Lua environment:

lua["answer"] = 42;
int answer;
answer = (int) lua["answer"];
answer = lua.GetInteger("answer"); // equivalent

You can even assign .NET objects into Lua variables, and all their methods and properties are instantly callable from Lua. Alas, this is not sandbox-friendly in the shipping version of LuaInterface, because by default, Lua can call private methods on that .NET object! But there’s an easy code change to LuaInterface that fixes this.

And Lua has built-in coroutine support. So I can use it for writing cooperative, synchronized scripts for sprite interaction. I’ll definitely be posting more about this.

1 Technically, they’re not classes, they’re “tables”, which are kind of like supercharged associative arrays. There’s some good Lua documentation if you want to know more; I’d recommend starting with Programming in Lua.

Leave a Reply

Your email address will not be published. Required fields are marked *