MUDLET_LUA
Lua is a scripting languages used in many popular games, and Mudlet has Lua integration built into
its core! This help file is intended to give a brief overview of some of the main concepts of Lua
including variables, control flow, functions, and tables. For more in-depth information please visit
some of the following websites:
Mudlet Scripting: https://wiki.mudlet.org/w/Manual:Scripting
Mudlet Advanced Lua: https://wiki.mudlet.org/w/Manual:Advanced_Lua
Mudlet Lua Functions: https://wiki.mudlet.org/w/Manual:Lua_Functions
Variables:
You are able to assign values to variables in Lua simply by using the = operator:
myVariable = 1
or
myVariable = my string
If statements:
An if statement checks the value of a variable, for example:
if myVariable == 1 then
-- this is a comment, we do something here
else
-- myVariable is NOT 1, do something else
end
The == operator tests for equality, other operators include >, <, >=, <= not, or, and, etc
You can use boolean logic in an if statement to chain together multiple tests, for example:
if myVariable == 0 or myVariable == 1 then
-- do something if myVariable is 0 or 1
end
Instead of using or, you can use and to perform the AND operation.
You can also use an elseif if you want to explicitly test another condition, for example:
if myVariable == 0 then
-- do something if myVariable is 0
elseif myVariable == 1 then
-- do something if myVariable is 1
else
-- do something if myVariable > 1
end
Loops:
There are several loop types available in Lua.
A for loop:
for i = 1, 10 do
-- do something 10 times
end
And you can look backwards by specifying a negative step increment:
for i = 10, 1, -1 do
-- count down from 10 to 1
end
A while loop:
while (myVariable == 1) do
-- do something while myVariable == 1
-- this will loop forever until myVariable becomes something other than 1, be careful!
end
Functions:
A function can be defined in Lua as such:
function myCoolFunction(a, b, c)
return a + b + c
end
You can call a function by its name:
myReturnValue = myCoolFunction(1, 2, 3) -- this adds all 3 numbers and returns the value
Lua can return multiple values from a function!
The local statement in this function ensures that the variables used are only valid inside that
function. You dont want to accidently overwrite some other scripts variable by making everything
global!
function doAllTheMath(a, b)
local sum = a + b
local dif = a - b
local prod = a * b
local div = a / b
return sum, dif, prod, div
end
Then you can call that function with multiple return variables:
mySum, myDif, myProd, myDiv = doAllTheMath(4, 2)
Remember, if youre doing a division be sure not to divide by 0!
Tables:
Tables in Lua are like arrays or dictionaries. They can be initialized empty as follows:
myTable = {}
You can then add values to a table:
myTable.name = Kymbahl or myTable[name] = Kymbahl -- These are equivalent
You can even nest a table within a table!
myTable.players = {}
myTable.players.Kymbahl = {}
myTable.players.Kymbahl.isACoolGod = true
Mudlet defines a function that prints out an entire table to assist in debugging:
display(myTable) will print out:
{
players = {
Kymbahl = {
isACoolGod = true
}
}
}
Errors:
The best way to debug your code is to not put bugs into it in the first place! Well okay, noone
is perfect... If and when your script has errors, Mudlet has an Errors panel that will show you
which script and which line the error occurred on. Open the Editor panel (Aliases/Triggers/Scripts/
etc) and click the red Errors button on the left.
Running Lua from the command line:
Mudlet includes an alias that will execute Lua code from the command line, this comes in handy
for debugging purposes and many other reasons your imagination can come up with.
Simple type lua <code into the command line (without the quotes), for example:
lua echo(Hello World)
Will print out Hello World to your display!
See also: MUDLET_TIPS
|