From StrategyWiki, the video game walkthrough and strategy guide wiki
Jump to navigation Jump to search

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.

References[edit]

Modules on SW[edit]

"Modules" are not pre-set libraries. They are pages that we have to create ourselves before being able to use any Lua code.

Basic syntax of calling a LUA function[edit]

This is only possible after a module has been created.

{{#invoke:Module_name|function_name|arg1|arg2|arg3 ...}}

Examples[edit]

Module:Sandbox[edit]

{{#invoke:sandbox|hello|, this module works!}}

Hello , this module works!

Module:Explode[edit]

Testing the explode function inside of Module:Explode
{{#invoke:Explode|explode|One,Two|,}}

Two

{{#invoke:Explode|explode|Category:Game images|:}}

Game images

{{#invoke:Explode|explode|Test category: the second adventure images| images|left}}

Test category: the second adventure

Debug test
{{#invoke:Explode|explode|Test category: the second adventure images| images|left|debug}}

rindex = 35
rindexnegative = -35
capturedstring = Test category: the second adventure

{{#invoke:Explode|explode|Test category: sub-title images|:|left}}

Test category

Testing string.find function inside of Module:Explode
{{#invoke:Explode|find|One,Two|,}}

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

LUA basics and random, useful notes[edit]

  • -- 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[edit]

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