Garry's Mod
Garry's Mod

Basic Scripting Concepts (Variables, Functions)

Learn Garry's Mod Lua scripting basics: variables for data storage and functions for performing actions. Understand data types and scope.

Basic Scripting Concepts (Variables, Functions)

Lua scripting is the engine behind Garry's Mod's vast customization. This section introduces fundamental scripting concepts: variables for storing data and functions for performing actions, laying the groundwork for creating your own addons.

Garry's Mod uses the Lua programming language for its scripting. To create custom game modes, weapons, entities, or any interactive element, you'll need to understand basic programming concepts. Variables are like containers that hold information, while functions are blocks of code that perform specific tasks. Mastering these two concepts is the first crucial step in your GMod scripting journey.

Variables: Storing Information

Variables are used to store data. In Lua, you declare a variable using the `local` keyword (for variables that are only accessible within the current scope) or simply by assing a value.

  • Data Types: Lua supports several data types, including:
    • Numbers: Integers and floating-point numbers (e.g., 10, 3.14).
    • Strings: Sequences of characters (e.g., "Hello, World!").
    • Booleans: Representing true or false values (true, false).
    • Tables: Lua's primary data structure, similar to arrays or dictionaries.

Example:

-- Declaring variables
local playerName = "Garry"
local playerScore = 100
local isAlive = true
local playerPosition = Vector( 100, 200, 50 ) -- A Vector is a special type of table

-- Printing variable values to the console
print( "Player Name: " .. playerName )
print( "Score: " .. playerScore )
print( "Is Alive: " .. tostring(isAlive) ) -- tostring() converts boolean to string

Functions: Performing Actions

Functions are named blocks of code that can be executed when called. They can accept input (arguments) and return output (return values).

  • Defining a Function: Use the `function` keyword.
  • Calling a Function: Use the function's name followed by parentheses, passing any required arguments.

Example:

-- Defining a function that adds two numbers
local function AddNumbers( num1, num2 )
 local sum = num1 + num2
 return sum -- Returns the calculated sum
end

-- Calling the function and storing the result
local result = AddNumbers( 5, 7 )
print( "The sum is: " .. result ) -- Output: The sum is: 12

-- Defining a function that prints a greeting
local function GreetPlayer( name )
 print( "Hello, " .. name .. "! Welcome!" )
end

-- Calling the greeting function
GreetPlayer( "Alice" ) -- Output: Hello, Alice! Welcome!

Key Concepts:

  • Scope: `local` variables are confined to the block of code where they are defined, preventing naming conflicts. Global variables (declared without `local`) are accessible everywhere but should be used sparingly.
  • Arguments: Values passed into a function when it's called.
  • Return Values: Values that a function sends back after it has finished executing.

Putting it Together:

Imagine you want to create a simple script that gives a player a health boost. You could use a variable to store the amount of health to add and a function to apply it.

local healthBoostAmount = 50

local function ApplyHealthBoost( ply )
 if IsValid( ply ) then -- Check if You entity is valid
 ply:SetHealth( ply:Health() + healthBoostAmount )
 ply:ChatPrint( "You received a health boost of " .. healthBoostAmount .. "!" )
 end
end

-- To use this, you would call ApplyHealthBoost( player_entity ) when needed.

Understanding variables and functions is the foundation for all Garry's Mod scripting. As you progress, you'll learn about tables, control structures (like `if` statements and loops), and how to interact with Garry's Mod's specific API.