games development 2 lua scripting co3301 week 6. contents introducing lua –comparison with python...
Post on 04-Jan-2016
241 Views
Preview:
TRANSCRIPT
Games Development 2 Lua Scripting
CO3301
Week 6
ContentsContents
• Introducing Lua– Comparison with Python
• Lua Language Overview
• Interfacing Lua with C++
Lua - BackgroundLua - 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
Lua – Key Language FeaturesLua – 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++)
Comparison with PythonComparison 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
Lua: Variables / Blocks / ConditionsLua: 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 < 5 or (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
Lua: Iteration / InputLua: 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 outputend
• Simple input:number = io.read("*n") -- Read number from inputname = io.read("*l") -- Read next line as stringprint("Hello"..name, "Your number is "..number)
Lua: FunctionsLua: Functions
• Simple function parameters are passed by value
function square(x)x = x*xreturn x
end
y = 3print(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)
Lua: ScopeLua: Scope
• All variables are global by default
function foo(x)var = 10*x
end
var = 2foo(5)print var --Prints 50
• Can explicitly request local variables
function foo(x)local var = 10*x
end
Lua: TablesLua: Tables
• Tables are associative arrays, with keys & values:
-- Table of values, default keys are integers from 1days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}print(days[1], days[3]) --Prints "Sunday Tuesday"
-- Table of keys and valuesrecord = {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 jobrecord.ID = 349332 --Add new key/value to record
-- Build a table of cubes from scratchnumbers = {} --Empty tablefor i=1,10 do
numbers[i]=i*i*iend
Lua: Looping with TablesLua: 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,"
Lua: nil & Garbage CollectionLua: 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 9a[3] = nil --Release memory for table entry with key “3”print (a[3],a[4]) --Prints "nil 16“: won’t change the keysa = nil --Release memory for entire table
LibrariesLibraries
• Several built in libraries:
-- Mathematicsprint(math.cos( math.pi / 3 )) --Output 0.5
-- Stringss = "Hello World"print(string.len(s), string.lower(s)) --Etc.
-- Tablesa = {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
Interfacing Lua with C++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
top related