cse 380 – computer game programming scripting and lua lua

19
CSE 380 – Computer Game Programming Scripting and Lua Lua

Upload: brooke-bennett

Post on 25-Dec-2015

248 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: CSE 380 – Computer Game Programming Scripting and Lua Lua

CSE 380 – Computer Game ProgrammingScripting and Lua

Lua

Page 2: 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

HardwareMachine CodeAssembly Code

High-Level Language CodeScripting Language Code

Page 3: CSE 380 – Computer Game Programming Scripting and Lua Lua

The first one? SCUMM

• Script Creation Utility for Maniac Mansion– by LucasArts for Commodore 64

Page 4: CSE 380 – Computer Game Programming Scripting and Lua Lua

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

Page 5: CSE 380 – Computer Game Programming Scripting and Lua Lua

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

Page 6: CSE 380 – Computer Game Programming Scripting and Lua Lua

2 Scripting Flavors

• 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

worldWidth:800

function process act(act) for i, act in ipairs(act) do if act:is_alive() then act.process() end endend

Page 7: CSE 380 – Computer Game Programming Scripting and Lua Lua

Typical Properties ofGame Scripting Languages

• Interpreted

• Lightweight

• Support for rapid iteration

• Convenience/Ease of use

• Good for Rapid Prototyping

Page 8: CSE 380 – Computer Game Programming Scripting and Lua Lua

Popular Game Scripting Languages

• Game Engine

–QuakeC

–UnrealScript

• General Purpose

–python

–Lua

Page 9: CSE 380 – Computer Game Programming Scripting and Lua 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/

Page 10: CSE 380 – Computer Game Programming Scripting and Lua Lua

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

Page 11: CSE 380 – Computer Game Programming Scripting and Lua Lua

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

--]]

Page 12: CSE 380 – Computer Game Programming Scripting and Lua Lua

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

Page 13: CSE 380 – Computer Game Programming Scripting and Lua Lua

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

Page 14: CSE 380 – Computer Game Programming Scripting and Lua Lua

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

Page 15: CSE 380 – Computer Game Programming Scripting and Lua Lua

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

Page 16: CSE 380 – Computer Game Programming Scripting and Lua Lua

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?

Page 17: CSE 380 – Computer Game Programming Scripting and Lua Lua

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

Page 18: CSE 380 – Computer Game Programming Scripting and Lua Lua

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

Page 19: CSE 380 – Computer Game Programming Scripting and Lua Lua

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