lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Sorry to be dense, but I am still not following.

My lua script is simply this:

function Event:start()
  self:log( self.name .. " started" )
end

My C is something like this:

if( luaL_newmetatable( vm, "Event.mt" ) )
{
	//trying to duplicat this functionality
  //Event = {}
  //Event.mt = {}
  //
  //function Event:new( pname )
  //  Event.mt.__index = Event
  //  return setmetatable({  name = pname }, Event.mt)
  //end

	//the following two lines are yours
	//I am not quite sure fo their purpose
	lua_pushvalue( vm, -1 ); //put mt on stack
	lua_setfield( vm, -2, "__index" );

	//this is what makes sense to me
	//but it does not work either
	lua_pushstring( vm, "Event" );
	lua_pushstring( vm, "__index" );
	lua_settable( vm, -3 );
}

lua_newtable( vm );

lua_pushstring( vm, "Event:log" );
lua_pushcclosure( vm, l_event_log, 0 );
lua_settable( vm, -3 );

lua_pushstring( vm, "Event:name" );
lua_pushstring( vm, eventname );
lua_settable( vm, -3 );

lua_setmetatable( vm, -2 );

luaL_dofile( vm, "test.lua" );

lua_getfield( vm, LUA_-1, "Event:start" );
lua_pcall( vm,  1, 2, 0 );


When I run my code, I get this interpreter error: " attempt to index global 'Event' (a nil value)".

I am a little lost, this being my first work with and dynamically prototyped language, and also having to explicitly manage the stack.

~S