C Lua: Functions
If you haven't already set up your IDE, it is recommended that you read this first: Setting Visual Studio Up for Making Binary Modules.
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.
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.
Now that we have our function defined, it's
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
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:
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.
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 functions you would do the following:
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"
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.