Skip to content

A mostly reasonable style & convention guide for FiveM's Lua ScRT

License

Notifications You must be signed in to change notification settings

project-error/fivem-lua-style

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Material-UI logo

FiveM Style Guide for Lua

A mostly reasonable style & convention guide for FiveM's Lua ScRT

license Discord

Intro

Unlike a language like JavaScript, Lua does not have a widely accepted styling across implementations. This style guide aims to ensure that styling is consistent across all Project Error Lua files.

Much of this style guide is based off popular JS/TS conventions.

Identation

I.1 - Always use spaces as target indentation

Primarily for code readability on GitHub as tabs are displayed as 8 spaces by default

I.2 - Use two spaces for indent spacing

Opionated carryover from JS/TS conventions.

Events & Exports

E.1 - Non-net cross resource communication should use exports

Why? Although exports are simple wrappers around events, they offer caching.

E.2 - NetEventHandlers on the Server should use RegisterServerEvent

Under the hood this is an alias to RegisterNetEvent. RegisterServerEvent is preferred for easy identification of Server Side handlers. (i.e Searching through a resource)

E.3 - Always use the optional callback argument for RegisterNetEvent

Increased readability and more concise

-- Good
RegisterNetEvent('netEvent', handlerFunc)

-- Bad
RegisterNetEvent('netEvent')
AddEventHandler('netEvent', handlerFunc)

Casing

C.1 - Constants should use upper case snakecase

-- Good
local INTERVAL_TIME = 50

-- Bad
local intervalTime = 50

C.2 - Local variables or functions should use camelCase

local myVar = {}

local function myLocalFunction()  end

C.3 - Global variables or functions should use upper camelcase

MyGlobalVar = 'nice'

function MyGlobalFunction()  end

Misc

M.1 - Where possible, avoid using the Citizen prefix for the Citizen table of functions

The most commonly used citizen table functions are aliased to work with removing the prefix. This appears more concise.

-- Bad
Citizen.CreateThread(fn)

-- Good
CreateThread(fn)

List of aliased functions:

SetTimeout
Wait
CreateThread
RconPrint -- Citizen.Trace

About

A mostly reasonable style & convention guide for FiveM's Lua ScRT

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published