Within the expansive Garry's Mod community, a standardized approach to writing Glua code has yet to be defined. Consequently, code contributions from various authors often ask consistency and aesthetic appeal. In response to this challenge, I have taken the initiative to establish the inaugural standard
I eagerly await your feedback and suggestions for further enhancements 👍
- Use 4 spaces for each level of indentation
- There is no difference between using double quotes
"
and single quotes'
- Spaces are placed around operators and after commas, but not inside parentheses and after function names
-- Good
x = 10
fruits = {"apple", "banana", "orange", "kiwi", "pineapple"}
-- Bad
x=10
fruits = {"apple","banana","orange","kiwi","pineapple"}
- Avoid extra spaces around parentheses, square brackets, and curly braces
-- Good
function foo(x)
return x * 2
end
local tabl = {key = "value"}
local myArray = {1, 2, 3, 4, 5}
-- Bad
function foo( x )
return x * 2
end
local tabl = { key = "value" }
local myArray = { 1, 2, 3, 4, 5 }
- If an invisible VGUI element is needed, do not create an empty paint function - assign it to
nil
-- Good
local pan = vgui.Create("DPanel")
pan.Paint = nil
-- Bad
local pan = vgui.Create("DPanel")
pan.Paint = function() end
- Use
_
for ignored variables.
for _, item in ipairs(items) do
foo(item)
end
- Functions and methods have uppercase syntax, object properties are always lowercase
- Arguments in functions are lowercase
- Prefer using
Is
when naming boolean functions
local function IsEven(x)
return x % 2 == 0
end
- Prefer function syntax over variable syntax
-- Good
local function eat()
...
end
-- Bad
local eat = function()
...
end
- Use dot notation to access known properties
local Person = {
age = 20
}
-- Good
Person.age
-- Bad
Person["age"]
false
andnil
are falsy in conditional expressions
-- Good
if name then
...
end
-- Bad
if name != nil then
...
end
- Put a space after
//
and--
- Avoid aligning variable declarations
-- Bad
local a = 1
local long_identifier = 2
- Use the name of the net requests, taking into account the script/addon to which they belong, and what role they perform
-- Examples
util.AddNetworkString("GangSystem-InvitePlayer")
util.AddNetworkString("HatShop-Buy")
- When sending a net request, the nested syntax of the writing data should be preserved
net.Start("Test")
net.WriteString("Hello World")
net.WriteTable({6, 8, 10, 12})
net.SendToServer()
- When writing a block/blocks of code, use the same style of variable names
- The name of the hook should indicate the addon/script to which the code and the meaning of the hook belong
hook.Add("PlayerSay", "AutoDonate.OpenMenu", function(pl, text)
if text == "/donate" then
pl:ConCommand("autodonate_menu")
end
end)
- During the declaration of color and material variables, their affiliation is indicated
local color_button = Color(75, 75, 75)
local mat_medal = Material("icon16/rosette.png")
- If there is an iteration in the code block, cache the variables
local color_text = Color(25, 25, 25)
hook.Add("HUDPaint", "CustomHUD.Test", function()
draw.SimpleText("Hello there", "TargetID", 25, 25, color_text)
end)
-- Somewhere during the creation of the interface
local pl_nick = LocalPlayer():Name()
pnl.Paint = function(_, w, h)
draw.RoundedBox(6, 0, 0, w, h, color_black)
draw.SimpleText(pl_nick, "Trebuchet24", w * 0.5, h * 0.5, color_white, 1, 1)
end
- GLua files should be named in lowercase
- Dedicate a separate folder for your addon within the
lua/
directory. Refer to it through a*_autorun.lua
file inlua/autorun/