lua is not a strict type language but supported 'types' would be doubles(floats? i do not remember my programming enough to remember the max values of doubles vs. floats nor do I know the max lua number), int, strings (obviously char because string is C* right), booleans, tables, and nil (null if your a c programmer)
I am not going to teach you everything here because it would be a glorious waste of my time and others who have already written these tuts, my aim is to get your feet on the ground so you can start running on learning the scripting language wherever your script ideas take you.
local num = 4
print(num) --4
print(type(num)) -- 'number'
num = num / 5 --+,-,/,*,% are acceptable operators for numbers
print(num) -- 0.8
print(type(num)) -- 'number'
num = 'It does not care you just made it a string'
print(num)
print(type(num)) -- 'string'
num = "a"
print(type(num)) -- 'string'
num = true
if num then print(num) end --prints (true)
local s = 'This statement will produce an error '
if not num then print(s..num) end --concat is .. but you can not concat strings with numbers/booleans
if num then print(s..tostring(not num)) end --you can tostring numbers and booleans, not tables. Well, you can but it spits out the table address
--Alright, enough with variable types. Lets push the ticket.
--===========================================--
--Tables!
--Inserting values
local t = {} -- { } denotes table.
t[1] = 2
t[2] = 4
for i = 3, 10 do
t[i] = 2*i
end
--First, I'll teach about the # operator.
print(#t) --prints 10, because there are 10 elements in t.
for i = 1, #t do print(t[i]) end -- prints 2,4,..20
--now, the in ipairs function
for k,v in ipairs(t) do
print('t['..tostring(k)..'] = '..tostring(v)..', ')
end
--now the in pairs function
for k,v in pairs(t) do
print(tostring(k)..' '..tostring(v)) --same as ipairs in this case
end
--now the caveats
t = {}
t = { ['Hello'] = 'Good Bye', ['Hallo'] = 'Chuse' }
print(#t) --prints 0 because you aren't using a contiguous integer key set. Thus:
for i = 1, #t do
print(t['Hello']) --never enters because the for loop is for i = 1, 0.
end
--the for k,v in ipairs(t) also fails because it ipairs is an ordered pair call and you do not have an ordered pair of keys and values.
--BUT the following works :)
for k,v in pairs(t) do
print(k..' '..v)
end
--The caveat, is the printed pair follows no specific order.
t = {}
for i = 1, 5 do
t[i] = i
end
--let us say we want to introduce a new element to the table at key 3, but we don't want to over right that value, but push it back.
-- the long way is
for i = #t, 3 do
t[i+1] = t[i]
end
t[3] = 'New Value'
-- The short way
table.insert(t,3,'New Value')
--Done.
Okay, I'm done for now. Play around, ask questions.