lua-users home
lua-l archive

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


On Aug 7, 2005, at 6:48:11, Shmuel Zeigerman wrote:
It seems the problems are in the UKAdventureNewRoom
and/or UKAdventureRoomToString. Could you post them?

Sure. Here they are:

// ------------------------------------------------------------------------ -----
//    UKAdventureRoomToString:
// Makes sure printing a room says more than just "userdata xxxx". // ------------------------------------------------------------------------ -----

int        UKAdventureRoomToString( lua_State *pLuaState )
{
    UKRoom*        self = UKAdventureRoomGetThis( pLuaState );

    lua_pushfstring( pLuaState, "Room(%lx)", (unsigned long)self );

    return 1;
}


// ------------------------------------------------------------------------ -----
//    UKAdventureNewRoom:
//        Constructor function for creating new rooms.
// ------------------------------------------------------------------------ -----

int    UKAdventureNewRoom( lua_State *pLuaState )
{
UKRoom* * newRoom = (UKRoom* *) lua_newuserdata( pLuaState, sizeof(UKRoom*) );

// Set a metatable so we can identify objects of this class and give them metamethods:
    luaL_getmetatable( pLuaState, "PACEmaker.Room" );
    lua_setmetatable( pLuaState, -2 );

    // Actually create a corresponding C++ object:
    *newRoom = new UKRoom();

    return 1;  /* new userdatum is already on the stack */
}


// ------------------------------------------------------------------------ -----
//    UKAdventureDeleteRoom:
// Destructor function for disposing of rooms. Called by the garbage
//        collector as needed.
// ------------------------------------------------------------------------ -----

int    UKAdventureDeleteRoom( lua_State *pLuaState )
{
    UKRoom*        theRoom = UKAdventureRoomGetThis( pLuaState );

    if( theRoom )
    {
        delete theRoom;
        printf( "Deleted room.\n" );
    }
    else
        printf( "No room to delete.\n" );

    return 0;
}

// ------------------------------------------------------------------------ -----
//    UKAdventureRoomGetThis:
// Function for getting the "this" parameter for a room method. If the // "this" object isn't a room, this throws an error (lua-style longjmp). // ------------------------------------------------------------------------ -----

UKRoom *    UKAdventureRoomGetThis( lua_State *pLuaState )
{
UKRoom** ud = (UKRoom**) luaL_checkudata( pLuaState, 1, "PACEmaker.Room" );
    luaL_argcheck( pLuaState, ud != NULL, 1, "`Room' expected" );

    return (UKRoom *) *ud;
}



Cheers,
-- M. Uli Kusterer
http://www.zathras.de