creating a table in C

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

creating a table in C

Shea Martin-3
int l_creat_player( lua_State* vm )
{
        lua_newtable( vm );

        int address = (int)&gSomePlayer;

        lua_pushstring( vm, "id" );
        lua_pushinteger( vm, address );
        lua_settable( vm, -3 );

        //Now I would like to add a function to this:

        lua_pushstring( vm, "get_mojo" );
        lua_pushcclosure( vm, l_player_get_mojo, 0 );
        lua_settable( vm, -3 );
        return 1;
}

I register the function as get_player;

My l_player_get_mojo() is like this:

int l_player_get_mojo( lua_State* vm )
{
        lua_getfield( vm, -1, "id" );
        int address = lua_tointeger( vm, -1 );
        Player* p = (Player*)address ;
        lua_pushnumber( vm, p->GetMojo() );
        return true;
}

My script is:

player = get_player()
print( "mojo is " .. player.get_mojo() )

The code dies on getting the "id" field, as the stack does not seem to
have the table on the top of the stack.

I think my problem is how I add the "member function" to the player table.

What am I missing here?

Thanks,
~S


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: creating a table in C

Shea Martin-3
Sorry, wrong newsgroup.
~S

Shea Martin wrote:

> int l_creat_player( lua_State* vm )
> {
> lua_newtable( vm );
>
> int address = (int)&gSomePlayer;
>
> lua_pushstring( vm, "id" );
> lua_pushinteger( vm, address );
> lua_settable( vm, -3 );
>
> //Now I would like to add a function to this:
>
> lua_pushstring( vm, "get_mojo" );
> lua_pushcclosure( vm, l_player_get_mojo, 0 );
> lua_settable( vm, -3 );
> return 1;
> }
>
> I register the function as get_player;
>
> My l_player_get_mojo() is like this:
>
> int l_player_get_mojo( lua_State* vm )
> {
> lua_getfield( vm, -1, "id" );
> int address = lua_tointeger( vm, -1 );
> Player* p = (Player*)address ;
> lua_pushnumber( vm, p->GetMojo() );
> return true;
> }
>
> My script is:
>
> player = get_player()
> print( "mojo is " .. player.get_mojo() )
>
> The code dies on getting the "id" field, as the stack does not seem to
> have the table on the top of the stack.
>
> I think my problem is how I add the "member function" to the player table.
>
> What am I missing here?
>
> Thanks,
> ~S
>
>
> -------------------------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job easier.
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: creating a table in C

Josh Green-3
Shea, it looks like you SHOULD have:

player:get_mojo()

notice the colon not the dot, this calls the function as a member of the
player, and hence, the player table is the first one on the stack :)

--Josh

----Original Message Follows----
From: Shea Martin <[hidden email]>
Reply-To: [hidden email]
To: [hidden email]
Subject: Re: [luabind] creating a table in C
Date: Thu, 01 Feb 2007 15:45:15 -0400

Sorry, wrong newsgroup.
~S

Shea Martin wrote:
 > int l_creat_player( lua_State* vm )
 > {
 > lua_newtable( vm );
 >
 > int address = (int)&gSomePlayer;
 >
 > lua_pushstring( vm, "id" );
 > lua_pushinteger( vm, address );
 > lua_settable( vm, -3 );
 >
 > //Now I would like to add a function to this:
 >
 > lua_pushstring( vm, "get_mojo" );
 > lua_pushcclosure( vm, l_player_get_mojo, 0 );
 > lua_settable( vm, -3 );
 > return 1;
 > }
 >
 > I register the function as get_player;
 >
 > My l_player_get_mojo() is like this:
 >
 > int l_player_get_mojo( lua_State* vm )
 > {
 > lua_getfield( vm, -1, "id" );
 > int address = lua_tointeger( vm, -1 );
 > Player* p = (Player*)address ;
 > lua_pushnumber( vm, p->GetMojo() );
 > return true;
 > }
 >
 > My script is:
 >
 > player = get_player()
 > print( "mojo is " .. player.get_mojo() )
 >
 > The code dies on getting the "id" field, as the stack does not seem to
 > have the table on the top of the stack.
 >
 > I think my problem is how I add the "member function" to the player
table.
 >
 > What am I missing here?
 >
 > Thanks,
 > ~S
 >
 >
 > -------------------------------------------------------------------------
 > Using Tomcat but need to do more? Need to support web services, security?
 > Get stuff done quickly with pre-integrated technology to make your job
easier.
 > Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
 > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job
easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user

_________________________________________________________________
Advertisement: It's simple! Sell your car for just $20 at carsales.com.au  
http://a.ninemsn.com.au/b.aspx?URL=http%3A%2F%2Fsecure%2Dau%2Eimrworldwide%2Ecom%2Fcgi%2Dbin%2Fa%2Fci%5F450304%2Fet%5F2%2Fcg%5F801577%2Fpi%5F1005244%2Fai%5F838588&_t=757768878&_r=endtext_simple&_m=EXT


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user