summaryrefslogtreecommitdiff
path: root/samples/test.lua
blob: c5546ceecc17868235876df1e010044c0f702ffa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
-----------------------------------------------------------------------
-- LEARN LUA BY EXAMPLE
-- Read from top to bottom. Run the file and modify values as you go.
-----------------------------------------------------------------------

print("Hello, Lua!")  -- Printing to the console

-----------------------------------------------------------------------
-- 1. VARIABLES & TYPES
-----------------------------------------------------------------------

local name = "Lua"        -- string
local version = 5.1       -- number (Lua only has one numeric type)
local isFun = true        -- boolean
local nothing = nil       -- nil means "no value"

print(name, version, isFun, nothing)

-----------------------------------------------------------------------
-- 2. BASIC OPERATIONS
-----------------------------------------------------------------------

local a = 10
local b = 3

print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Power:", a ^ b)
print("Modulo:", a % b)

-----------------------------------------------------------------------
-- 3. STRINGS
-----------------------------------------------------------------------

local first = "Hello"
local second = "World"

-- Concatenation uses ..
local message = first .. " " .. second .. "!"
print(message)

print("Length of message:", #message)

-----------------------------------------------------------------------
-- 4. CONDITIONS (if / elseif / else)
-----------------------------------------------------------------------

local health = 25

if health > 50 then
    print("You are healthy")
elseif health > 0 then
    print("You are injured")
else
    print("You are dead")
end

-----------------------------------------------------------------------
-- 5. LOOPS
-----------------------------------------------------------------------

-- for loop
for i = 1, 5 do
    print("For loop i =", i)
end

-- while loop
local counter = 1
while counter <= 3 do
    print("While loop counter =", counter)
    counter = counter + 1
end

-----------------------------------------------------------------------
-- 6. TABLES (VERY IMPORTANT IN LUA)
-- Tables are arrays, dictionaries, objects, and structs all in one
-----------------------------------------------------------------------

-- Array-like table
local fruits = { "apple", "banana", "cherry" }

print("First fruit:", fruits[1]) -- Lua arrays start at 1

-- Loop over array
for i, fruit in ipairs(fruits) do
    print(i, fruit)
end

-- Dictionary-like table
local player = {
    name = "Hunter",
    level = 60,
    alive = true
}

print(player.name, player.level)

-----------------------------------------------------------------------
-- 7. FUNCTIONS
-----------------------------------------------------------------------

local function add(x, y)
    return x + y
end

print("Function result:", add(4, 6))

-----------------------------------------------------------------------
-- 8. FUNCTIONS AS VALUES
-----------------------------------------------------------------------

local function apply(a, b, fn)
    return fn(a, b)
end

print("Apply add:", apply(2, 3, add))

-----------------------------------------------------------------------
-- 9. SIMPLE OBJECT-LIKE TABLE
-----------------------------------------------------------------------

local Enemy = {}
Enemy.__index = Enemy

function Enemy:new(name, hp)
    local obj = {
        name = name,
        hp = hp
    }
    setmetatable(obj, self)
    return obj
end

function Enemy:takeDamage(amount)
    self.hp = self.hp - amount
    print(self.name .. " takes " .. amount .. " damage. HP =", self.hp)
end

local wolf = Enemy:new("Wolf", 40)
wolf:takeDamage(15)

-----------------------------------------------------------------------
-- 10. NIL AND CHECKING VALUES
-----------------------------------------------------------------------

local value = nil

if value == nil then
    print("Value is nil")
end

-----------------------------------------------------------------------
-- 11. GLOBAL VS LOCAL
-----------------------------------------------------------------------

globalVar = "I am global" -- avoid globals when possible!
local localVar = "I am local"

print(globalVar)
print(localVar)

-----------------------------------------------------------------------
-- 12. ERROR HANDLING
-----------------------------------------------------------------------

local function risky()
    error("Something went wrong!")
end

local success, err = pcall(risky)
print("Success:", success)
print("Error:", err)

-----------------------------------------------------------------------
-- END
-----------------------------------------------------------------------

print("You just walked through the basics of Lua 🎉")