Skip to main content
gamedev.net
🔒 Locked

LUA: return exposed object

Started by nickem Sep 15, 2005 at 3:57 AM 2 replies 1.3k views
Original Post
nickem
nickem
In code, I can explain my problem better:

int li_GetPixel( lua_State *l )
{
    COLORREF c = GetPixel( GetDC( 0 ), lua_tonumber( l, 1 ), lua_tonumber( l, 2 ) );
    LRGB rgb;

    rgb.r = GetRValue( c );
    rgb.g = GetRValue( c );
    rgb.b = GetRValue( c );

    //now I want to return rgb, how would I do this?
    //I've already exposed this class via LuaBind
    //I thought it would be something easy like lua_pushuserdata()
    //but I'm guessing it ain't that easy(also there's no such thing) :)
}
Thanks in advance.
Electrovolt
Electrovolt
I'm pretty new to lua, but I've used it to pass around pointers.

In C:

// Host function in C
int HostCreateEffect(lua_State *psLuaState)
{
CEffect *psEffect;

// Create the effect
// ... some code to create the effect ...
// psEffect now stores pointer to effect

// Push pointer to the effect onto the stack so we can return it
lua_pushlightuserdata(psLuaState, psEffect);

// We are return one value (the pointer)
return 1;
}

In Lua:

-- call host function to create the effect
psEffect = CreateEffect("fire")



Don't know if that helps. Ofcourse, since your rgb value is a local variable I guess its not going to work passing it as a pointer, so you'll either need to malloc it, have it globally defined somewhere, or rather just return the three r,g,b values separetely.
ajones
ajones
Hi nickem.

There are a number of ways to export a value to Lua, and which one you use depends on what you've done elsewhere to expose your LRGB class. I know you said that you used LuaBind, but I'm not very familiar with how LuaBind works (I'm using a home-brewed export library), so these might not be as useful as I'd like, but bear with me [wink].

1) The first thing you should do is take a look at the LuaBind website, their documentation and forum/wiki (wherever that is...). There should be an example somewhere regarding how to return exposed classes from a custom function.

2) If I remember correctly, LuaBind allows you to export functions via its module interface - LuaBind does all the argument conversions for you. Can't you do something like this?

// LuaBind should handle the conversion of arguments for you// You should expose the function via LuaBind as "GetPixel"// you can call this function from lua as GetPixel( 13, 30 )LRGB exported_GetPixel( int x, int y ){    COLORREF c = GetPixel( GetDC(0), x, y );    LRGB rgb;    // set up rgb    return rgb;}


3) Lua can handle more than one return value - you could do:

int li_GetPixel( lua_State *l ){    // ... the rest of your function    lua_pushnumber(rgb.r);    lua_pushnumber(rgb.g);    lua_pushnumber(rgb.b);    return 3;}


And then Lua could collect these bits in a construction function:

local function FormLRGB(r,g,b)    -- assign r, g and b to a new LRGB object    -- with an appropriate metatableendmyRGB = FormLRGB( li_GetPixel(13,54) )


4) You could get lua to create a new userdatum via the lua_newuserdata call, then do some additional wizardry to get it working alongside LuaBind, but to be honest, it probably isn't worth it - you would have to do some digging in the LuaBind documentation and/or code before this one worked - I'd stick with 1, 2 or 3.

Hope this helps - good luck!

Cheers,
Jones.
nickem
nickem
Thanks! I got it working. I wasn't exporting my C++ functions with LuaBind(rather just Lua), after I did that I used your 2nd option.

Thanks.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.