Lua: Hello World
|Add to this template|
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
The above is sufficient to produce an output, but most things in Lua are handled through functions:
function hello() print("Hello World!") end
- 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.