Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "common/GMLuaModule.h" // Include the header for the required GMod Lua module definitions
- GMOD_MODULE( Open, Close ); // State the functions we want to be called when the module loads and unloads
- LUA_FUNCTION( PlayerInitialSpawnCallback ) // Declare our callback function that will be passed to hook.Add as the 3rd argument
- {
- Lua()->CheckType( 1, GLua::TYPE_ENTITY ); // Ensure the first argument is type "Entity" - we can just assume its a player
- int playerRef = Lua()->GetReference( 1 ); // Get a reference to the player for later use (using GetUserData results in garbage collection, causing errors elsewhere)
- if ( playerRef != -1 ) // Make sure our reference is valid
- {
- ILuaObject *playerMeta = Lua()->GetMetaTable( "Player", GLua::TYPE_ENTITY ); // Retrieve the "Player" metatable, equivalent to "playerMeta = _R.Player"
- if ( playerMeta ) // Make sure the result is valid
- {
- if ( playerMeta->isTable() ) // Check that our reference is a table
- {
- ILuaObject *playerChatPrint = playerMeta->GetMember( "ChatPrint" ); // Lookup _R.Player.ChatPrint
- if ( playerChatPrint ) // Does _R.Player.ChatPrint exist?
- {
- if ( playerChatPrint->isFunction() ) // Is _R.Player.ChatPrint a function?
- {
- // To call a meta function, we need to push it onto the stack using ILuaInterface::Push
- // The meta function must be pushed first, followed by the userdata, followed by its arguments
- Lua()->Push( playerChatPrint ); // Push the meta function
- Lua()->PushReference( playerRef ); // Push the player object (userdata)
- Lua()->Push( "You've just spawned!" ); // Push the argument
- // We've pushed 2 arguments and don't care about any return values
- Lua()->Call( 2, 0 );
- }
- playerChatPrint->UnReference(); // The function has been called, we can unreference
- }
- }
- playerMeta->UnReference(); // We're done, unreference
- }
- Lua()->FreeReference( playerRef ); // Free our player object reference, it isn't needed anymore
- }
- return 0; // Zero return values
- }
- int Open( lua_State *L ) // Declare the function we want to be called when the module loads
- {
- ILuaObject *hookTable = Lua()->GetGlobal( "hook" ); // Find the global "hook", equivalent of "hookTable = hook", or "hookTable = _G.hook" in Lua
- if ( hookTable ) // If hook wasn't found then GetGlobal returns NULL; we make sure it was successful before continuing
- {
- if ( hookTable->isTable() ) // Hook may have been found, but we should check that it's a table
- {
- ILuaObject *hookAdd = hookTable->GetMember( "Add" ); // Hook appears to be a valid table so we lookup its "Add" member ( "hookAdd = hook.Add" in Lua )
- if ( hookAdd ) // Was hook.Add successfully retrieved?
- {
- if ( hookAdd->isFunction() ) // Is hook.Add a function?
- {
- // To call a function, we need to push it onto the stack using ILuaInterface::Push
- // The function must be pushed first, followed by its arguments
- Lua()->Push( hookAdd ); // hookAdd is now the equivalent to the "hook.Add" function in Lua, so we push it onto the Lua stack
- Lua()->Push( "PlayerInitialSpawn" ); // hook.Add takes the hook type as its first argument
- Lua()->Push( "hookname" ); // The next argument is the unique hook name
- Lua()->Push( PlayerInitialSpawnCallback ); // Push our callback function which will be called when PlayerInitialSpawn is triggered
- // We've pushed 3 arguments and don't care about any return values
- Lua()->Call( 3, 0 );
- }
- hookAdd->UnReference(); // We've added our hook and no longer need access to the "hook.Add" function, unreference it
- }
- }
- hookTable->UnReference(); // The hook table isn't required any further, unreference it
- }
- return 0;
- }
- int Close( lua_State *L ) // Declare the function we want to be called when the module unloads
- {
- // You need to remove the hook here because the callback function will become invalid
- // Hopefully after reading through this, you'll be able to do it yourself
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement