Exposing object instance to Lua

Started by
24 comments, last by AverageJoeSSU 12 years, 9 months ago
Grawr, Trying to get it (the generated code) to build, but it's throwing linker errors. Specifically of the "multiple definition of 'symbolnamehere'" first defined here.
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
Advertisement
Ok, after some work and moving code around, I have it *almost* working. I'm getting an error from the lua interpreter when trying to run the function

TriggerHandler::AddNew(int type, void *argone, void *argtwo, void *argthree, void *argfour, void *argfive)

through TriggerHandler triggers;

With the lua line:

trigs.triggers.AddNew(mids.EMPTY_MESSAGE, NULL, NULL, NULL, NULL, NULL)

The error message is: Error in AddNew expected 7..7 args, got 6
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
It appears that SWIG is wrapping that function into a standalone function AddNew which takes a TriggerHandler object as its first argument, hence 7 arguments. Why is it doing this?
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
Silly me, I was using the wrong notation. Changed it from trigs.triggers.AddNew... to trigs.triggers:AddNew and it seems to be working. One last question (I think :)): Is there a lua equivalent of typecasting something to a void pointer? (The functions I exposed rely on void pointers to support different argument types since some messages expect ints, others floats, and other strings, etc)

the error in question is:
.\script\test.lua:7: unexpected symbol near ')'

the call is:
trigs.triggers:AddEffectToLast(mids.STOP_AUDIO, 0, NULL, NULL, NULL, NULL)

the five arguments at the end there are all void * in the C++ code

edit: This kind of thing in C++ would look like ... AddEffectToLast(STOP_AUDIO, (void*)0, NULL, NULL, NULL, NULL);

edit: Just in case there is no proper lua way to resolve this, I have cheated a little bit and manually bound (via lua_register) a little helper function which accepts an argument from the lua state (by way of lua_gettop), determines its type, and converts it to a void * in the appropriate manner (from the C++ side). It works :) though it is a sub-optimal solution.
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
Turns out I made a silly little mistake when I was binding my little helper function (accidently bound it to a different little helper function), and I have discovered that I wrote the little helper function all wrong (oops). Consequently, I'm back at square one with regards to casting or converting arguments in lua to void* through whatever means possible

EDIT: I have rewritten the function to bind, but it now cases the program to crash. I'm probably misusing lightuserdata, but I'm uncertain what else to do, I cannot find any mention one way or another about void pointers and lua. Below is the code for the function I'm trying to bind. It is called like trig.triggers:AddNew(mids.HEARTBEAT, VoidP(10), NULL, NULL, NULL, NULL)

int luabind_VoidP(lua_State *L)
{
int argc = lua_gettop(L);
int temp1 = 0;
float temp2 = 0;
void *toreturn;

if(argc != 1)
toreturn = NULL;
if(lua_isnumber(L, 1) == 1) // If this is a number
{
const char *tmpstr = lua_tostring(L, 1);
int slen = strlen(tmpstr);

if(tmpstr[slen-1] == '0' && tmpstr[slen-2] == '.') // Is this a quoted float ending in .0
{
temp2 = (float)lua_tonumber(L, 1);
toreturn = ftov(temp2);
}
else
{
temp2 = (float)lua_tonumber(L, 1);
if(fmod(temp2, 1) == 0) // If this looks like an integer
{
temp1 = (int)temp2;
toreturn = (void *)temp1;
}
else // If this is actually a float
{
toreturn = ftov(temp2);
}
}
}
else if(lua_isstring(L, 1) == 1) // If this is a char or a string
{
toreturn = (void *)lua_tostring(L, 1);
}

lua_pushlightuserdata(L, toreturn);
return 1;
}
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.

I'll perhaps give it another try, a couple questions relevant to that:
1) How do I properly initialize SWIG from the C++ side? (presumably there is a function that needs calling once I've opened my lua state
2) How do I expose the object I have mentioned (as that is really the whole point of this exercise right now)

Looks like owl solved #1. If you still need help with #2, lookee here. It helped me.
It's too bad i came on to this thread late....

My blog has quite a bit of posts on lua.

deadbeefgames.com/blog

I used lunar.h to bind my instances created by my engine.

for example. i have an EngineManager object that provides a "public api" of sorts to Lua. It seems like you are well into using SWIG,

basically using lunar, in my init function for the engine i would push the object onto the lua stack. providing a table of methods like you find in the lunar comment in the lunar.h file.

This has worked incredibly well for me and although it is pretty verbose in c++, i believe this could be automated.

------------------------------

redwoodpixel.com

This topic is closed to new replies.

Advertisement