Meh! (Re-rails thread)
Now that I have this committed to CVS...all this is in C, so if you're just interested in scripting with Lua, ignore this and see my recent build.
Note that while I'm keeping Python in for now, I'll end up removing all support for it if noone expresses interest in coding for it after awhile.Scripting extension how-to for Lua in fs2_open
Right now scripting is very easy to do, but also somewhat limited. I haven't added a function to let you get the result of evaluating an expression; but you can provide functions and objects.
First of all, find the "Housekeeping" line. You will not need to change anything below that.The important structs
script_lua_lib_list - An array of librariesImportant defines
script_lua_func_list - An array of library functions.
script_lua_obj_list - An array of objects provided by a library
script_lua_obj_func_list - An array of object methods.
LUA_NEW_OBJ() - Creates a new object in Lua and returns a pointer to it.
LUA_UDATA() - Used with script_parse_args to check an object's type and store it to a pointer
LUA_RETURN_OBJECT - Return this when you've created an object with LUA_NEW_OBJ that you want to return.Important functions
script_parse_args - Parses args provided to a function, using a format list that specifies the destination arguments.
b - boolean
d - double
f - float
i - int
s - string (Pointer to a char*)
u - userdata (Using LUA_UDATA for the argument)
script_return_args - Like parse_args, except returns the specified values. Lua can have multiple return values. If you have created a new Lua object with LUA_NEW_OBJ, note that it counts as a return value (I provide a 'u' placeholder)How-to
Look at the existing functions I have in there. It should be pretty straightforward. Adding libraries, functions, and objects is done by simply adding arrays and defining function bodies.General scripting usage
Once you have a script_state global, and have called Create*State for it, that scripting subsystem should be fully initialized. Yes, you should be able to create multiple states...I don't advise it though.Parsing code for later use
Call script_state::ParseChunk(). This will return a script_hook, which can be used later for...Running code
Call script_state::RunBytecode() with the script hook from ParseChunk(). This will return 1 if it succeeds. There's no way to get return values from code yet, at least not in a non-hackish way.
You can actually use script_parse_args() with GetLuaSession() after calling RunBytecode with Lua to get return values...that won't work at all for Python though, and somewhat breaks the abstraction I'm going for.