(*ch)->name;
(*ch)->name;
@@
@@ch->name <=> (*ch).name
@@
(*(*ch)).name;
ch.level = 35 – calls __newindex in C
if ch.level == 10 – calls __index in C
static int CHL_index( lua_State *L )
{
CHAR_DATA *ch = L_getchar(L);
char *field;
field = str_dup( luaL_checkstring(L, 2));
if (!str_cmp(field, "name"))
lua_pushstring(L, ch->name);
return 1;
}
/* registry for charlib_m (metatable) */
static const struct luaL_reg charlib_m [] = {
{"send", CHL_send},
{"__newindex", CHL_newindex},
{"__index", CHL_index},
{NULL, NULL}
};
function gettable_event (table, key)
local h
if type(table) == "table" then
local v = rawget(table, key)
if v ~= nil then return v end
h = metatable(table).__index
if h == nil then return nil end
else
h = metatable(table).__index
if h == nil then
error()
end
end
if type(h) == "function" then
return (h(table, key)) – call the handler
else return h[key] – or repeat operation on it
end
end
const char * name = luaL_optstring (L, 1, "self");
LUA_FUNCTION( LCharacter::__index ) {
CHAR_DATA *ch = CHECK_CHARACTER( L, 1 );
const char *field = luaL_checkstring( L, 2 );
// …
}
static int CHL_goTo( lua_State *L )
{
CHAR_DATA *ch = L_getchar(L);
const char *argument = lua_tostring(L, 2);
do_goto(ch, (char*)argument);
return 0;
}
Note, though, that you can also just store the pointer directly instead of using the whole CHAR_DATA** business – and then get a CHAR_DATA* out.