Functions in Lua are like blocks of code that perform a specific task. They allow you to encapsulate pieces of code, allowing you to reuse them later, they also allow making your scripts more modular, which in most cases, is a good practice, they also make the code more readable and easier to maintain. Functions can accept input parameters, perform operations, and return results
A function has 5 main parts, the declaration (function statement), the function name, the parameters, the function body (code to execute) and the end statement
-- Function definition function functionName(params, ...) -- Function body end -- end statement, which marks the end of the function, or block
Functions can accept zero or more parameters, which are values passed to the function when it's called. These parameters are accessible within the function's body and allow you to provide inputs to customize the function's behavior.
Example:
-- Function with parameters function greetPlayer(name) print("Hello, " .. name .. "!") end function gameStart() print("Game has started!") end -- Call the function with an argument greetPlayer("Alice") -- Prints: Hello, Alice! gameStart() -- Prints: Game has started!
If you call the greetPlayer function with no parameters, it will give you an error
If you call the gameStart function with a parameter, it will be unusable, but won't give any error
Functions in Lua can return one or more values using the return statement. These returned values allow functions to communicate results back to the caller.
Example:
-- Function with return value function addNumbers(x, y) return x + y end local sum = addNumbers(5, 3) print("Sum:", sum) -- Output: Sum: 8
You can return any type of value, functions, tables, variables, etc
Lua also supports anonymous functions, which are functions without a name. These functions can be assigned to variables or passed as arguments to other functions, in mini world, they are mostly used in the listener function
-- Anonymous function assigned to a variable local square = function(x) return x * x end -- Call the anonymous function local result = square(4) print("Square:", result) -- Output: Square: 16
Anonymous function as argument of another function (in this case the listener function):
These both do the same
function gameStart() print("Hello, world!") end ScriptSupportEvent:registerEvent([=[Game.Start]=],gameStart) ScriptSupportEvent:registerEvent([=[Game.Start]=],function() print("Hello, world!") end)
Both functions will do the same, anonymous functions can also take args
Example:
function clickBlock(params) local playerid = params.eventobjid local blockid = params.blockid print("A player has clicked a block with the id: ".. blockid) print("The uid of the player that clicked the block is: ".. playerid) end ScriptSupportEvent:registerEvent([=[Player.ClickBlock]=], clickBlock) ScriptSupportEvent:registerEvent([=[Player.ClickBlock]=],function(params) local playerid = params.eventobjid local blockid = params.blockid print("A player has clicked a block with the id: ".. blockid) print("The uid of the player that clicked the block is: ".. playerid) end)
Both functions will do the same when a block is clicked