🌙 Lua: an introduction
Tags: lua, programming, tutorial, gamedev
Published: 2023-02-09 11:15:01

---
cover_image: https://cdn.cloudflare.steamstatic.com/steam/apps/1730790/extras/skyisland.gif?t=1675702841
---

You have probably programmed with other languages like JavaScript or Java, or C++, but found yourself with the question _what is "Lua"?_, or you would simply like to learn about it or learn a new language, am I right?

![2d game](https://eliasdaler.files.wordpress.com/2016/03/u9cys5p.gif?w=372)

From video games to web applications, Lua has proven to be the perfect potion for creating dynamic and interactive content. So, grab your virtual wand and join me as we explore the wonders of Lua and discover why it's one of the most interesting languages in the programming realm!✨

## 🤔 What exactly is _Lua_? 

Before we begin with the more technical explanation, there is a funny particularity about the name, because "lua" in Portuguese means "moon". 😄 

![moon and clouds](https://media4.giphy.com/media/NPuROUdguXZUQ/giphy.gif)

Now, the technical side: Lua is a lightweight, high-level, multi-paradigm programming language designed primarily for embedded systems and clients. It is dynamically typed, interpreted, and garbage-collected, making it easy to learn and use. It supports procedural, object-oriented, and functional programming styles, and provides a simple syntax that can be used to build complex systems. It also has a small footprint, making it well suited for use in embedded systems, where memory and CPU resources are limited. Additionally, Lua has a strong ecosystem of libraries and tools, making it a popular choice for game development, scripting, and other applications.

## 💾 How do you install Lua?

Installing Lua depends on your machine, first of all, but I have divided the installation guide into Windows, MacOS and Linux so you only have to scroll to yours and start.

### Windows:

1. Download the latest version of the Lua binary distribution from the official website (www.lua.org).

2. Extract the contents of the downloaded archive to a directory of your choice.

3. Add the path to the Lua bin directory to your system's PATH environment variable.

```shell
set PATH=%PATH%;<directory where Lua was extracted>\bin
```

4. Open a command prompt or terminal and run the command "lua" to verify the installation.

### MacOS:

1. Download the latest version of the Lua binary distribution from the official website (www.lua.org).

2. Extract the contents of the downloaded archive to a directory of your choice.

3. Open Terminal and run the following commands:

```shell
$ cd <directory where Lua was extracted>
$ sudo make install
```

4. Verify the installation by running the command "lua" in the terminal.

### Linux:

1. Download the latest version of the Lua source code from the official website (www.lua.org).

2. Extract the contents of the downloaded archive to a directory of your choice.

3. Open Terminal and run the following commands:

```shell
$ cd <directory where Lua was extracted>
$ sudo make linux
$ sudo make install
```

4. Verify the installation by running the command "lua" in the terminal.

There is another possible alternative here, by installing Lua using the package manager for your specific distribution (e.g. apt-get on Ubuntu). For example:

```shell
$ sudo apt-get install lua5.4
```

In all cases, I recommend you check out the official documentation before jumping straight to installing: https://www.lua.org/manual/5.4/readme.html

# 👷‍♀️ Creating your first Lua script

Let's take the most basic of examples: writing a function. In Lua, you can define a function using the "function" keyword followed by the function name, its parameters (enclosed in parentheses), and its body (enclosed in curly braces). Sounds familiar, doesn't it? 😉

```lua
function greet(greeting, name) 
   return greeting + ", " + name
end

message = greet("Hey", "Bruno")
pring(message) -- Hey, Bruno
```

But, there's something new here: functions in Lua are _first-class citizens_, meaning they can be assigned to variables (as you can see with `message = greet("Hey", "Bruno")`), passed as arguments to other functions, and even returned as values from other functions. Here is an example use case: suppose you want to sort an array of numbers in ascending order. You can write a comparison `function` that returns `true` if the first argument is less than the second argument, and `false` otherwise. This comparison function can then be passed as an argument to the `table.sort` function, which sorts the array based on the results of the comparison function. Take a look at the code:

```lua
-- Define the comparison function
function compare(a, b) 
   return a < b
end

-- Define an array of numbers
numbers = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}

-- Sort the array using the compare function
table.sort(numbers, compare)

-- Print the sorted array
for i, v in ipairs(numbers) do
  print(v)
end

-- Output:
-- 1
-- 1
-- 2
-- 3
-- 3
-- 4
-- 5
-- 5
-- 5
-- 6
-- 9
```

In the above example, the comparison function `compare()` is a _first-class citizen_, meaning it can be assigned to a `variable`, passed as an `argument` to another function `(table.sort)`, and even returned as a value from another function. This allows for a high degree of flexibility and reusability in the code, and makes it easy to write generic functions that can work with any comparison function. Cool, right? 😉

## ✨ Thank you for reading!

Lua is a very powerful language and more articles will follow this one, so bear with me as we explore the realm of this language ✨🌙

![moon gif](https://looksrare.mo.cloudinary.net/0x02c4394307de62241aa8d5a0915494a96B288261/0x3058645742ef9614ed89ff5320b2cc0a3ba08b4d84001803f731a693eefa31f6?resource_type=image&f=auto&c=limit&w=iw&q=auto)

I'd like to thank you for reading my article and invite you to follow me on Dev.to, as well as my other platforms:

```
GitHub: https://github.com/bcostaaa01
Twitter: https://twitter.com/bruno2001costa
```

I look forward to seeing you on my next one!

Until then,
Happy Coding!👩‍💻