# Getting started with Lua

## Remarks

Lua là ngôn ngữ kịch bản tối giản (minimalistic), nhẹ và có thể nhúng. Sử dụng phổ biến gồm viết kịch bản trò chơi điện tử (scripting video game), mở rộng ứng dụng với plugins và configs, gói gọn một số logic nghiệp vụ cấp cao hoặc chỉ nhúng vào các thiết bị như TV, car,...

Trình biên dịch độc lập: just-in-time-compiler LUAJIT (link: <https://luajit.org/>)

## Install

{% embed url="<https://www.lua.org/download.html>" %}

## Comments

Single-line comments start with `--`

Block comments start with `--[[` and end with `]]`&#x20;

Trong khối comments có chứa nội dung giống với kiểu dấu phân cách thì ta sẽ sử dụng: (có thể thêm bao nhiêu dấu bằng cũng được)

```lua
--[=[
 This is also a block comment
 We can include "]]" inside this comment
--]=]

--[==[
 This is also a block comment
 We can include "]=]" inside this comment
--]==]
```

## Executing Lua programs

* `lua` - trình thông dịch độc lập và [shell interactive](#user-content-fn-1)[^1].
* `luac` - bytecode compiler

Câu lệnh để thực hiện file lua:

```sh
./lua name_file.lua
```

Câu lệnh hiển thị bytecode listing:

```
./luac -l name_file.lua
```

## Variables

```lua
var = 50 -- a global variable (biến toàn cục)
print(var) --> 50
do
 local var = 100 -- a local variable (biến cục bộ)
 print(var) --> 100
end
print(var) --> 50
```

## types

```lua
num = 20 -- a number
num = 20.001 -- still a number

str = "zaldrizes buzdari iksos daor" -- a string

tab = {1, 2, 3} -- a table (these have their own category)

bool = true -- a boolean value
bool = false -- the only other boolean value

print(type(num)) --> 'number'
print(type(str)) --> 'string'
print(type(bool)) --> 'boolean'
type(type(num)) --> 'string'

-- Functions are a type too, and first-class values in Lua.

print(type(print)) --> prints 'function'

old_print = print
print = function (x) old_print "I'm ignoring the param you passed me!" end
old_print(type(print)) --> Still prints 'function' since it's still a function.
-- But we've (unhelpfully) redefined the behavior of print.
print("Hello, world!") --> prints "I'm ignoring the param you passed me!"
```

### The special type `nil`

It is a kind of non-value value (nó tồn tại để khác biệt với tất cả các giá trị khác trong Lua).

### booleans

Only `false` and `nil` evaluate as false, evẻything else, including `0` and the empty string evaluate as true.

### tables

```lua
tab1 = {"a", "b", "c"}
tab2 = tab1
tab2[1] = "d"

print(tab1[1]) --> 'd' -- table values only store references.
--> assigning tables does not copy its content, only the reference.
tab2 = nil; collectgarbage()
print(tab1) --> (prints table address) -- tab1 still exists; it didn't get garbage-collected.
tab1 = nil; collectgarbage()
-- No more references. Now it should actually be gone from memory.
```

## expressions

```lua
a = 3
b = a + 20 a = 2 print(b, a) -- hard to read, can also be written as
b = a + 20; a = 2; print(a, b) -- easier to read, ; are optional though

true and true --> returns true
true and 20 --> 20
false and 20 --> false
false or 20 --> 20
true or 20 --> true
tab or {}
 --> returns tab if it is defined
 --> returns {} if tab is undefined
 -- This is useful when we don't know if a variable exists
tab = tab or {} -- tab stays unchanged if it exists; tab becomes {} if it was previously nil.
a, b = 20, 30 -- this also works
a, b = b, a -- switches values
```

## Defining functions

```lua
function name(parameter)
 return parameter
end
print(name(20)) --> 20

-- see function category for more information
name = function(parameter) return parameter end -- Same as above
```

## conditions

```lua
if (condition) then
 -- do something
elseif (other_condition) then
 -- do something else
else
 -- do something
end
```

## for loops

Vòng lặp có những dạng sau:

```lua
for a=1, 10, 2 do -- for a starting at 1, ending at 10, in steps of 2
 print(a) --> 1, 3, 5, 7, 9
end
```

Nếu bước nhảy bị bỏ qua, Lua sẽ mặc định là 1

```lua
for a=1, 10 do
 print(a) --> 1, 2, 3, 4, 5, etc.
end
```

Vòng lặp thông qua tất cả các giá trị mà hàm lặp trả về:

```lua
for key, value in pairs({"some", "table"}) do
 print(key, value)
 --> 1 some
 --> 2 table
end
```

[^1]: là loại shell tương tác với user, cho user biết ngay kết quả tương ứng được nhập vào tại dòng có prompt.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://viettaliii.gitbook.io/home/education/other/2.-lua-language/getting-started-with-lua.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
