1-----------------------------------------------------------------------
2-- LEARN LUA BY EXAMPLE
3-- Read from top to bottom. Run the file and modify values as you go.
4-----------------------------------------------------------------------
5
6print("Hello, Lua!") -- Printing to the console
7
8-----------------------------------------------------------------------
9-- 1. VARIABLES & TYPES
10-----------------------------------------------------------------------
11
12local name = "Lua" -- string
13local version = 5.1 -- number (Lua only has one numeric type)
14local isFun = true -- boolean
15local nothing = nil -- nil means "no value"
16
17print(name, version, isFun, nothing)
18
19-----------------------------------------------------------------------
20-- 2. BASIC OPERATIONS
21-----------------------------------------------------------------------
22
23local a = 10
24local b = 3
25
26print("Addition:", a + b)
27print("Subtraction:", a - b)
28print("Multiplication:", a * b)
29print("Division:", a / b)
30print("Power:", a ^ b)
31print("Modulo:", a % b)
32
33-----------------------------------------------------------------------
34-- 3. STRINGS
35-----------------------------------------------------------------------
36
37local first = "Hello"
38local second = "World"
39
40-- Concatenation uses ..
41local message = first .. " " .. second .. "!"
42print(message)
43
44print("Length of message:", #message)
45
46-----------------------------------------------------------------------
47-- 4. CONDITIONS (if / elseif / else)
48-----------------------------------------------------------------------
49
50local health = 25
51
52if health > 50 then
53 print("You are healthy")
54elseif health > 0 then
55 print("You are injured")
56else
57 print("You are dead")
58end
59
60-----------------------------------------------------------------------
61-- 5. LOOPS
62-----------------------------------------------------------------------
63
64-- for loop
65for i = 1, 5 do
66 print("For loop i =", i)
67end
68
69-- while loop
70local counter = 1
71while counter <= 3 do
72 print("While loop counter =", counter)
73 counter = counter + 1
74end
75
76-----------------------------------------------------------------------
77-- 6. TABLES (VERY IMPORTANT IN LUA)
78-- Tables are arrays, dictionaries, objects, and structs all in one
79-----------------------------------------------------------------------
80
81-- Array-like table
82local fruits = { "apple", "banana", "cherry" }
83
84print("First fruit:", fruits[1]) -- Lua arrays start at 1
85
86-- Loop over array
87for i, fruit in ipairs(fruits) do
88 print(i, fruit)
89end
90
91-- Dictionary-like table
92local player = {
93 name = "Hunter",
94 level = 60,
95 alive = true
96}
97
98print(player.name, player.level)
99
100-----------------------------------------------------------------------
101-- 7. FUNCTIONS
102-----------------------------------------------------------------------
103
104local function add(x, y)
105 return x + y
106end
107
108print("Function result:", add(4, 6))
109
110-----------------------------------------------------------------------
111-- 8. FUNCTIONS AS VALUES
112-----------------------------------------------------------------------
113
114local function apply(a, b, fn)
115 return fn(a, b)
116end
117
118print("Apply add:", apply(2, 3, add))
119
120-----------------------------------------------------------------------
121-- 9. SIMPLE OBJECT-LIKE TABLE
122-----------------------------------------------------------------------
123
124local Enemy = {}
125Enemy.__index = Enemy
126
127function Enemy:new(name, hp)
128 local obj = {
129 name = name,
130 hp = hp
131 }
132 setmetatable(obj, self)
133 return obj
134end
135
136function Enemy:takeDamage(amount)
137 self.hp = self.hp - amount
138 print(self.name .. " takes " .. amount .. " damage. HP =", self.hp)
139end
140
141local wolf = Enemy:new("Wolf", 40)
142wolf:takeDamage(15)
143
144-----------------------------------------------------------------------
145-- 10. NIL AND CHECKING VALUES
146-----------------------------------------------------------------------
147
148local value = nil
149
150if value == nil then
151 print("Value is nil")
152end
153
154-----------------------------------------------------------------------
155-- 11. GLOBAL VS LOCAL
156-----------------------------------------------------------------------
157
158globalVar = "I am global" -- avoid globals when possible!
159local localVar = "I am local"
160
161print(globalVar)
162print(localVar)
163
164-----------------------------------------------------------------------
165-- 12. ERROR HANDLING
166-----------------------------------------------------------------------
167
168local function risky()
169 error("Something went wrong!")
170end
171
172local success, err = pcall(risky)
173print("Success:", success)
174print("Error:", err)
175
176-----------------------------------------------------------------------
177-- END
178-----------------------------------------------------------------------
179
180print("You just walked through the basics of Lua ๐")