- Introduction
- Code style
- General principles
- Script security
- Error handling
- Performance considerations
We appreciate your interest in contributing to the development and improvement of the Default Lua resources that come with the Multi Theft Auto (MTA) multiplayer mod. To ensure high-quality code and a smooth collaboration process, please adhere to the following coding guidelines.
- Write clear, readable, and maintainable code.
- Follow the DRY (Don't Repeat Yourself) principle.
- Adhere to the KISS (Keep It Simple, Stupid) principle.
- Use meaningful variable and function names that convey their purpose.
- Comment your code where necessary to explain the logic.
To improve code readability, prefer using early returns to handle error conditions or special cases at the beginning of functions. This helps to avoid deep nesting and makes the main logic easier to follow.
-- Bad example
function exampleFunction(value)
if value > 0 then
-- Some logic here
if value < 100 then
-- More logic here
if value ~= 50 then
-- Main logic here
end
end
end
end
-- Good example
function exampleFunction(value)
if value \<= 0 then return end
if value \>= 100 then return end
if value == 50 then return end
-- Main logic here end
end
Use the Lua error (message [, level])
function to raise warnings when validating parameters passed to a function, with level
set to 2
so that the error points the error at the function call location. For example:
-- This function outputs player's name to the chat box
function outputPlayerName(player)
-- If player is invalid, raise an error
if not (isElement(player) and getElementType(player) == "player") then
error("Invalid player!", 2)
end
outputChatBox(getPlayerName(player))
end
-- This function is triggered each time a player joins and calls outputPlayerName
local function playerJoin()
outputPlayerName() -- note the missing player argument; the error will point to this line
end
addEventHandler("onPlayerJoin", root, playerJoin)
All function names and variables should use the camel case naming convention. Constant variables should use upper snake case (see below).
Use of constants and avoiding magic numbers
Don't use magic numbers in blocks of code - instead, define such values as "constants" at the top of the file using upper snake case.
Note: the version of Lua used by MTA does not support the const
keyword added in Lua 5.4. In MTA Lua, the concept of a const
is just that - a concept.
Ensure your code editor (e.g. Visual Studio Code applies the rules established by the project's .editorconfig file.
- Avoid unnecessary computations within loops.
- Cache results of expensive operations whenever possible.
- Use local variables to improve performance.
Enabling the OOP functionality in any given resource will incur a performance hit. In general, pull requests to the official resources repository that enable it will generally fail code review.
Use range-based for loops rather than ipairs
Range-based for loops are more performant than loops using ipairs
and should be used whenever possible. For example:
-- Rather than this:
for _, player in ipairs(getElementsByType("player")) do
iprint(player)
end
-- Do this:
local players = getElementsByType("player")
for i = 1, #players do
iprint(players[player])
end
Follow the Script security principles established for MTA:SA scripts to ensure the safety and integrity of your code.