lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I am a beginner to so don´t listen to me to much...

Anyway, if you want to use share any kind of data between Lua
and your engine you need to use toLua (search the weeb). toLua
have two parts that you need to build, a library file that you need
to link to from your engine and a bin file that you use to create
the binding code.

So how to use it? Well, you must first create create the binding
code to toLua. That's easy. Just copy-paste your header file and
remove all #inlcudes and other code that you don't want to expose
to toLua. Just keep a minimum of code (like function declarations and
variables) in the clean header file. Then execute the toLua bin file from
Visual Studio. In Settings-Debug-Program argument write:
"tolua -o bind.c cleaned_header_file.pkg".

Now you need write some code to expose your data to Lua. That
is not so easy. toLua have helped you alot but you need to create
a script wrapper class of some sort. You can take a look at my
code, but I warn you: there might bee a lot of bugs there becasuse
like you I'm a beginner to Lua to.

// header

extern "C"
 #include <lua.h>
 #include <lualib.h>

#include <tolua.h>

typedef int (*LuaCallback) (lua_State* lua);

class Script
 void RunScript(char* szFile);
 virtual ~Script();

 bool RegisterClass(char *szName,
  LuaCallback o_LuaGet, LuaCallback o_LuaSet);
 bool ExposeObject(const char* szName, void* pkData, char* szClassName);

 void Close();
 bool Open();
 lua_State* m_pkLua;


// cpp

bool Script::RegisterClass(char *szName, LuaCallback o_LuaGet,
         LuaCallback o_LuaSet)
 // Create Lua tag for Test type.
 lua_pushcfunction(m_pkLua, o_LuaGet);
 lua_settagmethod(m_pkLua, tolua_tag(m_pkLua,szName), "getglobal");

 lua_pushcfunction(m_pkLua, o_LuaSet);
 lua_settagmethod(m_pkLua, tolua_tag(m_pkLua,szName), "setglobal");

 return true;

bool Script::ExposeObject(const char* szName, void* pkData, char*
 lua_pushusertag(m_pkLua, pkData, tolua_tag(m_pkLua, szClassName));
 lua_setglobal(m_pkLua, szName);
 return true;

bool Script::Open()
 // Open Lua.
 m_pkLua = lua_open(0);

 // Open automatic generated toLua header files.

 return true;

void Script::RunScript(char *szFile)
 lua_dofile(m_pkLua, szFile);

void Script::Close()

The classes that you expose to Lua must also have this to static functions

 static int LuaSet(lua_State* pkLua)
  Character* var=(Character*) lua_touserdata(pkLua,2);
  Character* val=(Character*) lua_touserdata(pkLua,3);
  return 0;

 static int LuaGet(lua_State* pkLua)
  Character* var=(Character*) lua_touserdata(pkLua,2);
  lua_pushusertag(pkLua, var, tolua_tag(pkLua, "Character"));
  return 1;

Good Luck!

----- Original Message -----
From: "James Turk" <>
To: "Multiple recipients of list" <>
Sent: Monday, January 27, 2003 3:32 AM
Subject: Lua Embedded in Engine

>     Hey, sorry if this gets asked a lot (I imagine it might)  if it gets
> asked a lot just point me somewhere it's been asked before and I wont
> bother the list with my question.  I'm making a general purpose game
> engine in C++ and I'd like to have a way for users to run scripts.
> Since I'm having a hard time putting the full idea into words I'll show
> you an example in psuedocode.
> --test.cpp--
> int x,y;
> ...
> /*perhaps something is needed like engine->ExposeVariable(x);*/
> engine->RunScript("test.lua");
> ...
> --test.lua--
> if(x > 0)
>      y = 1;
> else
>      y = y*2;
> I apologize for the lack of detail in my question, but as I said I'm
> trying to make a general purpose engine, I started to look into lua a
> long time ago but never went far, and I'm under the impression Lua would
> be a good choice to integrate with the engine.
> If anyone can tell me if this is even possible I'd be glad to hear it,
> and suggestions on resources are more than welcome.
>     Thanks,
>        James Turk
> --