About the Tutorial

From GMod Wiki

Revision as of 18:54, 23 November 2011 by Kurt (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Lua: Lua in Garry's Mod
Page white text.png Description:This is an introduction to the lua language in general and is a recommended read if you are a beginner.
link=User:theJ89 Original Author:theJ89
Group.png Contributors:The community
Calendar.png Created:May 9, 2008


Introduction

A lua file is called a script, it's plain text. To create and edit these scripts you need a plain text editor such as notepad.

You can use any text editor, but to make your life easier, we recommend that you use Notepad++.

For the sake of simplicity we'll refer to both as "notepad".

Warning: Do not use Wordpad. It is a formatted text editor, not a plain text editor. It will put useless tags in around your code that will cause it to fail when loading. As a rule of thumb, if you can have text that uses two different fonts in one file, then it is not a plain text editor.


Creating the script

For our first script, we're not going to do anything too complex. We'll learn how to send a message to the console.

Type the following code into your chosen editor :

print("Hello world");

You're done! Wasn't that easy? It should have been.

Saving the script

You're now ready to create the actual Lua script file. To find your lua folder follow the following path - this may be slightly different on your computer, but will look something like this:

C:\Program Files\Steam\steamapps\<steamname>\garrysmod\garrysmod\lua\

In the filename box, type helloworld.lua (Note that you must specify .lua), and in the Save As type box, select All Files. Now, just press enter (or press the save button) to save your script.

Running the script

To run any of your scripts you need to be playing a map. Now, open the console and type the following :

lua_openscript helloworld.lua

Press enter. If you did everything correctly up to this point you should see a message in the console :

Hello World!

The script has done exactly what you told it to.

This is just the bare basics. We'll move onto more sophisticated things in the following pages.

Yarin Kaul Icon ArrowSquare32.png Back to Lua Tutorial Series

Variables Yarin Kaul Icon ArrowSquare32 right.png


Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox