Garry's Mod Wiki
Resources
If you haven't already set up your IDE for creating binary modules, refer to one of these pages:
- Creating Binary Modules: Premake (Recommended)
- Creating Binary Modules: CMake
- Setting Visual Studio Up for Making Binary Modules (Deprecated)
If you have never used C++ before, a tutorial is available to help get your footing
The Lua C API Documentation is an incredibly useful resource for understanding Lua's C API.
For additional useful functions, you can refer to LuaBase.h.
Navigation
This is a list containing all sections of this Page so you can navigate it easier.
- Understanding stacks
- Creating C++ functions for Lua
- Calling Lua Function In C++
- Iterating over a Lua Table.
- Creating C++ functions for readability & convenience
- A note on userdata & metatables
Understanding stacks
Lua's C API is built upon stacks and understanding them is critical.
A stack is a sort of array that can have elements "pushed" to it and "popped" from it.
Pushing an element puts it on top of the stack, while popping the stack removes the topmost element.
It is also possible to use the Remove
and Insert
functions to remove and insert values at certain positions in the stack, circumventing the restrictive nature of stacks, but you should use these sparingly.
You will often see negative numbers when fetching stack positions. 1
would refer to the bottom of the stack, or the first element, while -1
would refer to the top of the stack, or the last/latest element. -1
is often used to fetch the value that was just pushed to the top of the stack. -2
would fetch the secondmost top, or 2nd to last, value.
It is important to note that certain functions in Lua's C API interact with the stack in potentially unexpected ways, and if you find yourself running into errors and losing track of the stack, you should look up the functions you utilized.
One potent example is the Call
function. Calling a function from the C API pops the function and every argument from the stack.
The following snippet of code calls math.floor(5.6)
and stores its return value in a double MyDouble
while commenting a visualization of the current stack in a familiar array format:
Creating C++ functions for Lua
Making C++ functions for Lua is absolutely essential for your binaries. Without it, your modules just won't be as useful. In this section I will cover how to create functions in C++ for use in Lua, checking the arguments and return values.
Defining Our Function in C++
The first thing we will do is define our C++ function.
Now that we have a blank function, lets have it return a value. To do this we push something onto the stack and then return the number of values we want to return. So in this case we'll push a bool, true, and then return 1.
Now if we were to call this function in Lua, which we can't yet because we have only defined it in C++, then it would always return true.
Alright, so now that you have a basic understanding of how it works, let's make it more complex. How about we add a parameter and if it's over a certain value we return true, otherwise false.
Defining Our C++ Function in Lua
So now that we have or function created, we need a way to call it in Lua. This is the easy part.
This sets the Lua variable "MyFirstFunction" to the C++ function "MyFirstFunction", so you can call it in Lua using
Calling Lua Function In C++
It is very useful if you know how to call Lua functions using Lua C. In this section I will cover how to calling functions and getting the return values. Now a brief explanation of how Call works. If I did the following:
Then it would call my function with 0 arguments and not get a return value. The next example would call with 1 argument and still get no return value:
My next example would pass 3 arguments and get 1 return value:
Finally my last example would pass 2 arguments and get 4 return values:
Call and PCall
The only difference between Call and PCall is PCall is short for protected call. In other words if something goes horribly wrong, PCall won't freak out but call will. So it's a good habit to use PCall if your arguments aren't constants, but for these examples we will be using Call.
Calling from the Global Table
So calling from the global table is probably what you will use the most. It's very simple to call from the global table and easy. It just doesn't look very pretty.
This would output "Swag" in the console. Below is an example of using multiple arguments.
This would output "Swag 1337" in console. Below is an example of a pseudo-practical use.
If you wanted to call more than one function you would do the following:
Calling Functions Passed as Arguments
Calling functions passed as arguments is very useful. In another section I will cover how to store a function and then call it later, but for now we will start with the basics.
So now if you did something like below in Lua, it would output in console "Hey... swag swag swag swag"
Getting the Return Value
So you know how to call the functions, you just don't know how to get the return value. I will fix that for you. Below is an example of getting the return value of math.abs:
Now we have the return value of math.abs( -666 ) in iReturnValue. So iReturnValue is equal to 666. In the following example I get the result of math.abs( 1337 ) then I print it to my console.
This would output "666" to my console.
Iterating over a Lua Table.
At some point, you would want to iterate over a table passed from LUA. One of the ways you could do it is displayed below.
LUA->GetString
while the value is a table(could also be for other types) causes a memory leak!Creating C++ functions for readability & convenience
Internally, LUA_FUNCTION()
is a macro that exposes the LUA
variable and prepares the encased function for use with Lua.
We can pass this LUA
function to our own functions to create functions that exist solely in the C environment.
A function can be defined as follows:
...and then called from inside a LUA_FUNCTION
or even GMOD_MODULE_OPEN
:
For efficiency, it is important to understand when to use inline in functions.
A note on userdata & metatables
Userdata and metatables are handled differently in Garry's Mod. This helps the engine determine userdata type much faster.
First create your metatable (ideally in GMOD_MODULE_OPEN), then create a reference to it and store it globally in a variable.
To push your userdata to the stack:
To get your userdata from the stack:
This site is a community mirror of the official Garry's Mod wiki.. This site is not maintained by Facepunch Studios.
Page content is automatically updated twice a day. Edits and history are not available.
Last Parsed: Loading...