User:Notmyhandle/Lua notes

Lua (not LUA), which means moon in Portuguese, is a programming language. We are able to use it here at StrategyWiki because Extension:Scribunto is installed.

This page provides helpful documentation pertaining to the use of Lua on StrategyWiki.

Modules on SW
"Modules" are not pre-set libraries. They are pages that we have to create ourselves before being able to use any Lua code.
 * Module:Explode
 * Module:Sandbox

Basic syntax of calling a LUA function
This is only possible after a module has been created.

Module:Explode

 * Testing the explode function inside of Module&#58;Explode


 * Debug test


 * Testing string.find function inside of Module&#58;Explode

The above numbers are the returned starting and ending values of where the string matches.

LUA basics and random, useful notes

 * -- comments a line.
 * -- Multi-line comment "tags" --
 * Data type of all number variables are 64-bit doubles that can auto-store int values.
 * Single or double quotes are used to define strings.
 * Double brackets used to define multi-line strings.
 * Variable = nil will undefine the variable and process it through garbage collection.
 * Variable = {} will define a table; this is a constructor expression.
 * When defining the number of parameters allowable in a function (e.g. function test(parameter 1, parameter 2)), set it to simply function test(frame) and then define, inside a comment, what all of the possible parameters could be. Any unused parameters will fill up the function's table, but will not affect the outcome of the code.
 * To use one of the passed parameters, use frame.arg[1], frame.arg[2], etc. arg is the name of the array that unnamed parameters are sent to.
 * string.format can be used to print text. It has to be combined with the return output to actually display, but you can use it to take a variable and convert it into a displayable string. See Module:Explode for an example within explode.
 * string.sub(string, startingindex, endingindex) is used to grab a sub-string from within a string, perfect for exploding text. If the ending index is omitted, then it will grab all characters from the startingindex to the end of the string. Counting starts from the left with 1. If a negative value is used, it counts from the right starting at -1.

Example code
function p.find(frame) -- args: 1:string 2:delimiter character (divider character) 3:starting position (far left if not set) 4:set to 'true' if you want plain matching enabled. See http://lua-users.org/wiki/StringLibraryTutorial -- return string.find(frame.args[1],frame.args[2],frame.args[3],frame.args[4]) end