lua-users home
lua-l archive

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

We do the following to get a pointer known in lua (like the this pointer):

lua_pushusertag(pLuaState, pPointerToClass, tolua_tag(pLuaState, sTagName));
lua_setglobal(pLuaState, sVarName);

The sTagName is a string representation of the class name. The
pPointerToClass is the "this" pointer or whatever else is needed. The
sVarName is the name under which the pointer is known inside lua.

Hope that helps,


-----Original Message-----
[]On Behalf Of Matt Holmes
Sent: Friday, September 07, 2001 8:01 PM
To: Multiple recipients of list
Subject: Quick tolua question

I have a quick tolua question for someone out there :)

In my MUD server, I am writing the entire scripting engine using Lua. I have
a particular class, CScriptedObject, which is bound directly to Lua. This
means that each CScriptedObject instance is represented in script form by a
Lua script file. This is pretty straightforward. My question revolves around
accessing "this" in the case of the script file. For instance, I have an
event handling mechanism that hands off to the script based on certain
global functions existing, for instance:

function Create()
    Templar:Log("Hello from Create!")

Templar is a special class which is bound to all lua states when they are
created. Now, what I want to be able to do is this:

function Create()
    Templar:Log("Hello from create!")
    this:Set("SomeProperty", 1)

Now of course I know that the Set method must exist as part of the
CScriptedObject class, but what is the best way to refer to "this". Should I
simply push the this pointer on as a global variable in Lua? Something like

lua_pushusertag(m_pState, this, LUA_ANYTAG);
lua_setglobal(m_pState, "this");

Unfortunately, from what I understand of tolua and Lua, this won't work,
because Lua has no clue that "this" is a table that resembles the
CScriptedObject table that we pushed to Lua earlier. How do I tell Lua "hey,
this is a CScriptedObject type table, and here is the instance"?

It seems to me that I am trying to bind things backwards from what tolua
would normally do. I am trying to bind the script to the class, so that the
class owns the script, while tolua seems to bind the class to the script, so
that the script creates and owns instances of the class. If this is the
case, I am guessing that tolua is not the tool I need for this, and
something custom written is going to be the order of the day?

I guess my best solution would be to push a special global C function, call
it Set, which has the "this" pointer pushed as a usertag, and can thus call
the class "Set" method, something like

int LuaSet(lua_State *pState)
    // Get the this pointer in to pClass, code skipped because we all know
how to grab a global
    // Also assume the args are popped from the stack
    pClass->Set(arg1, arg2);

Each class owns its own lua_State, so this should theoretically work as the
"this" pushed to each state will actually be the class instance which owns
the lua_State which is passed to the function.

This seems a bit kludgy though, I would much rather just expose the "this"
pointer as an appropriate table to Lua. Has anyone figured out a way to
easily do that?

Matt "Kerion" Holmes
Lead Developer
Lua Studio - The Lua IDE (and the Templar MUD Engine, oy vey I work on too
much ;)