I spent last night upgrading the TileEngine game platform from Lua v5.02 to v5.1 and came across a few incompatibilities.
Firstly, the lua_baselibopen() function needs to be replaced with lua_openbase() which will just load the base library, or luaL_openlibs() which will open all of the standard lua libraries such as math, io, string, etc, and the new os (or operating system) library.
Next, the lua_dofile() function has been deprecated. It has a replacement macro luaL_dofile(), which simply calls both luaL_loadfile and lua_pcall and returns the result, however I decided to expand this out manually and correctly capture any errors (which I was lazy before and wasn't checking).
So the old syntax which I used was:
lua_dofile(luaVM, ".\\master.lua");
is now replaced with a luaL_loadfile(), and then lua_pcall()'ed with no params like this c++ code snippet:
int iResult = luaL_loadfile(g_pGameGlobals->luaVM, ".\\master.lua");
if (iResult == 0)
// the master.lua file is loaded, now we'll run it so
// that we get any global declarations and includes etc.
iResult = lua_pcall(g_pGameGlobals->luaVM, 0, 0, 0);
switch (iResult)
{
case 0: // no errors
break;
case LUA_ERRSYNTAX: // syntax error during pre-compilation
{
::MessageBox(g_hWnd,
"Syntax error during pre-compilation",
"Error",
MB_ICONERROR);
exit(LUA_ERRSYNTAX);
break;
}
case LUA_ERRMEM: // memory allocation error.
{
::MessageBox(g_hWnd,
"memory allocation error during load",
"Error",
MB_ICONERROR);
exit(LUA_ERRMEM);
break;
}
case LUA_ERRFILE: // error reading file
{
::MessageBox(g_hWnd,
"error reading file 'master.lua'",
"Error",
MB_ICONERROR);
exit(LUA_ERRFILE);
break;
}
default: // display the error message.
{
const char* szErrorText;
szErrorText = lua_tostring(g_pGameGlobals->luaVM,-1);
::MessageBox(g_hWnd,
szErrorText,
"Error Running 'Master.lua'",
MB_ICONERROR);
exit(iResult);
break;
}
}
As you can see, you simply load the lua chunk and execute it wholesale with lua_pcall(). If there was an error executing it then the error message will be placed on the top of the lua stack, and you can view it with the lua_tostring(luaVM,-1) function.
I should really wrap all of that into a function properly, but that'll happen later.
There is a very similar change for lua_dostring, which has been replaced with a luaL_dostring macro. I use this functionality for the command interface in the game engine.
Those changes were relatively simple. The one that really threw me, was that math.random() stopped working. Any call to random simply returned zero.
My Actor AI relies pretty heavily on random functionality, so all of my actors basically stood still and did nothing.
After an hour or so of debugging and not getting anywhere I ended up posting the question to the very helpful guys and gals on the lua list just before going to bed. By the time that I checked my mail this morning I had a dozen or so helpful emails waiting for me.
The short and simple answer was to create my Direct3D device with the D3DCREATE_FPU_PRESERVE flag. There were other options that I'm going to look into in the future, such as importing or creating a custom random function.
The only other problem I've encountered since upgrading to lua v5.1 (which I haven't had a chance to look into yet) is the inventory icons aren't displaying. I'll hopefully sort that out tonight.