Common Lua Functions for Developers in Garry's Mod
Lua is the scripting language that powers Garry's Mod, enabling developers to create custom gamemodes, weapons, entities, and much more. Familiarity with common Lua functions and Garry's Mod's API is essential for any aspiring developer. These functions allow you to interact with the game world, manage players, and implement game logic.
Garry's Mod provides a rich API (Application Programming Interface) built on top of Lua, offering functions specifically desed for game development. These functions can be broadly categorized into those that interact with the game engine, entities, players, and the UI.
Core Lua Functions (Standard Lua):
- `print(...)`: Outputs messages to the console. Invaluable for debugging.
- `type(value)`: Returns the type of a variable (e.g., 'string', 'number', 'table', 'function').
- `math.random(min, max)`: Generates a random number within a specified range.
- `table.insert(table, value)`: Adds an element to a table.
- `table.remove(table, index)`: Removes an element from a table.
Garry's Mod API - Key Functions and Concepts:
- Entity Functions:
- `ents.Create(className)`: Creates a new entity of a specified class (e.g., 'prop_physics', 'npc_citizen').
- `Entity:SetPos(Vector)`: Sets the position of an entity.
- `Entity:GetPos()`: Returns the current position of an entity as a `Vector` object.
- `Entity:SetModel(modelPath)`: Changes the model of an entity.
- `Entity:IsValid()`: Checks if an entity reference is still valid (i.e., the entity exists).
- `Entity:Remove()`: Removes an entity from the game.
- Player Functions:
- `Player:Nick()`: Gets the player's nickname.
- `Player:Health()`: Gets or sets the player's health.
- `Player:SetTeam(teamID)`: Asss You to a specific team.
- `Player:Give(weaponName)`: Gives You a specified weapon.
- Vector and Angle Operations:
- `Vector(x, y, z)`: Creates a 3D vector.
- `Angle(pitch, yaw, roll)`: Creates an angle object.
- `Vector:Normalize()`: Returns a unit vector in the same direction.
- Hooks:
- `hook.Add(hookName, identifier, function)`: Allows you to run your code in response to specific game events (e.g., `PlayerInitialSpawn`, `EntityTakeDamage`, `Think`). This is fundamental for creating dynamic gameplay.
- Console Commands:
- `concommand.Add(commandName, callbackFunction)`: Registers a new console command that players or the server can execute.
- Derma (UI):
- `vgui.Create(widgetName)`: Creates a Derma GUI element (e.g., 'DFrame', 'DButton').
Example: Spawning a Prop on Player Spawn
hook.Add('PlayerInitialSpawn', 'SpawnMyProp', function(ply)
-- Create a physics prop
local prop = ents.Create('prop_physics')
prop:SetModel('models/props_junk/watermelon01.mdl') -- Example model
prop:SetPos(ply:GetPos() + Vector(0, 0, 50)) -- Spawn it slightly above the player
prop:Spawn()
prop:Activate()
end)
This hook will run every time a player spawns, creating a watermelon prop above their head. Mastering these functions and concepts is the first step towards becoming a proficient Garry's Mod developer.