Lua: Hello World

From Mario Fan Games Galaxy Wiki
Add to this template
 Standardwikimessagebox.png This article assumes the use of Lua 5.1.

Information may not be accurate or may need revision if you are using a different version.

The following program is, of course, the ubiquitous Hello World program. Remember that Lua scripts require a host program to run, as they have no concept of a 'main()' function like C++; this host program could be something like SDL, one of MMF2's Lua extensions, or the main executable itself.

This tutorial was written with Lua 5.1.3 in mind, but it should work on newer and older versions due to its simplicity

print("Hello World!")

The above is sufficient to produce an output, but most things in Lua are handled through functions:

function hello()
    print("Hello World!")
  • Line 1: function hello(): function is a keyword that defines a variable containing a function. In Lua, functions are actually just another type of data allowed in a variable; in fact, there is another way this function could be defined: a = function()
  • Line 2: print("Hello World!"): Just prints "Hello World!" to the console window, if available.
  • Line 3: end: end signifies the end of most functions or statements; it is essentially equivalent to the '}' symbol in other languages.

Lua does not force the usage of semicolons to delimit statements or assignments (unless there would otherwise be an ambiguity), but they may be used if desired.