[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: creating a table in C
- From: "Jeremy Darling" <jeremy.darling@...>
- Date: Thu, 1 Feb 2007 13:42:57 -0600
Your script should be:
player = get_player()
print( "mojo is " .. player:get_mojo() )
This way, self is passed as the first argument to get_mojo. With the . operator, you are not passing self, and would need to call it like this:
player.get_mojo(player)
Basically its the way that functions are called from tables, : means send self reference, . means don't send I'll specify it.
On 2/1/07,
Shea Martin <shea08@eastlink.ca> 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
--
Jeremy
"Help I suffer from the oxymoron Corporate Security."