Setting up our Work space and Basic language introduction

So like mentioned in the introduction, we’re going to use Visual Studio Code – It’s a great code editor available for most platforms, it has all the good jazz that you’d use for Garry’s Mod development, like syntax highlighting, code completion and even integrated git support (which we won’t cover)

It’s worth mentioning, that there are alternatives out there, like Atom, Notepad++ and Sublime Text – They’re all great, but VSCode is my personal preference for the feature of LiveShare (collaboration with multiple developers!)

Let’s start by downloading Visual Studio Basic

Head over to this link and download appropriate version for your operating system – Install it, preferably on your SSD drive, if you have one.

After you’ve installed it, all that’s there left to do is to launch it – Got it? Great! We’re now ready to install an Garry’s Mod Lua extension for Visual Studio, so we can get all good stuff such as code completion, snippets and syntax highlighting – You can get the extension here.

Simply click install and your browser should ask you if you want to open the link with Visual Studio Code – Accept that dialog – After the extension is installed, Garry’s Mod VSCode extension will be active automatically in any file that has the .Lua extension!

Once you’ve done that, you’re now almost ready to start developing add-ons for Garry’s Mod – But first, there’s one more thing to do.

Garry’s Mod Lua wiki – Also known as gmod’s developers best friend

Seriously. This will be your daily go-to documentation – It has (almost) every single function documented, and even some native Lua functions – Meaning you generally won’t have to even open any Lua docs!

You can view the wiki here.

Before you go digging in, let’s actually explain what we’re seeing on your screen right now


The sidebar displays all of the information that you might want to know – If you want to see what object types (classes) are available to you – Go to Classes, and either click on the Player label to open the contents of that category, or click on those blue arrows, that will open a separate page with all of the functions (methods) of that class – And some, usually short, description.

Player Class as seen on wiki

Right now, it won’t mean much to you – But mark my words, this will be your new home.

Server / Client / Shared Realms

While Lua doesn’t have Client / Server states by default, it is a thing in, well, almost every multiplayer game, as well as Garry’s Mod

A server, is the machine that runs the game server in which you (a client) and other players (also clients) join to – Server has the absolute authority, it makes all the decisions, like where player’s position is, and distributes that information to everyone else.

To put things in perspective, let me tell you how movement is usually done in an FPS game.
In a server-authoritative environment, it’s the server that receives inputs from the clients, and validates them, processes them, and then updates every other client with that information.

So how, for example, is player movement done in Garry’s Mod?
Well, when you first start moving in Garry’s Mod, that means that you’ve pressed a key – You tell the server that you’ve pressed your IN_FORWARD (walk forward) key – and server processes that information, moving you slightly forward, checking if it’s possible for you to move (hitting a wall? moving at 400KM/h? If so, disallow that – After they processed our new position, they report back to every single client (including you) and say – Hey! This client has moved! Update their position on the client’s screen. Clients cannot, ever, communicate directly between each other, there’s always the middleman, which is the server.

But… There’s a bit more to that
You might have gotten the impression from that example that client tells the server the action it’s doing, the server actually does it, and updates everybody on the result of that action – well, that’s only half true!

To mitigate lag in a multiplayer session, client that does the action, such as moving, actually does it on their own screen and simultaneously reports to the server that hopefully, matches up the action with what the client just did and updates every other client on it. This is called prediction – Your client sends action to the server, then tries to predict servers output instead of just waiting on it – and if the output doesn’t match up when the response from the server comes back, then you will be abruptly corrected – It’s harder to predict things when your ping is high – therefore the well known “lag” happens – where you jitter all over the place, or rubber band through the map.

You can read more on prediction in Garry’s Mod here.

Let’s proceed with why this is relevant to the wiki:

Function Realms

By looking at the above screenshot, you can see that each entry has a box beside it – and its colour isn’t consistent, so what’s up with that?

Well, that defines realms in which this function can be used.

  • Blue means serverside,
  • Orange means clientside,
  • Mixed means shared (both realms)

 If you call a server side function in a client realm, you will get a runtime error that states that this doesn’t exist and vice versa in client side function in a server realm. You can occasionally see a green box, which means that the function is menu-realm – In all of my years in the development scene of Garry’s Mod, I haven’t touched a single menu-realm function – So let’s ignore those.

Let’s get to development!

To start developing your first Garry’s Mod addon, open Garry’s Mod but don’t run your server just yet – Open your File Explorer and navigate to the path where you have Garry’s Mod installed.
Navigate to garrysmod/ folder within that folder (fun, right?) and go to /addons/ and go ahead, create a new folder, within that folder create a /lua folder, and within that lua folder, create another folder, called autorun – Huh, that’s a lot of folders, right?

The structure is as follows:

  • addons
      • lua
        • autorun

Now, go ahead and create a file within the autorun folder, call it something like sh_myscript.lua (addon/YOURFOLDERNAME/lua/autorun/sh_myscript.lua) – Open it with Visual Studio Code.

Wow – We’re finally here… We’re ready to start our development adventure, so let’s step into Lua basics!

First program, the (in)famous Hello World

Let’s begin with a classic, go into your code editor and type in this line of code, and save the file (CTRL+S):

print( "Hello World" )

If you haven’t already, start a game – Just make sure you run it in a multiplayer server (doesn’t matter if you didn’t port-forward) – When you launch the server, if you open your console (~ key, you might have to enable it in the options) you might see a “Hello World” printed out in your console – It should also appear in both blue, and orange colours, twice.

Note: Lua Code is ran each time you start a server, and each time you save (CTRL+S) a file due to Lua refresh feature of Garry’s Mod

What do the colors mean? Again, orange stands for client – the code that we written above executed in the client realm and the print is orange – Then (or before) it executed in server side, therefore we executed it in both realms, so in a shared realm.

Let’s break down the line bit by bit – let’s start with print() – print() is a function, that, internally processes any arguments that you have passed to it, and prints it out to the console. What argument have we passed to it? A string! By doing “text” we actually created a new String object, with the text property of “Hello World”, therefore, it printed “Hello World”

Let’s do something more advanced, how about a result of a mathematical equation?

print( 3360 / 8 )

Should print out 420 – Nice.

Variables – Basics

Many tutorials relate to variables as little boxes that store information for later use.

This is one of those tutorials. Variables are little pieces of memory (box), that we store information in it for later uses, the box is named by whatever you want, and you keep the reference (key with the name) for whenever you want to use it.

Let me show you how you declare (create a box with a name) and assign (put things in that box) values to those variables

x = "Hello"
y = 213
z = x
p = x .. z

So we create 4 variables, they all have their references (names) as x, y, z and p – in the first variable, x, we put a String, in the second one, y, we assign some numbers to it.

Note: Some variable names are invalid, you cannot define a variable with some special symbols in it, and you cannot have spaces in them – Use underscores instead. Additionally to that, there are some reserved names that you shouldn’t use:

Reserved key-words

Then we set the value of z to x – so z equals “Hello” now. It’s might be kind of confusing at first, but don’t worry about that – You’ll get used to it.

But what’s that last variable? It’s called p, but it does “x .. z” – Well, “..” in Lua is concatenation operator for strings (only strings) – It basically joins two strings together – So in the end, p value is “HelloHello”, because we join z to x, and they’re both the same.

Let’s use those variables in a function!

x = 20
y = 15
math.min( x, y )

Aaaand… Nothing happens. Why? Because math.min is a function that takes 1 or more arguments, and then returns the result – It returned it, but we’ve done nothing with it. So let’s do something with it, take the previous code and edit it to that:

x = 20
y = 15
z = math.min( x, y )

print( z )
print( math.min( x, y ) )

This will pick and return the lowest number out of those provided within the function – we can store any type of returns within a variable, or just straight up use it within some other function. No difference.

That’s it for today!

To summarize, we’ve set up our program, and started a new addon with the proper structure, we’ve learned a bit about the Garry’s Mod wiki, and what variables are and how to use them, and as well we have used some functions. Clever.

In the next blog we’ll cover how to define your own functions and if statements – Two very big topics that if you master, you’ll have a good footing to start doing your own things in Lua!