Dessamator (talk | contribs) Tags: Visual edit apiedit |
Siroopwafel (talk | contribs) mNo edit summary |
||
(5 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
+ | {{Infobox Lua templating}} |
||
Lua is implemented in MediaWiki wikis using the [[mw:Extension:Scribunto|Scribunto/Lua extension]] and stored in resource pages using the Module: namespace.<ref>http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual</ref> |
Lua is implemented in MediaWiki wikis using the [[mw:Extension:Scribunto|Scribunto/Lua extension]] and stored in resource pages using the Module: namespace.<ref>http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual</ref> |
||
__TOC__ |
__TOC__ |
||
− | == Create |
+ | == Create your first Lua script == |
To create your first Lua script: |
To create your first Lua script: |
||
− | # Navigate to |
+ | # Navigate to '''Module:Sandbox/Username''', where Username is your Fandom username. |
+ | # It's a sandbox. Everyone is free to play in their sandbox. |
||
− | # Clear all existing code. |
||
⚫ | |||
− | # It's a sandbox. Everyone is free to play in the sandbox. But if you find another user is actively editing the sandbox at the same time, you may also use Module:Sandbox/Username, where Username is your Wikia username. |
||
⚫ | |||
<blockquote><pre> |
<blockquote><pre> |
||
local p = {} |
local p = {} |
||
Line 17: | Line 17: | ||
</pre></blockquote> |
</pre></blockquote> |
||
− | == Test |
+ | == Test your first Lua script == |
To test your first Lua script: |
To test your first Lua script: |
||
− | # Navigate to either the [http://dev.wikia.com/wiki/ |
+ | # Navigate to either the [http://dev.wikia.com/wiki/Module_talk:Sandbox Module_talk:Sandbox] page, or your own user or sandbox page. |
# Add the following code and save the page: |
# Add the following code and save the page: |
||
<blockquote><pre> |
<blockquote><pre> |
||
Line 31: | Line 31: | ||
</blockquote> |
</blockquote> |
||
− | == Edit |
+ | == Edit your first Lua script == |
To edit your first Lua script: |
To edit your first Lua script: |
||
# Return to the [[Module:Sandbox]]. |
# Return to the [[Module:Sandbox]]. |
||
Line 45: | Line 45: | ||
</blockquote> |
</blockquote> |
||
− | == Understand |
+ | == Understand your first Lua script == |
Now that you see what the script does, it's time to understand how it works. |
Now that you see what the script does, it's time to understand how it works. |
||
# <code>local p = {}</code> creates a local table or array for your code and names it <code>p</code>. |
# <code>local p = {}</code> creates a local table or array for your code and names it <code>p</code>. |
Revision as of 01:36, 2 April 2020
Lua is implemented in MediaWiki wikis using the Scribunto/Lua extension and stored in resource pages using the Module: namespace.[1]
Create your first Lua script
To create your first Lua script:
- Navigate to Module:Sandbox/Username, where Username is your Fandom username.
- It's a sandbox. Everyone is free to play in their sandbox.
- Clear all existing code. Add the following code and save the page:
local p = {} function p.hello() return 'Hello!' end return p
Test your first Lua script
To test your first Lua script:
- Navigate to either the Module_talk:Sandbox page, or your own user or sandbox page.
- Add the following code and save the page:
{{#invoke:Sandbox|hello}}
The result should be:
Hello!
Edit your first Lua script
To edit your first Lua script:
- Return to the Module:Sandbox.
- Edit the line with
return 'Hello!'
and add your name inside the single quotes. You should end up with something likereturn 'Hello Lua!'
. - Save the page.
- Return to the sandbox test page you used above to test your changes. Using the module's talk page is very convenient for quick testing.
- Refresh the page to see your name returned from the script.
The result should be similar to:
Hello Lua!
Understand your first Lua script
Now that you see what the script does, it's time to understand how it works.
local p = {}
creates a local table or array for your code and names itp
.function p.hello()
adds a function namedhello
to the table. Functions can be invoked (called) by name from outside the module.return 'Hello!'
returns the stringHello!
when the function is invoked (called).end
ends the function.return p
returns the code table to whatever process loads this Lua module.
The code that runs the script includes:
invoke:
invokes (calls) a Lua module, and loads somethingSandbox
specifies the name of the module to be loaded.hello
specifies the name of the function inside the module that is to be invoked (called).
{{#invoke:Sandbox|hello}} | Keyword | 1st Parameter | 2nd Parameter |
Code |
#invoke: |
Sandbox |
hello |
What it does |
specifies action - here load module and implement function |
specifies the name of the module to be loaded |
specifies the name of the function inside the module that is to be invoked (called). |
Conclusion
Congratulations! You've now created, tested, edited, and understood your first Lua script.
Learn more
- See Wikiversity:Lua for more lessons
- See Wikibooks:Lua for lua basics