My First Lua Script
From GMod Wiki
Lua: My First Garry's Mod 10 Lua Script |
Description: | Getting started with Lua scripting in Garry's Mod 10. |
Original Author: | mrflippy |
Created: | December 14, 2006 |
Contents |
What You Will Learn
This article is intended to help people get started with Lua scripting in Garry's Mod 10. You will learn how to:
- Run a Lua script file when a new game is started.
- Set up a function in a Lua script.
- Attach a Lua function to a console command.
- Write a message to the console.
These are all very basic first steps, but are good starting points for breaking into Garry's Mod 10 Lua scripting.
Tutorial
Let us begin with your first Lua script!
Create the Lua Script File
The first step is to create the actual Lua script file. Open up your favorite text or Lua script editor (notepad will work fine for this). Go ahead and save this file to the following path:
<Garry's Mod Install location>\garrysmod\lua\autorun\client\my_test.lua
This may be slightly different on your computer, but will look something like this:
C:\Program Files\Steam\steamapps\<steamname>\garrysmod\garrysmod\lua\autorun\client\my_test.lua
Notes
- <steamname> is your steam username
- <Garry's Mod Install location> is the Steam directory for Garry's Mod. The default installation location for this is usually "C:\Program Files\Steam\steamapps\<steamname>\garrysmod" but may be different on your computer.
Notice the last part of the path: "autorun\client". Any script files placed in this directory will be run when Lua is started on the client. (Like when you start a new game or join a server) For more information, read about the Lua Folder Structure. Poking around in your Garry's Mod directory can also be very informative!
Write a Lua Function
Now that we have our file created, let's add some Lua script to it. We'll start with a simple "Hello World" type script, but you can test any Lua script using this same method.
Type this into your file:
function myTestFunction() local myTable = {"Entry 1", "Another Entry", "Garry's Mod is pretty awesome!"} print("Contents of myTable:") for key, value in ipairs(myTable) do print("Table entry #"..key..": "..value) end end concommand.Add("my_test_function", myTestFunction)
That may be a little confusing all at once! This script prints out the contents of a table (what arrays or lists are called in Lua) and hooks the function up to a console command. Let's look at the code in more detail.
Script Explanation
function myTestFunction() ... end
This tells Lua that you want to define a function. This is a simple function that takes no parameters and returns no value. Be sure to put an "end" at the end of your functions! Functions are modular pieces of code that you can use multiple times. See Functions for more information.
local myTable = {"Entry 1", "Another Entry", "Garry's Mod is pretty awesome!"}
This sets up a table with some initial values. Tables in Lua are just lists of things. We have set up a list of strings in this example, but you can stick other Lua objects into tables as well. Lua tables are associative (or hashed), meaning that each item in the list can be referenced by a key. Our table values have automatically been assigned numeric keys. See Tables for more information.
print("Contents of myTable:")
This simply prints a string to the Garry's Mod console.
for key, value in ipairs(myTable) do print("Table entry #"..key..": "..value) end
These commands print out all of the items in our table. This structure is called a loop. Notice the "pairs(myTable)" command. The pairs command is run once for each item in the table and returns key/value pairs for the items in the table. The value is the item in our table, while the key is just a way to reference the item. See Loops for more information.
print("Table entry #"..key..": "..value)
This print command is more complicated than the first one. This shows how to print out both text values and variable values with the same print command. The "key" and "value" variables are set up in the loop above. Notice the ".." between our strings and variables. ".." is the Lua concatenation operator. This tells Lua to glue the two items together and treat them as one text string.
concommand.Add("my_test_function", myTestFunction)
This sets up a console command that runs the function we just created. The first parameter to this command is the name of the console command. (In this case: "my_test_function") This is what you type into the Garry's Mod console to run the script. The second parameter is the function to run. (In this case: myTestFunction)
Save
Don't forget to save your file!
Test the Script
Now it's time to test the script!
Run Garry's Mod and start up a new game. Once the game is started, bring up the Console using tilde (~). If the developer console does not come up when you press tilde, you may need to enable it.
Type "my_test_function" into the entry line at the bottom of the console. (If all goes well, you should see auto-complete pop up with your command name)
Hit enter to run the command. You should see the following output in the console:
] my_test_function Contents of myTable: Table entry #1: Entry 1 Table entry #2: Another Entry Table entry #3: Garry's Mod is pretty awesome!
That's it! We're finished! Now you have a way to test your scripts inside of Garry's Mod.