Game Development Community

howto: adapting Joe Rossi's lua resource to T3D.

by Orion Elenzil · in Torque 3D Professional · 08/25/2009 (1:39 am) · 2 replies

i haven't tested the Lua resource at all beyond the extremely simple example given below, so caveat coder.

----

adapting Joe Rossi's lua resource to T3D. orion elenzil 200908.
the resource is here: www.garagegames.com/community/resource/view/9665

* download and unzip the zip.

* make a directory "lua" in Engine/lib

* put the lua files right in there. ie so that you have: engine/lib/lua_inc.h

* add the files to the "FPS Genre Kit DLL" project.

* in mainLoop.cpp, and add these lines up near the top, after other includes:
#include "lua/lua_inc.h"     // all lua includes  
lua_State *L=0;  
#include "lua/lua_export.h"  // main bridge classes

* still in mainLoop.cpp, and add these lines after the call to Sim::init() :
//-----------------------------------------------
   // initialize LUA
   L = lua_open();
   lua_gc(L, LUA_GCSTOP, 0);  
   luaL_openlibs(L); 
   Lunar<game>::Register(L);
   lua_gc(L, LUA_GCRESTART, 0);
   
   // oxe - this needs to go after Sim::init()
   //open our bridge objects  
   Con::evaluate("$LuaObject=new LuaObject(lua); lua.docode("tge=game()"); ",0,0);  
   //change dofile function to our tge function so it will print errors to the console  
   Con::evaluate("dofile = function(s) tge:docode('lua.dofile("' ..string.gsub(s,'', '\' ) ..'");' ) end");" ,0,0);  
   //-----------------------------------------------


* in lua-export.h, and un-comment out ifdef LUA_DEBUG stuff around lua_atpanic,
in the LuaObject::LuaObject() constructor.


* still in lua-export.h, remove the pararameter "2" entirely from this function call:
void LuaObject::doCallback(){  Con::executef(this, "onCallback", "string_variable");

* still in lua-export.h, change "execScript" to "Con::execScript".


* in console.h, after the declaration(s) of executef(), add:
void execScript(const char *scriptFile);

* in console.cpp, after the definition(s) of executef(), add:
//------------------------------------------------------------------------------
// execScript for LUA implementation: Jonathon "dishmal" Stevens  
void execScript(const char *scriptFile)
{  
   // execute the script  
   FileStream str;  

   if (!str.open(scriptFile, Torque::FS::File::Read))  
      return;  

   U32 size = str.getStreamSize();  
   char *script = new char[size + 1];  

   str.read(size, script);  
   str.close();  

   script[size] = 0;  
   Con::executef("eval", script);  
   delete[] script;  
}


* i'm not sure what's up with this line in LuaObject::processArguments():
if(!argc) return true; else return true; return false;
i left it as is (compiler warnings and all) but it's possible the correct version is
if(!argc) return true; else return false;


* test it!
at this point the project should compile and run.
to test it, open the torque console and enter:
lua.docode("x = pow(3,3)");
  echo(lua.geti("x"));
you should see "27".



#1
08/25/2009 (7:45 pm)
This resource made me read up on Lua, as I have not yet used it before. It could serve really well as a language that players could use to customize / override the gui for example. Doesn't WoW do that btw?

It looks like it could make a great sandbox. Really interesting stuff, Orion!

Thank you!
#2
08/27/2009 (11:04 pm)
Yes the WoW client uses it actually for all its scripting.

Its a very powerfull language and much easier to learn than python for example. Also its one of the few better WWW backed languages with the nice feature to return more than 1 value and to natively work with associative arrays where you can use anything as index you want, making it great for example for AI (Supreme Commander has the whole game logic, ai etc in LUA accessable for modding), Quests but other things naturally too


thank you for sharing the port :)