User:Lakelimbo/Lua

From Bulbapedia, the community-driven Pokémon encyclopedia.
Jump to navigationJump to search

This is a draft for a future feature on Bulbapedia

What is Lua?

Lua is a programming language, a very simple one. Lua is what we'd call a "scripting language", and it's also dynamically typed, but don't worry about those terms. Lua is also a word, not an acronym (so don't say "LUA").

Lua is the language that is used by Scribunto.

What is Scribunto?

Scribunto is an extension for MediaWiki (the software that runs Bulbapedia) that allows us to run Lua code as an alternative to templates for more dynamic and/or complex use cases.

By default, the things we will write for Scribunto, with Lua, will live under the Module: namespace (just like how templates live in the Template: namespace).

When to use Lua Modules rather than Templates?

You can do most things on templates on a Module, and vice-versa, but there are trade offs with each approach. There is not a strict "rule", so to say, that will define that you should use Modules for X thing always, or templates for Y, but you can think like this: if the Template might benefit from a more dynamic or "programmatic" approach, i.e. it gets unnecessarily complex, it might be worth turning it into a Module. In reverse, if it's too basic, it's a bit of an overkill to make a Module.

Take two examples: a Module calculating the type effectiveness for Pokémon types would work much better than a traditional Template simply because with Lua, being an actual programming language, you can create variables, loops, and generally more complex conditions that might be a huge mess to handle on a template (or might need a very different approach). In the other hand, a Template for something displaying just a few blocks of text or links will work better simply because Templates are more basic and easier to maintain, so creating a whole Module for it is not necessary.

But as mentioned, it depends, if you're still on doubt, talk to a staff member to decide if something should be a Template or a Module. Lua is faster than Templates (because it's interpreted directly by the Lua interpreter, while Templates are parsed by something called Parsoid which then is interpreted by PHP, the language that runs MediaWiki), but the difference might not significant for certain use cases.

Learning Lua

Lua is, as mentioned, a programming language, so if you never had contact with any programming language before you will need to learn at least some basic fundaments. Of course this tutorial is much simpler and aimed at writing Lua for Bulbapedia specifically, but it's highly recommended to check out the official Lua documentation or simply search about it on the web.

Variables

Variables are like a "place", or a container, where you can store a piece of information/data. You can think of it like variables on mathematics, where you have an equation (say ) and might be equal to 5, so 5 is the data we're storing on this variable.

To create a variable in Lua, you simply type its name (which will identify the variable so it can be called later), an equal sign, and the value in front of it:

foo = "Hello, Bulbasaur!"

So I could call the built-in function print() (we'll talk about this in the following sections), which will print anything you put there into the standard output (like the terminal):

foo = "Hello, Bulbasaur!"

print(foo)
-- will print "Hello, Bulbasaur!"

Global and private variables

If you search about Lua however, you might see that most of the time we define variables using the keyword local rather than just placing the name and its value. This is because, for whatever reason, variables are global (public) by default, which most of the time might not be preferred. Public variables basically mean they're accessible elsewhere (like outside of its scope — we'll see about this as well). Local (or private) variables are only accessed, well, locally, which is probably your intention most of the time. So let's modify the previous example:

local foo = "Hello, Bulbasaur!"

Now foo is a local variable.

Scope

Talking about this, let's explain simply what scope is. It's basically the "area", so to say, where that piece of code is "running" (or that can be accessed). Most languages (like C, C#, Java, etc.) scope is defined by curly brackets (example in JavaScript):

let foo = 123;

function first() {
  let bar = 345;
  // I can call `foo` here because it's in the "parent" scope
}

function second() {
  let cat = 678;
  // I can call `foo` here because it's in the parent scope
  // But I can't call `bar` because it's in the scope of `first()`
}

// I can't call `bar` or `cat` here because it's in the scope of `first()` and `second()`, respectively

Some languages like Python defines scope with only indenting, but Lua is more similar to Ruby or Elixir and uses the keywords end and sometimes do and then, so this JS example before would look like this:

local foo = 123

function first()
  local bar = 345
end

function second()
  local cat = 678
end

Another important thing to note: unlike some other languages, semicolons are optional to determine the end of some statement. This means that it is a good practice to properly break, indent, and space your code so it doesn't get misinterpreted.