// MY BINDING struct CBaseEntity_wrapper : CBaseEntity, luabind::wrap_base { CBaseEntity_wrapper( bool bServerOnly=false ) : CBaseEntity(bServerOnly) {} virtual void Spawn( void ) { call("Spawn"); } static void default_Spawn(CBaseEntity* ptr) { return ptr->CBaseEntity::Spawn(); } }; luabind::class_("CBaseEntity") // Export CBaseEntity (only some members here) .def(luabind::constructor()) // 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! end function CMyEnt:Spawn() --overwrite the spawn function Msg("HELLO") end LINK_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(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 ); }