@The Coding Area: Do you mean a sample Lua script, or an example on how to embed Lua into an application?
Doesn't matter, I'll show you both things he he.
A Lua script looks like this:
-- set sync on and sync rate to 60 frames per second
dbSyncOn()
dbSyncRate(60)
-- make a cube
dbMakeObjectCube(1, 10)
-- loop until the escape key is pressed
while dbEscapeKey() == 0 do
-- update screen
dbSync()
end
To embed Lua into your executable, the simplest form is to use toLua++. toLua++ contains a library and a command line tool. The command line tool takes a C header file (in this case it would be "DarkSDK.h"), and generates a .cpp files with the code needed to export the Dark Game SDK fucntions to Lua.
Then, you must create a Lua State, which is a virtual machine capable of running a script:
lua_State* vm = lua_open(); // Now we have a virtual machine capable of running Lua scripts
lua_baselibopen(vm); // This enables the standard Lua functions for this Lua state
tolua_DarSDK_open(vm); // This function is in the .cpp file generated by toLua++. Adds all the DarkSDK functions to the Lua state
lua_loadfile(vm, "main.lua"); // Loads a Lua script
lua_call(vm, 0, LUA_MULTRET); // This is the simplest form of calling a script.
// Simply runs the whole script and then ends
Of course you would write a better script handler. For example, in my game engine (which is a heavily modified version of Cipher), there are three Lua scripts: ui.lua, game.lua and cgame.lua. Each one runs on its own lua_State. These scripts has a function which works as entry point for the script: the main() function (as in C). The game runtime puts the main() parameters on the Lua stack, and then calls the main() function of the corresponding script.
And another cool way of exporting the funtions to Lua, would be writing a C++ wrapper first with object-oriented code. Then the Lua script would look something like this:
-- set sync on and sync rate to 60 frames per second
Sync.On()
Sync.Rate(60)
-- make a cube
cube = ObjectCube:new(10)
-- loop until the escape key is pressed
while Key.Escape() == 0 do
-- update screen
Sync.Do() -- In this sample code, Sync is a table containing functions, not a functions itself, so the Dark Game SDK dbSync() function is wrapper in the Do() function of the Sync table
end
cube:delete()
toLua++ website:
http://www.codenix.com/~tolua/
If you are serious about using Lua, the Programming In Lua book is simply awesome. You can check it online here, but I highly recommend buying it!:
http://www.lua.org/pil/
== Jedive ==
AthlonXP 1600+, 512MB, GeForce4200, WinXP/DX9, Mandrake 10
iBook G4 1Ghz, 256MB, Radeon9200, Mac OS X Panther