Functions
From GMod Wiki
In Lua, you can make your own functions. Making functions can save you work and can be really useful!
As always, open notepad.
Now, here's what a simple, basic function looks like:
function myFunc() Msg("myFunc has been run!\n") end
Lets take that apart.
The word "function" tells lua that you want to make a function. All functions in lua start with the word function.
Next, myFunc is the name of the function. This is the name you'll use to call it.
Next to that there are a set of parentheses( an ( and an ) ). I'll tell you what these are for later. Just know that these need to follow the name like I show in the example.
Between the parentheses and the "end" word is the code the function runs. Notice that there's 4 spaces before it. These four spaces tell a programmer that the code on that line is part of that function. You don't have to do this, but I really recommend you do. It's very helpful, it helps keep your code organized.
Lastly there's an "end" word. This tells lua that that's the end of the function, and nothing past it is part of that function.
Now that we understand functions and know how to make them, I'm sure you're wondering how to run them. Running a function is called calling them.
So, to call a function...
function myFunc() Msg("myFunc has been run!\n") end myFunc()
Yeah! That's it. You just need to type the name of the function followed by a pair of parentheses. Does this look familiar? It's like Msg, but with nothing between the parentheses.
Next I'll show you how to make a function with arguments. Arguments are sort of like temporary variables. Arguments are placed between the parentheses that follow the name of the function. Whenever you call a function, you can pass data to it through an argument to change what it does.
This is what a function with arguments looks like:
function sayMyName(whatsMyName) Msg("Hey "..whatsMyName.."!\n") end sayMyName("Jeff") sayMyName("Josh") sayMyName("Alan") sayMyName("Andrew") sayMyName("Ryan")
So, as you can see, this function has an argument. The argument is called whatsMyName and it's right inbetween the two parentheses.
Inside of the function you see that I have whatsMyName being put between a couple of strings.
Below that you see I have sayMyName() with different names in it. Each time sayMyName is called, whatsMyName becomes equal to the name I gave the function when I called it.
So...
sayMyName("Jeff")
Does:
Msg("Hey Jeff!\n")
Do you get it? Hope so.
So, write a function, one with arguments, and one without arguments. Just write this and change it around if you want:
function myFunc() Msg("I ran myFunc\n") end function whoRanMyFunc(who) Msg(who.." ran myFunc\n") end myFunc() whoRanMyFunc("Jeff") whoRanMyFunc("Josh")
Save your script in your garrysmod/lua folder as functions.lua.
After that's done go into GMod and load it. You should see this in your console:
I ran myFunc
Jeff ran myFunc
Josh ran myFunc
There is also another version of the function that is slightly more useful, here is its formula:
function myFunc(input) local output --do stuff to input within your function-- return output --returns what you want it to back into the main program for usage end
This is more useful than just printing messages because now you can get actual values to use in other parts of your program.
Simple example, I want to convert Fahrenheit to Celsius and then check if it matches the temperature outside (which I'd put in Celsius) in two steps.
function tempConvert(input) --declaring our function and its arguments return (input - 32) * (5 / 9) --temperature conversion formula for F to C, returns it end local outerTempC = 0 --conditional figures out if it is warmer or colder than 0 degrees Celsius and responds accordingly if outerTempC <= tempConvert(32) then Msg("Nipples in danger!") else Msg("Your nipples are relatively safe.") end
Now, you may ask: "Why not just keep the temperature value locally within the function and even go so far as to execute the conditional within the function and return a Msg like before?"
Because then, this is just a one-purpose function. It isn't open-ended at all. If I keep the function open like so, I can reference it at any time to retrieve a value and even store it in a variable should I need it to become a constant.