### GMOD_MODULE_OPEN Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/macros-and-entry-points.md Example of initializing a module. ```cpp GMOD_MODULE_OPEN() { LUA->PushCFunction(MyFunction); LUA->SetField(GarrysMod::Lua::INDEX_GLOBAL, "MyFunction"); return 0; } ``` -------------------------------- ### CreateProject Example (Server) Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/configuration.md Example of creating a server-side project. ```lua -- Server project CreateProject({ serverside = true, manual_files = false }) ``` -------------------------------- ### CreateWorkspace Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/configuration.md Example usage of the CreateWorkspace function. ```lua CreateWorkspace({ name = "MyProject", allow_debug = true, path = "build" }) ``` -------------------------------- ### Complete Configuration Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/configuration.md A comprehensive example of a premake5.lua configuration file. ```lua -- premake5.lua PROJECT_GENERATOR_VERSION = 3 newoption({ trigger = "gmcommon", description = "Path to garrysmod_common", value = "path" }) local gmcommon = assert(_OPTIONS.gmcommon or os.getenv("GARRYSMOD_COMMON"), "garrysmod_common not found") include(gmcommon) -- Create workspace CreateWorkspace({ name = "MyModules", allow_debug = true, path = "build" }) -- Create server module CreateProject({ serverside = true, manual_files = false }) -- Include libraries IncludeGOOMod() IncludeHelpersExtended() IncludeSDKCommon() IncludeSDKTier0() IncludeSDKTier1() -- Build options per compiler filter { "action:vs*" } -- Visual Studio settings defines { "_CRT_SECURE_NO_WARNINGS" } filter { "action:gmake" } -- GCC/Linux settings buildoptions { "-std=c++17" } filter { "action:xcode4" } -- Xcode/macOS settings -- Reset filter filter {} -- File configuration if _OPTIONS.manual_files then files { "src/**.cpp", "src/**.h" } end ``` -------------------------------- ### ModuleBase Initialize Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/module-system.md Example of overriding the Initialize method. ```cpp int MyModule::Initialize(Lua::ILuaBase *LUA) { // Register functions LUA->CreateMetaTable("MyType"); return 0; } ``` -------------------------------- ### ModuleBase PushFunction Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/module-system.md Example of using PushFunction. ```cpp int MyFunc(Lua::ILuaBase *LUA) { LUA->PushNumber(42); return 1; } void MyModule::Initialize(Lua::ILuaBase *LUA) { PushFunction(LUA, MyFunc); LUA->SetField(GarrysMod::Lua::INDEX_GLOBAL, "MyFunction"); } ``` -------------------------------- ### Top Method Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluabase.md Demonstrates how to get the current number of values on the Lua stack. ```cpp int stackSize = LUA->Top(); ``` -------------------------------- ### ModuleLoader Integration Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/macros-and-entry-points.md Example of using ModuleLoader to load a dynamic module and get its entry point. ```cpp #include SourceSDK::ModuleLoader loader("mymodule"); if (!loader.IsValid()) { throw std::runtime_error("Module load failed"); } // Get entry point typedef int (*ModuleOpen)(lua_State*); ModuleOpen mod_open = (ModuleOpen)loader.GetSymbol("gmod13_open"); ``` -------------------------------- ### Usage Example: Auto-installing Compiled Files Source: https://github.com/danielga/garrysmod_common/blob/master/readme.md This shows how to configure your project to automatically install compiled files to a specified directory, either through command-line flags or configuration settings. ```bash # Append the --autoinstall flag to your command to either use the GARRYSMOD_LUA_BIN environment var, automatic path finder or the DEFAULT_GARRYSMOD_LUA_BIN_DIRECTORY config (which you have to define yourself in config.lua). # Append the --autoinstall=path config to your command to use the path you want. ``` -------------------------------- ### SDK Inclusion Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/configuration.md Example demonstrating the inclusion of multiple SDK components. ```lua IncludeGOOMod() IncludeHelpersExtended() IncludeLuaShared() IncludeDetouring() ``` -------------------------------- ### ReferenceCreate Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluabase.md Example demonstrating the usage of ReferenceCreate and ReferencePush. ```cpp LUA->PushNumber(42); int ref = LUA->ReferenceCreate(); // stack popped // Later: LUA->ReferencePush(ref); // Push back the referenced value ``` -------------------------------- ### CreateProject Example (Client with Manual Files) Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/configuration.md Example of creating a client-side project with manual file inclusion. ```lua -- Client project with manual files CreateProject({ serverside = false, manual_files = true }) files { "src/client/*.cpp" } ``` -------------------------------- ### CreateObject Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluainterface.md Example of creating and using an ILuaObject wrapper. ```cpp ILuaObject *obj = LUA->CreateObject(); obj->PushString("hello"); // ... use object ... LUA->DestroyObject(obj); ``` -------------------------------- ### CreateMetaTable Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluabase.md Example of creating a metatable and getting its type ID. ```cpp int typeId = LUA->CreateMetaTable("MyType"); ``` -------------------------------- ### GetBinaryFileName Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/helpers.md Example demonstrating platform-specific binary filename generation. ```cpp std::string filename = GetBinaryFileName("engine"); // Windows: "engine.dll" // Linux: "libengine_srv.so" // macOS: "engine.dylib" ``` -------------------------------- ### AutoStack GetStackDifference Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example showing how to get the current stack difference using AutoStack. ```cpp AutoStack stack(LUA); LUA->PushNumber(1); LUA->PushNumber(2); assert(stack.GetStackDifference() == 2); ``` -------------------------------- ### Global Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluainterface.md Example of accessing the global table and its members. ```cpp ILuaObject *globals = LUA->Global(); globals->GetMember("Entity", entity_obj); ``` -------------------------------- ### Complete Module Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/macros-and-entry-points.md A comprehensive example of a GMod module, including function registration and module table creation. ```cpp #include // Function implementations LUA_FUNCTION(Echo) { const char *text = LUA->CheckString(1); LUA->PushString(text); return 1; } LUA_FUNCTION(Multiply) { double a = LUA->CheckNumber(1); double b = LUA->CheckNumber(2); LUA->PushNumber(a * b); return 1; } LUA_FUNCTION_STATIC(Version) { LUA->PushNumber(1.0); return 1; } // Module entry GMOD_MODULE_OPEN() { Msg("MyModule loading...\n"); // Register functions LUA->PushCFunction(Echo); LUA->SetField(GarrysMod::Lua::INDEX_GLOBAL, "Echo"); LUA->PushCFunction(Multiply); LUA->SetField(GarrysMod::Lua::INDEX_GLOBAL, "Multiply"); // Module table LUA->CreateTable(); { LUA->PushCFunction(Version); LUA->SetField(-2, "Version"); LUA->PushString("MyModule 1.0"); LUA->SetField(-2, "Name"); } LUA->SetField(GarrysMod::Lua::INDEX_GLOBAL, "MyModule"); return 0; } // Module cleanup GMOD_MODULE_CLOSE() { Msg("MyModule unloading...\n"); return 0; } ``` -------------------------------- ### RunString Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluainterface.md Example usage of the RunString function. ```cpp LUA->RunString("default", "", "print('Hello')", true, true); ``` -------------------------------- ### LUA_FUNCTION_STATIC Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/macros-and-entry-points.md Example of a static Lua function. ```cpp LUA_FUNCTION_STATIC(PrivateHelper) { LUA->PushString("internal"); return 1; } ``` -------------------------------- ### Using Predefined Symbols Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/helpers.md Example of how to use predefined symbols to resolve a factory function. ```cpp #include // Get the FileSystem factory for (const auto &sym : Symbols::FileSystemFactory) { // Try each symbol variant void *factory = ResolveSymbol(sym); if (factory) break; } // Direct symbol access typedef void *(*FactoryFunc)(const char *, int*); FactoryFunc factory = (FactoryFunc)ResolveSymbol(Symbols::FileSystemFactory[0]); ``` -------------------------------- ### GMOD_MODULE_CLOSE Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/macros-and-entry-points.md Example of cleaning up a module. ```cpp GMOD_MODULE_CLOSE() { Msg("Module unloading\n"); return 0; } ``` -------------------------------- ### Example: Loading and Using an Interface Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/helpers.md Demonstrates how to load a module and retrieve a typed interface using SourceSDK::FactoryLoader. ```cpp #include #include // Load the engine module SourceSDK::FactoryLoader engine_loader("engine"); if (!engine_loader.IsValid()) { throw std::runtime_error("Failed to load engine module"); } // Get a typed interface class IEngine { }; // Declare interface class IEngine *engine = engine_loader.GetInterface("IEngine001"); if (!engine) { throw std::runtime_error("Failed to get IEngine interface"); } // Use the interface engine->DoSomething(); ``` -------------------------------- ### AutoStack Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example demonstrating automatic stack cleanup using AutoStack. ```cpp int MyFunction(ILuaBase *LUA) { // Automatically manage stack cleanup { AutoStack stack(LUA); // These are automatically cleaned up LUA->PushNumber(1); LUA->PushString("test"); LUA->CreateTable(); // ... use the values ... } // Stack automatically cleaned here // Safe to return LUA->PushNumber(result); return 1; } ``` -------------------------------- ### Auto-Installation Method 1: Command Line Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/configuration.md Command line for automatic installation of compiled binaries. ```bash premake5 gmake --autoinstall ``` -------------------------------- ### PushSpecial Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluabase.md Example showing how to push a special value like the global table. ```cpp LUA->PushSpecial(SPECIAL_GLOB); // Push _G ``` -------------------------------- ### Example: Symbol Resolution Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/helpers.md Illustrates creating symbols using signature patterns, names, and offsets. ```cpp #include // Create a signature-based symbol const char pattern[] = "\x55\x8B\xEC\x8B\x2A\x5D\xC3"; auto sym = Symbol::FromSignature(pattern); // Create a name-based symbol auto sym_by_name = Symbol::FromName("ProcessMessages"); // Create symbol with offset auto sym_with_offset = Symbol::FromSignature(pattern, 10); ``` -------------------------------- ### Example Constraint for Internal Classes Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/configuration.md An example constraint to enforce release mode and specific macOS SDK versions. ```cpp #if defined(_DEBUG) || (defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED < 1070) #error "GameDepot::System requires release mode and macOS 10.7+" #endif ``` -------------------------------- ### FactoryLoader Constructor Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/helpers.md Loads a module and retrieves its CreateInterface factory function. ```cpp SourceSDK::FactoryLoader factory_loader("engine"); ``` -------------------------------- ### CFunc Type Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/macros-and-entry-points.md Example of a C function callback and its registration. ```cpp int MyCallback(lua_State *L) { GarrysMod::Lua::ILuaBase *LUA = L->luabase; LUA->SetState(L); LUA->PushNumber(42); return 1; } // Register LUA->PushCFunction(MyCallback); LUA->SetField(GarrysMod::Lua::INDEX_GLOBAL, "MyCallback"); ``` -------------------------------- ### Msg Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluainterface.md Example of using Msg to print a formatted message to the console. ```cpp LUA->Msg("Player count: %d\n", count); ``` -------------------------------- ### NewGlobalTable Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluainterface.md Example of creating a new global table and setting a value within it. ```cpp LUA->NewGlobalTable("MyGlobal"); LUA->PushNumber(42); LUA->SetField(-2, "value"); ``` -------------------------------- ### Auto-Installation Method 2: Specific Path Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/configuration.md Command line for automatic installation to a specific Garry's Mod path. ```bash premake5 gmake --autoinstall=/path/to/garrysmod ``` -------------------------------- ### ModuleBase Think Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/module-system.md Example of overriding the Think method. ```cpp int MyModule::Think(Lua::ILuaBase *LUA) { // Per-frame logic return 0; } ``` -------------------------------- ### CreateConVar Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluainterface.md Example of creating a console variable accessible from Lua and C++. ```cpp ConVar *cv = LUA->CreateConVar("my_var", "100", "My variable", 0); ``` -------------------------------- ### AutoStack Constructor Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example demonstrating the usage of AutoStack to manage the Lua stack. ```cpp { AutoStack stack(LUA); LUA->PushNumber(1); LUA->PushString("test"); // Stack automatically cleaned on scope exit } ``` -------------------------------- ### CreateTable Method Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluabase.md Illustrates creating a new table and setting a field within it. ```cpp LUA->CreateTable(); LUA->SetField(-1, "name", "value"); ``` -------------------------------- ### Optional Arguments Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/macros-and-entry-points.md Shows how to handle optional arguments in a GMod Lua function. ```cpp #include LUA_FUNCTION(Greet) { const char *name = LUA->GetString(1, nullptr); const char *greeting = LUA->GetString(2, nullptr); if (name == nullptr) name = "Friend"; if (greeting == nullptr) greeting = "Hello"; LUA->PushFormattedString("%s, %s!", greeting, name); return 1; } ``` -------------------------------- ### GetUpvalueIndex Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/macros-and-entry-points.md Demonstrates how to access upvalues in a closure using GetUpvalueIndex. ```cpp #include LUA_FUNCTION(MyClosure) { // Access first upvalue double base = LUA->GetNumber(LUA->GetUpvalueIndex(1)); double arg = LUA->CheckNumber(1); LUA->PushNumber(base + arg); return 1; } // Register with upvalues LUA->PushNumber(100); // Upvalue LUA->PushCClosure(MyClosure, 1); // Closure with 1 upvalue LUA->SetField(GarrysMod::Lua::INDEX_GLOBAL, "MyClosure"); ``` -------------------------------- ### LUA_FUNCTION_DECLARE Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/macros-and-entry-points.md Example of declaring a Lua function in a header. ```cpp // In header LUA_FUNCTION_DECLARE(PublicFunction); // In source int PublicFunction(lua_State *L) { // Implementation return 0; } ``` -------------------------------- ### Call Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluabase.md Illustrates calling a function with specified arguments and return values. ```cpp LUA->PushCFunction(myfunc); LUA->PushNumber(1); LUA->PushString("arg"); LUA->Call(2, 1); // Call with 2 args, keep 1 result ``` -------------------------------- ### Complete Module Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/module-system.md A comprehensive example demonstrating the structure and functionality of a Garry's Mod module, including initialization, deinitialization, frame-based thinking, and registration of Lua functions and closures. ```cpp #define GMOD_MODULE_NAME "example" #include class ExampleModule : public GarrysMod::Module { private: int counter = 0; public: ExampleModule(const std::string& module_name) : GarrysMod::Module(module_name) {} int Initialize(GarrysMod::Lua::ILuaBase *LUA) override { // Register a function PushMemberFunction(LUA, &ExampleModule::Increment); LUA->SetField(GarrysMod::Lua::INDEX_GLOBAL, "Increment"); // Register a closure LUA->PushNumber(100); // Upvalue PushMemberClosure(LUA, &ExampleModule::AddValue, 1); LUA->SetField(GarrysMod::Lua::INDEX_GLOBAL, "AddValue"); // Enable per-frame thinking EnableThink(LUA); return 0; } int Deinitialize(GarrysMod::Lua::ILuaBase *LUA) override { Msg("Module shutting down\n"); return 0; } int Think(GarrysMod::Lua::ILuaBase *LUA) override { // Called every frame return 0; } int Increment(GarrysMod::Lua::ILuaBase *LUA) { ++counter; LUA->PushNumber(counter); return 1; } int AddValue(GarrysMod::Lua::ILuaBase *LUA) { // GetUpvalueIndex(1) gets the first upvalue double base = LUA->GetNumber(LUA->GetUpvalueIndex(1)); double add = LUA->GetNumber(1); LUA->PushNumber(base + add); return 1; } }; // Module entry point (Garry's Mod calls this) GMOD_MODULE_OPEN() { auto factory = ExampleModule::CreateModuleFactory(); // Module system handles creation and lifecycle return 0; } GMOD_MODULE_CLOSE() { // Cleanup return 0; } ``` -------------------------------- ### Build with auto-installation Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/README.md Instructions for building and automatically installing a module using premake5 with the --autoinstall flag. ```bash export GARRYSMOD_LUA_BIN=/path/to/garrysmod premake5 gmake --autoinstall make install ``` -------------------------------- ### PCall Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example of using PCall with AutoHook to safely call a function and handle potential errors. ```cpp AutoHook hook(LUA, "PlayerSpawn"); LUA->PushNumber(player_id); if (hook.PCall()) { SpawnPlayer(player_id); } ``` -------------------------------- ### GetUserType Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluabase.md Example demonstrating how to retrieve a typed pointer from UserData. ```cpp MyObject *obj = LUA->GetUserType(1, my_type_id); if (obj) { obj->DoSomething(); } ``` -------------------------------- ### AutoObject Default Constructor Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example demonstrating the creation of an invalid AutoObject. ```cpp AutoObject obj; assert(!obj.IsValid()); ``` -------------------------------- ### SetTable Method Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluabase.md Shows how to set table[key] = value, where table, key, and value are on the stack. ```cpp LUA->SetTable(1); ``` -------------------------------- ### IsType Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluabase.md Example of checking if a value at a stack position is a string. ```cpp if( LUA->IsType(1, GarrysMod::Lua::Type::String) ) { // Argument 1 is a string } ``` -------------------------------- ### Sub-Module Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/module-system.md An example demonstrating how to create and register a sub-module, which can be used for organizing functionality within a larger module system. ```cpp #include class DataModule : public GarrysMod::SubModule { private: int value = 0; public: int Initialize(GarrysMod::Lua::ILuaBase *LUA) override { PushMemberFunction(LUA, &DataModule::GetValue); LUA->SetField(GarrysMod::Lua::INDEX_GLOBAL, "GetValue"); return 0; } int GetValue(GarrysMod::Lua::ILuaBase *LUA) { LUA->PushNumber(value); return 1; } }; // Register as sub-module auto factory = DataModule::CreateSubModuleFactory(); ``` -------------------------------- ### LUA_FUNCTION Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/macros-and-entry-points.md Example of a Lua function that adds two numbers. ```cpp LUA_FUNCTION(Add) { double a = LUA->CheckNumber(1); double b = LUA->CheckNumber(2); LUA->PushNumber(a + b); return 1; } ``` -------------------------------- ### Push Method Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluabase.md Shows how to copy a value from a specific stack position to the top. ```cpp LUA->Push(1); // Copy first argument to top ``` -------------------------------- ### Complete Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluaobject.md A comprehensive example demonstrating the creation and manipulation of Lua objects using ILuaObject and ILuaBase. ```cpp int CreatePlayerObject(ILuaBase *LUA) { // Create table ILuaObject *player = LUA->GetNewTable(); // Set properties player->SetMember("name", "PlayerName"); player->SetMember("health", 100.0f); player->SetMember("armor", 50.0f); // Create inventory table ILuaObject *inventory = LUA->GetNewTable(); inventory->SetMember(1.0f, "weapon"); inventory->SetMember(2.0f, "ammo"); player->SetMember("inventory", inventory); // Get member const char *name = player->GetMemberStr("name", "Unknown"); float health = player->GetMemberFloat("health", 0.0f); // Push to Lua stack player->Push(); LUA->SetField(GarrysMod::Lua::INDEX_GLOBAL, "CurrentPlayer"); // Cleanup LUA->DestroyObject(inventory); LUA->DestroyObject(player); return 0; } ``` -------------------------------- ### SetString Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluaobject.md Example of setting an ILuaObject to a string value. ```cpp obj->SetString("hello world"); ``` -------------------------------- ### Insert Method Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluabase.md Shows how to move the top stack value into a specified position. ```cpp LUA->PushNumber(42); LUA->Insert(1); // Move to position 1 ``` -------------------------------- ### AutoObject Constructor From Pointer Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example of wrapping an existing ILuaObject pointer with AutoObject. ```cpp ILuaObject *raw_obj = LUA->CreateObject(); AutoObject obj(raw_obj); // obj now manages the reference ``` -------------------------------- ### PushString Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluabase.md Demonstrates pushing string values onto the stack with and without specifying length. ```cpp LUA->PushString("hello"); LUA->PushString("data", 4); ``` -------------------------------- ### AutoHook Constructor Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example of initializing AutoHook to call a Garry's Mod hook. ```cpp { AutoHook hook(LUA, "Initialize"); LUA->PushNumber(123); if (hook.PCall()) { // Hook returned true (call original) } } ``` -------------------------------- ### SetFloat Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluaobject.md Example of setting an ILuaObject to a float value. ```cpp obj->SetFloat(3.14f); ``` -------------------------------- ### PCall Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluabase.md Demonstrates a protected call to a function, including error handling. ```cpp LUA->PushCFunction(myfunc); LUA->PushNumber(1); int result = LUA->PCall(1, 1, 0); if (result != 0) { Msg("Error: %s\n", LUA->GetString(-1)); } ``` -------------------------------- ### AutoCall Call Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example demonstrating the basic Call method of AutoCall. ```cpp call.Call(1); // Call, keep 1 result ``` -------------------------------- ### AutoObject operator= Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example of reassigning an AutoObject to wrap a different ILuaObject. ```cpp AutoObject obj1, obj2; obj2 = obj1; // Both wrap the same object ``` -------------------------------- ### AutoHook Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Demonstrates how to use AutoHook to safely call Lua hooks and manage their execution. ```cpp int PlayerInitialSpawn(int player_id) { ILuaBase *LUA = GetLuaInterface(); { AutoHook hook(LUA, "PlayerInitialSpawn"); LUA->PushNumber(player_id); // Call hook - returns true if original should run if (hook.PCall()) { // Original spawn code SpawnPlayer(player_id); } } return 0; } ``` -------------------------------- ### Number Usage Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/types.md Example usage for getting and pushing number values. ```cpp if (LUA->IsType(1, GarrysMod::Lua::Type::Number)) { double value = LUA->GetNumber(1); } LUA->PushNumber(3.14); ``` -------------------------------- ### PushCClosure Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluabase.md Shows how to push a C function with one upvalue (closure) onto the stack. ```cpp LUA->PushNumber(42); // upvalue LUA->PushCClosure(MyFunc, 1); ``` -------------------------------- ### AutoReference Constructor Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example of wrapping a Lua reference with AutoReference for automatic cleanup. ```cpp LUA->PushNumber(42); int ref = LUA->ReferenceCreate(); AutoReference auto_ref(LUA, ref); // Reference automatically freed on scope exit ``` -------------------------------- ### Bool Usage Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/types.md Example usage for getting and pushing boolean values. ```cpp if (LUA->IsType(1, GarrysMod::Lua::Type::Bool)) { bool value = LUA->GetBool(1); } LUA->PushBool(true); ``` -------------------------------- ### ModuleLoader Constructor Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/helpers.md Loads the specified module by name. Platform-specific extensions (.dll, .so, .dylib) are added automatically. ```cpp SourceSDK::ModuleLoader loader("engine"); if (loader.IsValid()) { // Module loaded successfully } ``` -------------------------------- ### GetMemberStr Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluaobject.md Example of getting a member as a string with a fallback default value. ```cpp const char *name = obj->GetMemberStr("name", "unknown"); ``` -------------------------------- ### AutoObject operator bool Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example showing how to use AutoObject in a boolean context. ```cpp if (obj) { // obj is valid } ``` -------------------------------- ### AutoHook Call Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example demonstrating the Call method of AutoHook to determine if the original code should execute. ```cpp if (hook.Call()) { // Original code should run DoOriginalBehavior(); } ``` -------------------------------- ### Symbol FromSignature Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/helpers.md Creates a symbol from a byte signature pattern. ```cpp // Pattern: 55 8B EC 8B ?? 5D C3 const char pattern[] = "\x55\x8B\xEC\x8B\x2A\x5D\xC3"; auto sym = Symbol::FromSignature(pattern, 0); ``` -------------------------------- ### AutoCall PCall Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example of using PCall for protected calls with error handling. ```cpp if (call.PCall(1)) { // Success - result at top of stack double result = LUA->GetNumber(-1); } else { // Error message at top Msg("Error: %s\n", LUA->GetString(-1)); } ``` -------------------------------- ### Entity Type Example Usage Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/types.md Shows how to check if a value is an Entity type and how to get the global 'Entity' table. ```cpp if (LUA->IsType(1, GarrysMod::Lua::Type::Entity)) { // Get entity reference from userdata } LUA->GetField(GarrysMod::Lua::INDEX_GLOBAL, "Entity"); ``` -------------------------------- ### Metatable with Methods Registration Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/macros-and-entry-points.md Example of registering methods for a metatable in a GMod module. ```cpp #include LUA_FUNCTION(Vector_ToString) { const Vector &v = LUA->GetVector(1); LUA->PushFormattedString("Vector(%f, %f, %f)", v.x, v.y, v.z); return 1; } GMOD_MODULE_OPEN() { int type = LUA->CreateMetaTable("MyVector"); LUA->PushCFunction(Vector_ToString); LUA->SetField(-2, "__tostring"); return 0; } ``` -------------------------------- ### GetTable Method Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluabase.md Illustrates pushing a table's value (table[key]) onto the stack. ```cpp LUA->PushString("key"); LUA->GetTable(-2); // table[key] now at top ``` -------------------------------- ### AutoStack Reset Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example of manually resetting the Lua stack using AutoStack's Reset method. ```cpp AutoStack stack(LUA); LUA->PushNumber(42); stack.Reset(); // Stack back to initial state LUA->PushString("new"); ``` -------------------------------- ### AutoReference Push Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example of pushing the referenced value onto the Lua stack using AutoReference. ```cpp auto_ref.Push(); double val = LUA->GetNumber(-1); ``` -------------------------------- ### SDK Components Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/configuration.md Includes for common SDK components. ```lua IncludeSDKCommon() IncludeSDKTier0() IncludeSDKTier1() IncludeSDKTier2() IncludeSDKTier3() IncludeSDKMathlib() IncludeSDKRaytrace() IncludeSteamAPI() ``` ```lua IncludeSDKCommon() IncludeSDKTier0() IncludeSDKTier1() IncludeSDKSteamAPI() ``` -------------------------------- ### Multiple Return Values Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/macros-and-entry-points.md Demonstrates how a C++ function can return multiple values to Lua. ```cpp #include LUA_FUNCTION(GetPair) { LUA->PushString("first"); LUA->PushString("second"); return 2; // Return 2 values } // In Lua: // local a, b = GetPair() ``` -------------------------------- ### Auto-Installation Method 3: Configuration File Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/configuration.md Configuration file setting for default auto-installation path. ```lua DEFAULT_GARRYSMOD_LUA_BIN_DIRECTORY = "C:/Program Files/Steam/steamapps/common/GarrysMod" ``` -------------------------------- ### Usage Example: Generating x86-64 Binaries Source: https://github.com/danielga/garrysmod_common/blob/master/readme.md To generate projects with the ability to compile x86-64 binaries, you need to change the project generator version. ```lua PROJECT_GENERATOR_VERSION = 3 ``` -------------------------------- ### FactoryLoader GetInterface Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/helpers.md Retrieves a typed interface from the module using the factory. ```cpp class IEngine { }; IEngine *engine = factory_loader.GetInterface("IEngine001"); ``` -------------------------------- ### AutoObject Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Illustrates the usage of AutoObject for managing Lua tables and their properties. ```cpp void CreatePlayerTable(ILuaBase *LUA, const char *name) { AutoObject player_obj(LUA->GetNewTable()); if (!player_obj.IsValid()) { return; } // Set properties player_obj->SetMember("name", name); player_obj->SetMember("health", 100.0f); player_obj->SetMember("armor", 0.0f); // Push to globals player_obj->Push(); LUA->SetField(GarrysMod::Lua::INDEX_GLOBAL, "CurrentPlayer"); // Object automatically unreferenced here } ``` -------------------------------- ### GetField Method Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluabase.md Shows how to push a table field (table[strName]) onto the stack without popping. ```cpp LUA->GetField(1, "myfield"); // Push arg1.myfield ``` -------------------------------- ### Pop Method Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluabase.md Illustrates removing a specified number of values from the top of the stack. ```cpp LUA->Pop(2); // Remove 2 values ``` -------------------------------- ### Simple Global Function Registration Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/macros-and-entry-points.md Example of registering a simple global function in a GMod module. ```cpp #include LUA_FUNCTION(PrintMessage) { const char *msg = LUA->CheckString(1); Msg("%s\n", msg); return 0; } GMOD_MODULE_OPEN() { LUA->PushCFunction(PrintMessage); LUA->SetField(GarrysMod::Lua::INDEX_GLOBAL, "PrintMessage"); return 0; } ``` -------------------------------- ### GetString Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluaobject.md Demonstrates retrieving a string value from an ILuaObject. ```cpp const char *str = obj->GetString(); if (str) { Msg("Value: %s\n", str); } ``` -------------------------------- ### AutoReference Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Shows how AutoReference can be used to manage Lua references, ensuring automatic cleanup. ```cpp class PlayerData { private: AutoReference player_ref; public: PlayerData(ILuaBase *LUA, const char *name) : player_ref(LUA, 0) { // Create player object LUA->CreateTable(); LUA->PushString(name); LUA->SetField(-2, "name"); // Store reference int ref = LUA->ReferenceCreate(); player_ref = AutoReference(LUA, ref); } void PrintName(ILuaBase *LUA) { player_ref.Push(); LUA->GetField(-1, "name"); Msg("Player: %s\n", LUA->GetString(-1)); } // Reference automatically freed in destructor }; ``` -------------------------------- ### Protected Call Pattern Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/macros-and-entry-points.md Illustrates how to safely call a Lua function from C++ using PCall. ```cpp #include LUA_FUNCTION(SafeCall) { LUA->GetField(GarrysMod::Lua::INDEX_GLOBAL, "UserFunction"); if (!LUA->IsType(-1, GarrysMod::Lua::Type::Function)) { LUA->ThrowError("UserFunction not found"); } LUA->PushNumber(42); if (LUA->PCall(1, 1, 0) != 0) { Msg("Error: %s\n", LUA->GetString(-1)); return 0; } return 1; } ``` -------------------------------- ### ModuleLoader IsValid Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/helpers.md Returns true if the module was successfully loaded. ```cpp if (!loader.IsValid()) { ThrowError("Failed to load module"); } ``` -------------------------------- ### Remove Method Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluabase.md Demonstrates removing a value at a specific stack position. ```cpp LUA->Remove(-1); // Remove top value ``` -------------------------------- ### LUA_FUNCTION_STATIC_MEMBER Macro Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/macros-and-entry-points.md Member function variant (automatic state setup). ```cpp static int FunctionName(lua_State* L) { GarrysMod::Lua::ILuaBase *LUA = L->luabase; LUA->SetState(L); return FunctionName__Imp(LUA); } static int FunctionName__Imp(GarrysMod::Lua::ILuaBase *LUA) ``` -------------------------------- ### Usage Example: CreateWorkspace function Source: https://github.com/danielga/garrysmod_common/blob/master/readme.md This function creates the workspace for your project, with options for including a debug compilation mode and specifying a path for project files. ```lua CreateWorkspace({ name = workspace_name, allow_debug = workspace_add_debug, -- optional path = workspace_path -- optional }) ``` -------------------------------- ### Using Stack Operations Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/README.md Example demonstrating basic stack operations for pushing and retrieving numbers, and returning a value. ```cpp LUA_FUNCTION(Add) { // ILuaBase methods available as LUA-> double a = LUA->CheckNumber(1); double b = LUA->CheckNumber(2); LUA->PushNumber(a + b); return 1; // 1 return value } ``` -------------------------------- ### Reference Lifecycle Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/README.md Shows how to manage references to Lua values, including creation, pushing, and freeing. ```cpp LUA->PushNumber(42); int ref = LUA->ReferenceCreate(); // Store reference // ... later ... LUA->ReferencePush(ref); // Restore value double val = LUA->GetNumber(-1); LUA->ReferenceFree(ref); // Clean up ``` -------------------------------- ### SetField Method Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluabase.md Demonstrates setting a table field (table[strName]) to the top stack value. ```cpp LUA->PushNumber(100); LUA->SetField(1, "health"); // arg1.health = 100 ``` -------------------------------- ### CreateProject Function Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/configuration.md Lua function to create a project for either server or client. ```lua CreateProject({ serverside = project_serverside, manual_files = project_manual_files, -- optional, default: false source_path = project_source_path -- optional }) ``` -------------------------------- ### Table of Functions Registration Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/macros-and-entry-points.md Example of registering multiple functions within a table in a GMod module. ```cpp #include LUA_FUNCTION(Greet) { LUA->PushString("Hello!"); return 1; } LUA_FUNCTION(Farewell) { LUA->PushString("Goodbye!"); return 1; } GMOD_MODULE_OPEN() { LUA->CreateTable(); LUA->PushCFunction(Greet); LUA->SetField(-2, "Greet"); LUA->PushCFunction(Farewell); LUA->SetField(-2, "Farewell"); LUA->SetField(GarrysMod::Lua::INDEX_GLOBAL, "MyLibrary"); return 0; } ``` -------------------------------- ### Usage Example: CreateProject function Source: https://github.com/danielga/garrysmod_common/blob/master/readme.md This function creates a project for the provided state, with options for serverside compilation, manual file inclusion, and specifying the source file path. ```lua CreateProject({ serverside = project_serverside, manual_files = project_manual_files, -- optional source_path = project_source_path -- optional }) ``` -------------------------------- ### AutoObject IsValid Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example of checking if an AutoObject is valid before using it. ```cpp if (obj.IsValid()) { obj->SetMember("name", "value"); } ``` -------------------------------- ### Creating a Structured Module Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/README.md Example of creating a more structured module using the GarrysMod::Module template. ```cpp #include class MyModule : public GarrysMod::Module { public: int Initialize(GarrysMod::Lua::ILuaBase *LUA) override { PushMemberFunction(LUA, &MyModule::DoThing); LUA->SetField(GarrysMod::Lua::INDEX_GLOBAL, "DoThing"); return 0; } int DoThing(GarrysMod::Lua::ILuaBase *LUA) { LUA->PushString("Done!"); return 1; } }; ``` -------------------------------- ### Usage Example: Generating x86-64 Binaries via Environment Variable Source: https://github.com/danielga/garrysmod_common/blob/master/readme.md Alternatively, you can set the project generator version using environment variables when calling premake. ```bash PROJECT_GENERATOR_VERSION=3 premake5 gmake ``` -------------------------------- ### Angle Type Example Usage Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/types.md Shows how to retrieve an Angle (QAngle) from the stack and push a new Angle onto the stack. ```cpp if (LUA->IsType(1, GarrysMod::Lua::Type::Angle)) { const QAngle &ang = LUA->GetAngle(1); } LUA->PushAngle(QAngle(0, 0, 0)); ``` -------------------------------- ### ErrorNoHalt Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluainterface.md Example of using ErrorNoHalt to print an error message without stopping execution. ```cpp LUA->ErrorNoHalt("Warning: %s\n", warning_msg); ``` -------------------------------- ### Setting Project Generator Version via Lua Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/configuration.md Demonstrates setting PROJECT_GENERATOR_VERSION to 3 and including garrysmod_common via Lua. ```lua PROJECT_GENERATOR_VERSION = 3 newoption({ trigger = "gmcommon", description = "Path to garrysmod_common", value = "path" }) local gmcommon = _OPTIONS.gmcommon or os.getenv("GARRYSMOD_COMMON") include(gmcommon) ``` -------------------------------- ### AutoReference GetReference Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example of retrieving the reference ID from an AutoReference object. ```cpp int ref_id = auto_ref.GetReference(); ``` -------------------------------- ### IncludeHelpersExtended Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/configuration.md Includes extended helper utilities from the garrysmod_common repository. ```lua IncludeHelpersExtended() -- Uses garrysmod_common path ``` -------------------------------- ### AutoCall Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example of calling a user function with error handling using AutoCall. ```cpp int CallUserFunction(ILuaBase *LUA) { AutoCall call(LUA, true); // Enable custom error function // Get the global function LUA->GetField(GarrysMod::Lua::INDEX_GLOBAL, "UserFunction"); // Push arguments LUA->PushNumber(10); LUA->PushString("data"); // Call with error handling if (!call.PCall(2, 1)) { // Error occurred - message is on stack Msg("Error: %s\n", LUA->GetString(-1)); return 0; } // Process result if (LUA->IsType(-1, GarrysMod::Lua::Type::Number)) { double result = LUA->GetNumber(-1); } return 0; // Stack cleaned automatically } ``` -------------------------------- ### AutoObject operator ILuaObject* Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example of implicit conversion of AutoObject to an ILuaObject pointer. ```cpp ILuaObject *ptr = obj; ``` -------------------------------- ### IncludeGOOMod Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/configuration.md Includes the GOOMod framework for structured modules. ```lua IncludeGOOMod() ``` -------------------------------- ### IncludeDetouring Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/configuration.md Includes the detouring library from a submodule. ```lua IncludeDetouring() ``` -------------------------------- ### AutoObject operator-> Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example of using the dereference operator to access members of the wrapped ILuaObject. ```cpp obj->SetMember("x", 10.0f); obj->SetMember("y", 20.0f); ``` -------------------------------- ### Setting Project Generator Version via Environment Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/configuration.md Shows how to set PROJECT_GENERATOR_VERSION to 3 using an environment variable before running Premake. ```bash PROJECT_GENERATOR_VERSION=3 premake5 gmake ``` -------------------------------- ### Create Workspace with Debug Mode Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/configuration.md Toggles debug mode via `CreateWorkspace`. ```lua CreateWorkspace({ name = "MyProject", allow_debug = true -- Includes Debug configuration }) ``` -------------------------------- ### IncludeLuaShared Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/configuration.md Includes LuaJIT shared library definitions. ```lua IncludeLuaShared() ``` -------------------------------- ### AutoCall Constructor Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/lua-helpers-extended.md Example of using AutoCall to wrap a Lua function call with error handling. ```cpp { AutoCall call(LUA, false); LUA->PushCFunction(my_function); LUA->PushNumber(arg1); call.PCall(1, 1); // Call with 1 arg, 1 result } ``` -------------------------------- ### IncludeScanning Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/configuration.md Includes the symbol scanning library from a submodule. ```lua IncludeScanning() ``` -------------------------------- ### PreCreateTable Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluainterface.md Prepares to create a table with preallocated space. ```cpp virtual void PreCreateTable( int arrelems, int nonarrelems ) ``` -------------------------------- ### Type Name Retrieval Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/types.md Shows how to get the string name of a Lua type using LUA->GetTypeName and how to get the type of an argument. ```cpp const char *type_name = LUA->GetTypeName(GarrysMod::Lua::Type::Number); // Returns "number" int actual_type = LUA->GetType(1); const char *arg_type = LUA->GetTypeName(actual_type); Msg("Argument 1 is: %s\n", arg_type); ``` -------------------------------- ### CheckNumberOpt Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluainterface.md Gets a number with a default fallback. ```cpp virtual double CheckNumberOpt( int iStackPos, double def ) ``` -------------------------------- ### Usage Example: Including SDK and Utility Modules Source: https://github.com/danielga/garrysmod_common/blob/master/readme.md These functions allow you to include various modules and SDK components required for your Garry's Mod compilation project. ```lua IncludeHelpersExtended() -- uses this repo path IncludeLuaShared() -- uses this repo path IncludeDetouring() -- uses this repo detouring submodule IncludeScanning() -- uses this repo scanning submodule IncludeSDKCommon() IncludeSDKTier0() IncludeSDKTier1() IncludeSDKTier2() IncludeSDKTier3() IncludeSDKMathlib() IncludeSDKRaytrace() IncludeSteamAPI() ``` -------------------------------- ### CheckStringOpt Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluainterface.md Gets a string with a default fallback. ```cpp virtual const char *CheckStringOpt( int iStackPos, const char *def ) ``` -------------------------------- ### GetPooledString Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluainterface.md Gets a pooled string by index. ```cpp virtual const char *GetPooledString( int index ) ``` -------------------------------- ### Stack Management Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/README.md Illustrates automatic stack management using the AutoStack RAII wrapper, ensuring cleanup on scope exit. ```cpp { AutoStack stack(LUA); LUA->PushNumber(1); LUA->PushString("test"); // Automatically cleaned on scope exit } ``` -------------------------------- ### GetDataString Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluainterface.md Gets a binary data string from the stack. ```cpp virtual size_t GetDataString( int index, const char **str ) ``` -------------------------------- ### GetActualTypeName Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluainterface.md Gets the actual type name for a type ID. ```cpp virtual const char *GetActualTypeName( int type ) ``` -------------------------------- ### SetMetaTable Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluaobject.md Example of setting the metatable for an object. ```cpp ILuaObject *meta = LUA->GetNewTable(); meta->SetMember("__add", add_function); obj->SetMetaTable(meta); ``` -------------------------------- ### UnReference Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluaobject.md Example of releasing a reference to an object. ```cpp obj->UnReference(); // obj is no longer valid ``` -------------------------------- ### GetStack Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluabase.md Gets stack information for debug purposes. ```cpp inline int GetStack( int level, lua_Debug *ar ) ``` -------------------------------- ### SetUserData Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluaobject.md Example of setting a userdata pointer for an object. ```cpp MyObject *ptr = new MyObject(); userdata_obj->SetUserData(ptr); ``` -------------------------------- ### SetFromStack Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluaobject.md Example of referencing a value from the Lua stack. ```cpp LUA->PushNumber(42); obj->SetFromStack(-1); LUA->Pop(1); // obj now references the number 42 ``` -------------------------------- ### SetMemberNil Example Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/api-reference/iluaobject.md Demonstrates setting a member to nil. ```cpp obj->SetMemberNil("removed"); ``` -------------------------------- ### Setting garrysmod_common Path with Premake Source: https://github.com/danielga/garrysmod_common/blob/master/_autodocs/configuration.md Ensures the 'garrysmod_common' path is correctly set when using premake. ```bash premake5 --gmcommon=/path/to/garrysmod_common gmake ```