Download presentation
Presentation is loading. Please wait.
Published byAlban Copeland Modified over 9 years ago
1
References: 1. “Programming in LUA, 2 nd ed.”, Roberto Lerusalmschy Chapters 1-6 2. “Lua Reference Manual” (included in Lua installation or online)
2
What is Lua? A very lightweight scripting language Very bare-bones Easy C integration (note: NOT C++) Reasonably Fast (for a scripting language) Used as a scripting language in many games WoW The Witcher Legend of Grimlock … Getting it: http://www.lua.orghttp://www.lua.org Documentation: http://www.lua.org/docs.htmlhttp://www.lua.org/docs.html Trivia: “Lua” means moon in Portugese
3
Our Intro’s steps ( one lecture / mini-lab each / quiz for each ) 1. Get familiar with the non-OOP features (this lecture) 2. OOP-esque programming in Lua 3. Writing a C extension to Lua 4. Embedding Lua in a C program. 5. Integrate Lua into our Engine Note: We’re NOT going to cover a significant amount of Lua. We’ll just look at enough to get started – then it’s up to you!
4
Running Lua programs For now… Create a.lua (text) file in Notepad++ Run through the command line Eventually… No command-prompt. We’ll make ssuge contain an embedded Lua interpreter which works with our C/C++ code.
5
Simple example ( sample01.lua ) print(“Hello, World”) -- it has to be this…:-) save it to sample01.lua (mine is in c:\Shawnee\2014sp\etgg3802\lua) run “cmd” from the start menu
6
Something a little more exciting (sample02.lua) -- defines a new factorial function function fact(n) if n == 0 then return 1 else return n * fact(n - 1) end print("Enter a number: ") a = io.read("*number") -- could also be... -- [no args] waits for enter -- "*all" reads file contents(not useful here) -- "*line" reads a line of text -- n reads n (int) characters print(a.. "! = ".. fact(a)) -- concatenation with auto-conversion
7
Lua types These are the only built-in constructs in Lua nil (a non-value; useful to indicate garbage) booleans numbers (doubles, (sort of) int’s) strings (ascii 8-bit) tables (the biggie) functions userdata (we’ll see these when embedding) threads (we won’t cover these) Note: no classes, but we can use tables to do OOP.
8
string examples (sample03.lua) x = "This is a \"test\"" print(x) y = string.gsub(x, "test", "quiz") print(y) -- “This is a “quiz”” z = [[This is a really long string covering four lines]] print(z) -- useful for a block "comment" q = [==[Another multi-line text]==] -- This is necessary because sometimes we'll use [[ and ]] -- for other things. e.g. a = b[c[i]] (list-like access) -- You can have any number of '=' characters. print(q) r = string.rep(“abc”, 3) print(r) -- abcabcabc
9
string examples, cont. print("10" + 1) -- "11" print("10 + 1") -- "10 + 1" print("15" * "3") -- "45" (auto-conversion to number) print("-5.3e-10" * "2") -- -1.06e-9 (auto-conversion to number) --print("hello" + 1) -- ERROR print(10.. 20) -- "1020" (auto-conversion to string) print(tostring(10).. tostring(20)) -- "1020" (preferred way)
10
string examples, cont. print("Enter something: ") response = io.read() n = tonumber(response) -- nil if unconvertable if n == nil then error("'".. response.. "' can't be converted to a number" ) else print(n * 2) end xlen = #x -- length of (string) x print("'".. x.. "' is ".. xlen.. " characters long") -- a wrapper for C’s printf function print(string.format("[%s, %d::%0.2f]", "hello", 5, 15.37894))
11
string examples, cont. s = "==qasdlkfjh==slkjh==u==" -- Extracting a sub-string subs = string.sub(s, 2, 10) -- includes start / end indicies -- finds the character *after* each ==, if there is one indx = 1 while true do new_indx, end_indx = string.find(s, "==", indx) if new_indx == nil then break elseif end_indx < #s then print("(".. new_indx.. ") = ".. string.sub(s, end_indx + 1, end_indx + 1)) end indx = new_indx + 1 end
12
Tables Nearly everything in Lua is a table. A table is an associative array A collection of key-value pairs ○ Both can be any Lua “thing” (including other tables!) Unordered Like a python dictionary or STL (C++) maps
13
Table example (sample04.lua) a = {} -- an empty table a.x = 15 a.y = "hello" a.z = 3.7 print(a) -- table: 003FC9B0 for k, v in pairs(a) do print("key=".. k.. ", value=".. v) end
14
Table example, cont. a.x = a.x + 1 a["x"] = a["x"] + 1 -- same as line before print(a.x) print(a.q) -- nil: a non-existent key (not an error...) b = a -- Note: a and b are *pointers* to the *same* table b.y = "goodbye" print(a.y) -- goodbye
15
Table example, cont. -- sorting the keys of a table temp_keys = {} for k, v in pairs(a) do temp_keys[#temp_keys + 1] = k end table.sort(temp_keys) for _, v in pairs(temp_keys) do -- _ is a "don't-care" variable print("a[".. v.. "] = ".. a[v]) end
16
Table example, cont. -- creating a table with (integer) values c = {[5] = "abc", [6] = "def"} -- integer keys c[7] = "xyz" -- new int key for i = 5, 7 do print(c[i]) end -- Looks a lot like an array, doesn't it... --...in fact there is NO array type in lua. This is it!
17
Table example, cont. -- lua "lists" start with index 1 by convention. d = {} for i = 1, 10 do d[i] = i * i + 2 end print(#d) -- 10 (length -- only works for 1-based "lists") print(d[#d]) -- 102 (last element) print(d[10]) -- 102 print(d["10"]) -- nil print(d["+10"]) -- nil (not the same key as "10")
18
Table example, cont. -- table with table values (a 2d table) times_table = {} for i = 1, 10 do times_table[i] = {} for j = 1, 10 do times_table[i][j] = i * j end -- pull out result for 5 * 7 print(times_table[5][7]) -- or times_table[7][5] here
19
[File] I/O (sample5.lua) (This is the more general method) And a taste of OOP in Lua -- Wrapping in the assert call will fail if io.open returns nil (error) local f = assert(io.open("test.txt", "r")) count = 1 while true do -- Note the colon here. f is an "object" and we're calling -- the read "method" L = f:read("*line")-- reads one line of text. Could also be -- "*all": reads entire file -- n: Reads n characters if L == nil then break end print("Line #".. count.. " = '".. L.. "'") count = count + 1 end f:close()
20
local vs. global variables Normally all variables are global. Regardless of where they're defined Put the local keyword before the name when creating to limit it to that block It's good practice to use locals to hide "unncessary" variables Encapsulation
21
local example (sample06.lua) x = 10 -- a global function foo() y = 99 -- also a global print(x) -- 10 local x = 11 print(x) -- 11 (masks the global x) if x < 50 then local x = 12 print(x) -- 12 masks the local above end print(x) -- 11 (the 12 is no longer masking) end foo() print(x)-- 10 (function locals are gone)
22
command-line args An alternate way to call lua from command line: lua script_name.lua arg1 arg2 … lua sample1_07.lua 5 3.2 abc -- command-line arguments test for i, val in pairs(arg) do print("arg#".. i.. " = '".. val.. "'") end
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.