Thread: Lua "Class" problem

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,735

    Lua "Class" problem

    I can "create" the class alright via this:
    Code:
    local dir = LakeDir(Lake.getcwd())
    if not dir then error('Could not open our directory!') end
    local ent = dir:next()
    if not ent then error('No entries!') end
    dir:shut()
    dir = nil
    ent = nil
    With these as the backend
    Code:
    static int LakeDir_next( lua_State *L ) {
    	LakeDir_t *d = (LakeDir_t*)lua_touserdata(L,1);
    	if ( !d ) {
    		puts("WTF!? We have no object!");
    		lua_pushnil(L);
    		return 1;
    	}
    	else if ( (d->ent = readdir(d->dir)) != NULL )
    		return LakeDir__next( L, d );
    	puts("readdir returned NULL!");
    	lua_pushnil(L);
    	return 1;
    }
    ...
    static int LakeDir( lua_State *L ) {
    	const char *path = luaL_checkstring( L, 1 );
    	LakeDir_t *d = (LakeDir_t*)lua_newuserdata(L, sizeof(LakeDir_t) );
    	if ( !d ) {
    		lua_pushnil(L);
    		return 1;
    	}
    	memset( d, 0, sizeof(LakeDir_t) );
    	luaL_getmetatable( L, LAKEDIR_META );
    	lua_setmetatable( L, -2 );
    	d->dir = opendir( path );
    	if ( !d->dir ) {
    		lua_pushnil(L);
    		return 1;
    	}
    	lua_newtable(L);
    	lua_pushstring( L, "next" );
    	lua_pushcfunction( L, LakeDir_next );
    	lua_settable( L, -3 );
    	lua_pushstring( L, "shut" );
    	lua_pushcfunction( L, LakeDir_gc );
    	lua_settable( L, -3 );
        return 1;
    }
    Resulting in this output:
    Code:
    "O:\Common\Lake64\lake64" (in directory: O:\Data\C_Playground\cheat-engine-master\Cheat Engine)
    Loading lua file...
    WTF!? We have no object!
    Failed to run makefile.lua
    Fatal Lua Error!
    makefile.lua:5: No entries!
    stack traceback:
    Compilation failed.
    Since I still don't quite understand how to use the Lua "class" related c functions I need some help understanding how I got to the least expected result of no object at all
    Last edited by awsdert; 01-10-2019 at 04:08 PM. Reason: typo

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,735
    Found a VERY helpful guide, 4. Making Your API Classy - Creating Solid APIs with Lua [Book] and am now using it to rectify C side errors such as no checking of the userdata (which flagged an error on the above script once I compiled and ran it)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A "Simple" Array/String Problem. "Help" ASAP needed
    By AirulFmy in forum C Programming
    Replies: 10
    Last Post: 08-19-2015, 04:30 AM
  2. Replies: 2
    Last Post: 12-08-2014, 08:12 PM
  3. the operator "="error in class "String"
    By boyhailong in forum C++ Programming
    Replies: 9
    Last Post: 07-27-2011, 08:39 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread