Presentation is loading. Please wait.

Presentation is loading. Please wait.

Games Development 2 Lua Scripting CO3301 Week 6. Contents Introducing Lua –Comparison with Python Lua Language Overview Interfacing Lua with C++

Similar presentations


Presentation on theme: "Games Development 2 Lua Scripting CO3301 Week 6. Contents Introducing Lua –Comparison with Python Lua Language Overview Interfacing Lua with C++"— Presentation transcript:

1 Games Development 2 Lua Scripting CO3301 Week 6

2 Contents Introducing Lua –Comparison with Python Lua Language Overview Interfacing Lua with C++

3 Lua - Background Lua is a lightweight scripting language –Small and simple feature-set –Small memory footprint Created in 1993 by Roberto Ierusalimschy –Pontifical Catholic University of Rio de Janeiro Lua means the “moon” Widely used in games industry –Including major engines and titles Good to have on CV

4 Lua – Key Language Features Small, but powerful feature set –The interpreter code compiles down to just 150k Dynamically typed Performs automatic garbage collection Not natively object-oriented Only one kind of data structure – the table Language features designed for extension: –Support other data structures –Implement OO-like features, etc. Simple integration with C API (and hence C++)

5 Comparison with Python Less high-level in its programming features –Fewer built-in data structures and language features –No OO –Code tends to be longer (more C-like) Rather a niche language outside games Better performance, less memory used –Was suitable even for last-gen consoles Simpler interface for C (& C++) –State-based interface (see later) lends itself well to game entity scripting

6 Lua: Variables / Blocks / Conditions Variable use (no types, equate several at once, no semicolons) – similar to Python: x,y,z = 1,2,3 first, second = second, first Blocks are defined by keywords, i.e. no braces or indentation requirement: if x 10 and x < 20) then print "The value is OK" end if x ~= 5 then print "The value is not equal to 5" end

7 Lua: Iteration / Input Loops (and comments): -- Loop from 1 to 10 inclusive (line comment) for i=1,10 do print("Iteration number", i) end --[[Every third value from 0 to 99 inclusive (block comment)]] for value=0,99,3 do print("Value="..value) --.. Concatenates output end Simple input: number = io.read("*n") -- Read number from input name = io.read("*l") -- Read next line as string print("Hello"..name, "Your number is "..number)

8 Lua: Functions Simple function parameters are passed by value function square(x) x = x*x return x end y = 3 print(square(y), y) --Prints "9 3“, y unchanged by fn Functions can return multiple results: function sincos(x) return math.sin(x), math.cos(x) end s,c = sincos(3.14)

9 Lua: Scope All variables are global by default function foo(x) var = 10*x end var = 2 foo(5) print var --Prints 50 Can explicitly request local variables function foo(x) local var = 10*x end

10 Lua: Tables Tables are associative arrays, with keys & values: -- Table of values, default keys are integers from 1 days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} print(days[1], days[3]) --Prints "Sunday Tuesday" -- Table of keys and values record = {name="Bob", job="Builder"} print (record.job) --Prints "Builder" print (record["job"]) --Exactly same as above, note the " print (record[job]) -- Error - looks for variable called job record.ID = 349332 --Add new key/value to record -- Build a table of cubes from scratch numbers = {} --Empty table for i=1,10 do numbers[i]=i*i*i end

11 Lua: Looping with Tables Special loop form for tables: --[[Function ‘ipairs’ iterates over integer keys in numeric order and gets their values]] fruit1 = {"apple", "banana", "plum",} for i,v in ipairs(fruit1) do print(i,v) end --Prints "1 apple, 2 banana, 3 plum," --[[Function ‘pairs’ iterates over all keys and values]] fruit2 = {best="apple", middle="banana", worst="plum"} for k,v in pairs(fruit2) do print(k,v) end --Prints "best apple, middle banana, worst plum,"

12 Lua: nil & Garbage Collection nil is a special value to represent non-initialisation or no useful value print(a) --Prints "nil" as a is not initialised Lua releases variables and tables when there are no remaining references them –Automatic garbage collection So we use nil to release memory: a = {1, 4, 9, 16, 25, 36, 49} print (a[3]) --Prints 9 a[3] = nil --Release memory for table entry with key “3” print (a[3],a[4]) --Prints "nil 16“: won’t change the keys a = nil --Release memory for entire table

13 Libraries Several built in libraries: -- Mathematics print(math.cos( math.pi / 3 )) --Output 0.5 -- Strings s = "Hello World" print(string.len(s), string.lower(s)) --Etc. -- Tables a = {5,3,4,9,1} table.insert(a, 2, 12) --insert at position 2 (after the 5) table.remove(a, 9) table.sort(a) -- And many others Many external libraries too

14 Interfacing Lua with C++ Interfacing Lua with C++ is fairly simple since Lua is itself a C program, and has a direct C API We will see the process in detail in the lab: –Create a Lua state variable, which persistently holds all the functions and globals that are declared This state will be retained between calls to scripts –Load a script file, all functions and globals in it will be registered and available to use –Call a script function by pushing it and its parameters onto the Lua stack –Get return values back after the call from the stack –All done with tidy C API calls


Download ppt "Games Development 2 Lua Scripting CO3301 Week 6. Contents Introducing Lua –Comparison with Python Lua Language Overview Interfacing Lua with C++"

Similar presentations


Ads by Google