All pastes #987262 Raw Edit

Stuff

public cpp v1 · immutable
·published 2008-04-16 11:55 UTC
rendered paste body
// MY BINDINGstruct CBaseEntity_wrapper : CBaseEntity, luabind::wrap_base{    CBaseEntity_wrapper( bool bServerOnly=false )        : CBaseEntity(bServerOnly)     {}    virtual void Spawn( void )     {         call<void>("Spawn");     }    static void default_Spawn(CBaseEntity* ptr)    {        return ptr->CBaseEntity::Spawn();    }};    luabind::class_<CBaseEntity, CBaseEntity_wrapper>("CBaseEntity")  // Export CBaseEntity (only some members here)      .def(luabind::constructor<bool>()) // if bool is yes then its server side only apparently	  .def("KeyValue", (bool(CBaseEntity::*)(const char*, const char*)) &CBaseEntity::KeyValue ) // in the cbaseentity or whatever its called header there are many diffrent types of this function ;)	  .def("SetAbsOrigin", &CBaseEntity::SetAbsOrigin )	  .def("SetAbsAngles", &CBaseEntity::SetAbsAngles )	  .def("GetAbsOrigin", &CBaseEntity::GetAbsOrigin )	  .def("GetAbsAngles", &CBaseEntity::GetAbsAngles )	  .def("Precache", &CBaseEntity::Precache )	  .def("Spawn", &CBaseEntity::Spawn, &CBaseEntity_wrapper::default_Spawn)	  .def("SetModel", &CBaseEntity::SetModel )	  .def("SetSolid", &CBaseEntity::SetSolid )	  .def("GetClassname", &CBaseEntity::GetClassname )	  .def("entindex", &CBaseEntity::entindex )	  .def("GetAbsAngles", &CBaseEntity::GetAbsAngles )	  .def("SetHealth", &CBaseEntity::SetHealth )	  //.def("EmitSound", &CBaseEntity::EmitSound )	  .def("GetAbsOrigin", &CBaseEntity::GetAbsOrigin ),	  class 'CMyEnt' (CBaseEntity)function CMyEnt:__init() super(true) -- o_o the true in constructor of cbaseentity means server side only remember!endfunction CMyEnt:Spawn() --overwrite the spawn function	Msg("HELLO")endLINK_ENTITY_TO_CLASS( 'CMyEnt' )//////////// MEANWHILE IN C++class LuaEntityFactory : public IEntityFactory{public:    IServerNetworkable *Create( const char *pClassName )    {		char fstr[ 512 ];		Q_snprintf( fstr, sizeof( fstr ), "return %s()", pClassName);		int status = luaL_dostring( luaState, fstr );		//if ( status != 0 ) handle_the_error();		luabind::object obj( luabind::from_stack(luaState, -1) );        if ( !obj )        {			cvar->ConsoleColorPrintf( LUA_ERROR_PRINT_COLOUR, "[Lua] LINK_ENTITY_TO_CLASS, Hey put a valid object in it will you!\n");            return NULL;        }        CBaseEntity* pEnt;        try {            pEnt = luabind::object_cast<CBaseEntity*>(obj);        } catch ( const luabind::cast_failed& e ) {            // deal with it			cvar->ConsoleColorPrintf( LUA_ERROR_PRINT_COLOUR, "[Lua] LINK_ENTITY_TO_CLASS, Unable to cast your object to its base class!\n");            return NULL;        }        pEnt->PostConstructor(pClassName);        return pEnt->NetworkProp();    }     void Destroy( IServerNetworkable *pNetworkable )    {        if ( pNetworkable )        {            pNetworkable->Release();        }    }     virtual size_t GetEntitySize()    {        return 0;  // TODO: we could dispatch this to Lua somehow....    }};static void LUA_LINK_ENTITY_TO_CLASS( const char* pClassName ){    static LuaEntityFactory lua_entity_factory;    EntityFactoryDictionary()->InstallFactory( &lua_entity_factory, pClassName );}