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
181
182
183
184
185
186
187
188
189
-- A variable can hold any data type in it. By default, a variable that has not been defined is set to nil, which is the Lua name for 'nothing'.
-- For future reference, there are 8 data types: nil, number, string, boolean, table, function, userdata, thread
var = 1 --Here, a variable has been defined and set to an integer value of 1
var2 = "Hello" --Here, a variable has been defined and set to a string value of "Hello"
var3 = function() print("Yes! Yes!") end --Yes, functions are variables, too
-- There are local and global variables. A local variable will only be addressable in the chunk it's been defined in, like thus:
local var1 = "test"
if var1 == "test" then --we check if var1 equals to the string value of "test"
	local var2 = "test indeed"
	print(var2) -> test indeed
end
print(var1) -> test
print(var2) -> nil
-- So, the var2, defined in that if chunk, will only be accessible IN that if chunk.
-- Local variables can also be defined like this, in some special cases:
local a --The local variable a now exists, but it isn't set to anything
-- Changing the value of an existing local variable does not require any prefix:
local a = 1
a = a + 1
print(a) -> 2
-- A demonstration of boolean variables --
-- A boolean variable can assume only two forms - true or false. It's a very good thing to use when you need a variable telling you if something is or is not, or other such applications. Although 1 and 0 are more understandable for some, true and false are generally better to use.
var = true
print(var) -> true
var = false
print(var) -> false
print(var.."a") -> attempt to concatenate boolean value
if var == true then print("true") end -- This can be used to check if a boolean variable is true, however a shorter variant is this:
if var then print("true") end
if var == false then print("false") end -- Shorter:
if not var then print("false") end --But beware, 'not var' will be true both if var is false and if it's nil!
----------------------
-- A string is a set of characters of some length. One of the most popular operations on a string is concatenation, i.e. appending one string to another, which is demonstrated here:
string = "Hello"
print(string) -> Hello
string2 = "there"
print(string2) -> there
-- .. concatenates (puts together) elements
print(string..string2) -> Hellothere
print(string.." "..string2) -> Hello there
print(string.."!") -> Hello!
-- In Lua, numbers can be concatenated with strings
print(string.." "..1) -> Hello 1
print(string.." 1") -> Hello 1
-- Even two numbers can be concatenated into a string; the following concatenation of two numbers will return a string:
print(1 .. 2) -> 12
---------------------------
-- A function performs actions in it and, if specified, returns a value when it finished (or failed to finish).
function test(par1)
	return par1
end
print(test(1)) -> 1
function test(par1,par2)
	return par1+par2
end
print(test(1,3)) -> 4
print(test(1,"lol")) -> attempt to perform arithmetic on string
-- In the following example, if the first parameter is a number, the function will stop and return the boolean value of 'true', and anything after that return statement will be ignored.
function test(par1)
	if type(par1)=="number" then --the type() function returns the data type of its argument in form of a string
		return true
	end
	return false
end
print(test(1)) -> true
print(test("a")) -> false
--------------------
-- Tables are storages which hold values in them.
tbl = {} --This is an empty table. The data type of the variable tbl will be table, however any index in it will be equal to nil.
tbl = {1,2,3} --This table is now not empty, and will have three indices - 1, 2, 3, each of which will have the value 1, 2, 3 respectively. Another notation:
tbl = {}
tbl[1] = 1 --Set tbl index 1 to 1
tbl[2] = 2 --Set tbl index 2 to 2
tbl[3] = 3 --Set tbl index 3 to 3
-- You can't print the table variable itself:
print(tbl) -> table: 0049A640
-- You can, however, print its indices:
print(tbl[1]) -> 1
print(tbl[2]) -> 2
-- A table's values can assume any data type.
tbl = {}
tbl[1] = 1
tbl[2] = "two"
tbl[3] = true
tbl[4] = {}
tbl[5] = function() print("test") end
-- A table can have keys, or indexes (the ones in []), of almost any data type, except for nil, like thus:
local test = function() print("test") end
tbl = {}
tbl2 = {}
tbl[1] = 1
tbl["two"] = 2
tbl[true] = 3
tbl[test] = 4
tbl[tbl2] = 5
tbl[nil] = 6 -> table index is nil
-- Side note: functions stored in tables can be accessed like so:
tbl = {}
tbl[1] = function(a1) print(a1) end
print(tbl[1](1)) -> 1
-- Side note:
tbl = {}
tbl["lol"] = "lol"
tbl["rofl"] = "rofl"
-- The following will be true:
tbl["lol"] == tbl.lol
tbl["rofl"] == tbl.rofl
-- It doesn't change anything, it just makes the code look prettier at times. It is called syntactic sugar.
-- You can also iterate through a table. The term iteration, put in simple words, is cycling through table values. There are default iterator functions called pairs and ipairs, which are used in loops. I will demonstrate:
for k,v in pairs(tbl) do
	print("["..k.."] = "..v)
end
-- The result will be three messages:
-> [1] = 1
-> [2] = 2
-> [3] = 3
-- The core difference between pairs and ipairs is that the former iterates through ALL keys, and the second only through numbered ones.
tbl = {1,2,3,["lol"] = "lol",["rofl"] = "rofl"}
for k,v in pairs(tbl) do
	print("["..k.."] = "..v)
end
-> [1] = 1
-> [2] = 2
-> [3] = 3
-> lol = lol
-> rofl = rofl
for k,v in ipairs(tbl) do
	print("["..k.."] = "..v)
end
-> [1] = 1
-> [2] = 2
-> [3] = 3
-- To get the length of a table, you can use the hash sign (#):
tbl = {1,2,3,4,5}
print(#tbl) -> 5
-- However, it does not return the absolute number of values in a table. See:
tbl = {1,2,3,4,[6]=5}
print(#tbl) -> 4
-- To get the absolute number of values, the following function can be used:
function table.count(tbl)
	local n=0
	for k,_ in pairs(tbl) do
		n=n+1
	end
	return n
end
-- You can see here that the function is, in fact, a value in the 'table' table, and without the syntactic sugar it's actually table["length"]. You can also see a vivid demonstration of how local variables, iterators and return statements are used in real functions.