Getting Involved Getting Involved |
Discord Discord |
Knight Challenges Knight Challenges |
Image edit request and support center Image Requests |
Guidelines:Main Guidelines |
---|
The editors are working to update all pertinent information as soon as possible. We apologize for any incomplete or missing information.
The following exercises are designed to help editors learn to make contributions in the Module namespace. The only prerequisite is an understanding of how templates are used.
The exercises have assigned readings from Programming in Lua. The readings explain the concepts covered in the exercise. Read them once before starting the exercise and refer back to them if you get stuck.
If you have any questions or feedback about these exercises, please bring them to the #modules
channel on Discord.
Getting Started[]
Readings[]
Setup[]
- Create the page User:35.170.81.33/Sandbox/Exercises with the following contents:
{{Sandbox/35.170.81.33/Exercises|Hello world!}}
- Create the page Template:Sandbox/35.170.81.33/Exercises with the following contents:
{{#invoke:Sandbox/35.170.81.33|Main}}
- Create the page Module:Sandbox/35.170.81.33 with the following contents, but don't save the page yet. (If you did accidentally, just go back to edit mode afterward.)
local p = {} function p.Main(frame) return p.main(frame:getParent().args) end function p.main(args) local msg = args[1] return msg end return p
- In the Preview page with this form beneath the main text area, input
User:35.170.81.33/Sandbox/Exercises
in the Page title field and click Show preview. This displays the page as it would appear if you were to save the module changes. You should see the following text on the previewed page:- Hello world!
- Scroll past the edit form to the black and green Debug console. In the text field, type the following, then press the Enter key. You should see the text
p.main({"Kooloo Limpah"})
Kooloo Limpah
appear in the console. - Save the module page.
Recap[]
This exercise covered:
- How articles use modules.
- Your sandbox page uses a template but has no notion of modules per se. The template uses the
#invoke
parser function to invoke the functionMain
on your module. The process is the same for real modules. The Link page uses Template:Term which invokes a function on Module:Term.
- How modules use template arguments.
- Your sandbox page passes the argument
Hello world
to the template. The module retrieves that argument from theframe
object. (More on that later.)
- How to make sure your module works before saving.
- The preview form and the debug console are the two main tools for debugging modules. Never submit code blindly.
Game Links 1[]
The goal of this exercise is to recreate the functionality of game link templates using Lua.
Readings[]
- Local Variables and Blocks (until third paragraph)
- Types and Values
- Strings
- Concatenation
- Logical Operators
- if then else
Setup[]
- Replace the content of your user sandbox with five invocations, each time with a different canon Zelda game of your choosing. Like so:
{{Sandbox/35.170.81.33/Exercises|Breath of the Wild}}
{{Sandbox/35.170.81.33/Exercises|The Wind Waker}}
{{Sandbox/35.170.81.33/Exercises|Majora's Mask}}
{{Sandbox/35.170.81.33/Exercises|Spirit Tracks}}
{{Sandbox/35.170.81.33/Exercises|Oracle of Ages}}
Breath of the Wild The Wind Waker Majora's Mask Spirit Tracks Oracle of Ages - In your module, put
mw.logObject(msg)
on line 9. Yourmain
function should look like this:function p.main(args) local msg = args[1] mw.logObject(msg) return msg end
Preview your user sandbox with these changes. Scroll past the edit box to the very bottom of the page until you see the section Parser profiling data (it may be collapsed by default). At the bottom of that table, uncollapse the the Lua logs section. You should see the following:
"Breath of the Wild" "The Wind Waker" "Majora's Mask" "Spirit Tracks" "Oracle of Ages"
mw.logObject
allows you to inspect the value assigned to a variable for a given invocation. In this case, each of the five times your module was invoked when loading the preview, the value assigned to the variablemsg
was printed in the logs. They are shown in quotes because they are values of typestring
. - Move the
mw.logObject(msg)
call to just before thereturn p
line, outside of themain
block, and preview the page again:function p.main(args) ... end mw.logObject(msg) return p
You should the following logs:
nil nil nil nil nil
nil
represents the absence of a value. Becausemsg
is declared as a local variable in themain
function, it is lexically scoped to that function and has no value outside it. In other words, only themain
function "knows" that the variable exists.Main
could declare its ownmsg
variable and it would be considered completely different. Likewise, the variablep
in our module is unrelated to thep
in any other module. - Leave the edit form without saving.
mw.logObject
is meant for debugging only and should not be left on a module.
Problems[]
- Using concatenation, change the return value to a string like
"* game"
so that your user sandbox looks like so:- Breath of the Wild
- The Wind Waker
- Majora's Mask
- Spirit Tracks
- Oracle of Ages
- Change the return value to a string like
* ''game''
, so that your user sandbox looks like so:- Breath of the Wild
- The Wind Waker
- Majora's Mask
- Spirit Tracks
- Oracle of Ages
- Change the return value to a string like
* [[The Legend of Zelda: game|''game'']]
. The output should be like: - Add
The Adventure of Link
(if you haven't already) or some other game whose full title doesn't start withThe Legend of Zelda:
. Use anif
statement to ensure the proper link is returned for that game: - Bonus: Rewrite the function to use
string.format
instead of concatenation.
Recap[]
This exercise covered:
- Variables and lexical scope.
- Local variables only have meaning in the block (function, if statement, for loop, etc.) or chunk (module) in which they are declared. Attempting to use one outside its scope produces
nil
. This is a good thing, as it prevents other parts of the code from changing data unintentionally. - Global variables are almost always a bad idea.
- Data types.
- The data types relevant to Scribunto are
nil
,boolean
,string
,number
,table
, andfunction
. At a given point in time, a variable will have any one of these types. A variable's type can change when it it is re-assigned—Lua is a dynamically typed language like JavaScript or Python, as opposed to a statically typed one like C or Java.
- String manipulation
- Most modules boil down to being string manipulators. They take as input some string(s) and output some other string (usually wikitext).
- Control flow
- This exercise involved using
if
statements with the relational operator==
. There are other relational operators such as~=
and<
, as well as the logical operatorsand
,or
, andnot
.
Solutions[]
There are many ways to arrive at solutions that work, but some are better than others in terms of programming style. When writing real modules, take the time to think about what you can do to make the code easier to understand for the next person who has to read it or change it.
Programs must be written for people to read, and only incidentally for machines to execute. | ||
— Gerald Jay Sussman and Hal Abelson, Structure and Interpretation of Computer Programs |
|