210 likes | 469 Views
CSE 380 – Computer Game Programming Scripting and Lua Lua. What is a scripting language?. A higher-level programming language (of sorts) Interpreted by another program at runtime like a game engine. Scripting Language Code. High-Level Language Code. Assembly Code. Machine Code. Hardware.
E N D
What is a scripting language? • A higher-level programming language (of sorts) • Interpreted by another program at runtime • like a game engine Scripting Language Code High-Level Language Code Assembly Code Machine Code Hardware
The first one? SCUMM • Script Creation Utility for Maniac Mansion • by LucasArts for Commodore 64
What’s the point? • NO MORE CONSTANTS • Why? • What’s wrong with constants? • They’re simpler languages • So? • Doesn’t more languages mean more complexity? • Programming for non-programmers • Compiler-time liberated development • Tools + Scriping
And for games? • Make Mods or Original Games • Use scripts to specify: • Game Settings • GUI layout • Audio cues • Control bots • Responses to game events • Bottom Line: • leave the heavy lifting to the engine. How? • tap into engine via premade script functions & data
2 Scripting Flavors worldWidth:800 • Data definition languages • declarative statements • used at game or level load time • Runtime languages • code executes within engine context at runtime • extend or customize engine • allows for more complex instructions function process act(act) for i, act in ipairs(act) do if act:is_alive() then act.process() end end end
Typical Properties ofGame Scripting Languages • Interpreted • Lightweight • Support for rapid iteration • Convenience/Ease of use • Good for Rapid Prototyping
Popular Game Scripting Languages • Game Engine • QuakeC • UnrealScript • General Purpose • python • Lua
A crash course in Lua • Let’s learn some simple stuff • Commenting • Variables • Operators • Functions • Tables • Conditional Statements • Iteration • Download LuaExample.zip and open • Ref: http://www.lua.org/manual/5.2/
A note about Lua • Lua is a C library • Using it in a C++ program can be tricky • structs instead of objects • global methods • unsupported C functions • An alternative is LuaPlus • not standard Lua • you could write your own wrapper classes as well
Lua Commenting • For single line, start line with “--” • Ex: -- THIS IS A COMMENT • To span multiple lines: • open with --[[ • close with ]]-- • Ex: --[[ THIS IS A COMMENT --]]
Lua Variables • Dynamically typed. Huh? • they can change type x = 3 -- x is an integer x = 3.14 -- x is a float x = "PI" -- x is a String • A Lua variable can be a: • number, string, boolean, table, function, nil, userdata, or thread • nil ≈ NULL • also marks variable for deletion • Default scope for Lua variables is global
Lua Operators • Many the regular suspects • like +, -, *, /, %, =, ==, <, >, <=, >= • Some you may not have used before, like: ^ for exponentiation ~= for testing for inequality .. for string concatenation • And some are missing • like ++, --, +=, -=, *=, /= • And some are done via keywords • Like and, or, and not
Lua Functions • First class objects. Huh? • Can be treated like any other variable • passed as a method argument • assigned to a variable • Function Declaration Example: function Square(val) return val * val end • Function Invocation Example: x = Square(5) -- invoke our function print(x) -- prints 25
Lua Tables • Lua’s only data structure • Arrays and generic dictionaries all in one. Ex: prime = {2, 3, 5, 7, 11 } -- make a table print(prime[2]) -- access an element, print 5 prime[2] = 13 -- assign a value • Tables can be indexed by other types. Ex: idTable = {} -- make a table idTable["id1"] = 123 -- put a num in idTable["id2"] = 321 -- and another • Table is actually either an array or a map
Lua Conditionals • Support for if … elseif … else • Ex: if x == 5 then print(5) elseif x == 6 then print(5) print(6) else print(0) end • How does it know when one code block ends and another begins?
Lua Iteration • Support for while and for statements • Ex: prime = {2, 3, 5, 7, 11} for index, value in ipairs(prime) do print(index, value) end • Nested Example: for i = 1, 10, 1 do if (i % 2) == 2 then print(i) end end
C++ Lua and Lua C++ • We may need to do both • Tricky with C++. Why? • Lua is in C. • Solution: Use LuaPlus • C++ library for interfacing with Lua • Not Lua standard • Wed: • LuaPlus hands on examples • C++ LuaPlus • LuaPlus C++ • How and why to do so
Reference • Game Engine Architecture by Jason Gregory • Chapter 14: Runtime Gameplay Foundation Systems • Game Coding Complete, 4th Edition by Mike McShaffry and David Graham • Chapter 12: Scripting with Lua