Garry's Mod
Garry's Mod

Introduction to Lua in GMod

Begin scripting in Garry's Mod with this Lua introduction. Learn syntax, control structures, and how to use GMod's API with hooks and entities.

Introduction to Lua in GMod

Lua is the scripting language that powers much of Garry's Mod's functionality, from simple entity behaviors to complex gamemodes. Understanding Lua basics is crucial for anyone wanting to create custom addons, modify existing ones, or even just understand how certain addons work. This guide will introduce you to the fundamental concepts of Lua scripting within the Garry's Mod environment.

Lua is a lightweight, embeddable scripting language known for its simplicity and speed. In Garry's Mod, Lua is used extensively for:

  • Client-side scripting: Affects what You sees and interacts with locally (e.g., HUD elements, custom effects).
  • Server-side scripting: Controls game logic, NPC behavior, player management, and more on the server.
  • Shared scripting: Code that runs on both the client and the server.

Basic Lua Syntax

Lua's syntax is relatively straightforward:

  • Variables: Declared using the `local` keyword for local scope or globally if `local` is omitted (though local is preferred). Example: local playerName = "Garry"
  • Data Types: Common types include numbers (integers and floats), strings, booleans (`true`, `false`), tables (similar to arrays/dictionaries), and functions.
  • Operators: Arithmetic (`+`, `-`, `*`, `/`), comparison (`==`, `~=`, `<`, `>`, `<=`, `>=`), and logical (`and`, `or`, `not`).
  • Comments: Use `--` for single-line comments and `--[[ ... ]]` for multi-line comments.

Control Structures

These allow you to control the flow of your script:

  • If Statements: if condition then ... elseif other_condition then ... else ... end
  • Loops:
    • for i = 1, 10 do ... end (numeric loop)
    • for key, value in pairs(myTable) do ... end (iterator loop)
    • while condition do ... end (while loop)

Functions

Reusable blocks of code:

  • function MyFunction(arg1, arg2) ... return result end

Tables

Lua's primary data structure, used for arrays, lists, and dictionaries:

  • local myTable = {}
  • myTable[1] = "Apple"
  • myTable.name = "Garry"
  • print(myTable[1]) -- Output: Apple
  • print(myTable.name) -- Output: Garry

Garry's Mod Specifics

Garry's Mod provides a rich API (Application Programming Interface) that exposes game functions to Lua scripts. Key concepts include:

  • Hooks: Functions that are called automatically when specific game events occur (e.g., `PlayerSpawn`, `EntityTakeDamage`). You register hooks using hook.Add().
  • Entities: The fundamental objects in the game world (players, props, NPCs). You can create, manipulate, and destroy entities using Lua functions.
  • Console Commands: You can execute console commands from Lua using RunConsoleCommand().

Example: A Simple Print Statement

To see Lua in action, open the developer console in Garry's Mod (usually with the `~` key) and type:

print("Hello, Garry's Mod!")

Press Enter, and you should see the message appear in the console.

This is just a starting point. As you delve deeper, you'll learn about object-oriented programming, networking, and more advanced GMod-specific functions.