Receiving C++ Object Reference in Lua

Phlox Icon <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
I want to use a Lua script to alter an object that was created, modified and
bound in C++. I'm experienced in C++ but not in Lua. I've been looking at
tutorials but I can't figure out how I should do this. I'm wondering if
there's a specific LuaBind method of doing this.
I basically want to get a pointer to an object in C++ so I can perform some
functions on it through script. The pseudo code would be like:
void Guard::RunAI {
referenceToObject = this
do the following lua{
Guard* g = referenceToObject
g:patrol()
}

I've looked at the tutorials:
http://lua-users.org/wiki/UserDataWithPointerExample - but this assumes the
call for the object to be created is done in Lua
http://lua-users.org/wiki/CppObjectBinding - this is much closer to what I
want but this requires that the class be in a wrapper. I can't do this
because I have hundreds of classes that I'd have to write a wrapper for,
though, I could probably use a template. Also, I want my objects to appear
in the Lua file by their class name as LuaBind let's me rather than using
the wrappers name.

I only need a pointer to my object so I've been focusing on light userdata
( lua_pushlightuserdata ). Still, I've had no luck:
void AIManager::Spawn() {
...
guard = new Guard();
...
}

void Guard::RunAI() {
 lua_pushlightuserdata(L, (void*)this);
lua_setglobal(L,"guard"); // lame method
 // (Guard*)lua_touserdata(L, 1);

luaL_dostring(
 L,
 "g = Guard(guard)\n" // trying to give Lua control of a guard made in C++
using horrible method
 "g:Test()\n" // just prints message to see if it worked.
 );
}
This code doesn't compile because I don't have a Guard constructor that
takes a LuaState for the reason mentioned earlier (too many classes). I'm
looking for a better way of doing this but I'm beginning to settle on the
idea of anything that works.

I have almost no idea what I'm doing here. Any idea on how I might do this?
Thanks.

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july

_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.