scripting programs with lua

9
Scripting Programs with Lua Christian Vogler Gallaudet Research Institute 2 What is Scripting? Scripting languages are programmin g languages, with three key characteristics: Very high level No fiddling with bits, bytes, and pointers Interpreted Instant “gratification,” interactive development Dynamically typed No need to declare variables, types are inferred from what you actually do with the vari able 3 Demonstration of Scripting Show scripting demonstrations 4 Scripting – why do it? What is scripting useful for? Quickly allows you to change behavior of complex programs “Glue” program parts together Makes you much more productive Because of high-level and dynamic typ ing No need to wait f or long compiles More flexible than configuration files With scripti ng languages, you don't need to anticipate every possible option

Upload: mikhail-miguel

Post on 06-Apr-2018

239 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Scripting Programs With Lua

8/3/2019 Scripting Programs With Lua

http://slidepdf.com/reader/full/scripting-programs-with-lua 1/9

Scripting Programs with Lua

Christian Vogler

Gallaudet Research Institute

2

What is Scripting?

  Scripting languages are programminglanguages, with three key characteristics:

  Very high level¡

No fiddling with bits, bytes, and pointers 

Interpreted¡ Instant “gratification,” interactive development

 

Dynamically typed¡ No need to declare variables, types are inferredfrom what you actually do with the variable

3

Demonstration of Scripting

  Show scripting demonstrations

4

Scripting – why do it?

  What is scripting useful for? 

Quickly allows you to change behavior ofcomplex programs

¡

“Glue” program parts together 

Makes you much more productive¡

Because of high-level and dynamic typ ing  No need to wait for long compiles 

More flexible than configuration files¡ With scripting languages, you don't need toanticipate every possible option

Page 2: Scripting Programs With Lua

8/3/2019 Scripting Programs With Lua

http://slidepdf.com/reader/full/scripting-programs-with-lua 2/9

5

The Lua Scripting Language

  “Lua” = Portuguese word for “moon” 

Developed in Brazil, originally on a contractwith Petrobras, the national oil company

  Free software (MIT license)¡

(Don't tell Darl, or SCO might get the idea that itis somehow a derivative work of System V!)

6

YASL?

  Yet Another Scripting Language??? 

After all, we have Perl, Python, Guile, Ruby,Tcl, ...

  True, but choose the best tool for the job 

Lua shines at embedding, gluing togetherother program parts, and data description

7

Advantages of Lua

  Lua is:¡

Small  < 500 KB of source code, can be further stripped

¡

Written in pure ANSI C  Compiles out of the box everywhere

¡

Fast  At the top of the pack among scripting languages¡ Easy to learn and use

  Simple syntax, a lot of nonprogrammers in the gameindustry use it

¡ Powerful  Mechanisms for extension

8

Some projects that use Lua

  Games¡

Baldur's Gate, Escape from Monkey Island,MDK, Grim Fandango

¡

Mostly used to control AI and behavior¡ This is where many designers (non-

programmers) also use Lua 

Tools¡ APT-RPM, Clan Lib¡ Extend tool functionality via scripting

Page 3: Scripting Programs With Lua

8/3/2019 Scripting Programs With Lua

http://slidepdf.com/reader/full/scripting-programs-with-lua 3/9

9

Fundamental Lua Datatypes

  Numbers, strings

 

¡ ¢

£

¤

¥

¦

§

¨

© 

 

¡ ¢

£

¤

¥

¦

 

¢

 

 

¢

 

 

¡ ¢

£

¤

¥

¦

 

 

 

¢

 

¢

 

 

¡ ¢

£

¤

¥

¦

   !

£

 "

£

 " # "

¥

¢

£

¤ $

 

¥

 "

¥

¢

£

¤ $

¦

§

©  

 ! £

 "

£

 " # "

¥

¢

£

¤$

§ ©

10

Variables

  Variable assignment and type conversion

 

%

 

¡ ¢

£

¤

¥

¦

%

 

 

'

(

 

¡ ¢

£

¤

¥

¦

%

¨

'

 

)

 

0

  1

 "

¥

¢

£

¤ $ 2 

 

 

 "

¥

¢

0

'

 

¡ ¢

£

¤

¥

¦

 "

¥

¢

 

1

 "

¥

¢

£

¤ $ 2 

(

11

Tables

  The basic data structure are tables  Can be used as arrays or lists:

 

¥

3 §

 4

§

 4

©

 4

 4

6

 4

7

 4

§

 4

©

§ 8

 

¡ ¢

£

¤

¥

¦

¥

9 § @

 

§

 

¡ ¢

£

¤

¥

¦

¥

9 ( @

 

¡ ¢

£

¤

¥

¦

¥

#

B

 $

¥

¤

¦

¥

 

7

 

¡ ¢

£

¤

¥

¦

¥

9

C

 @

 

¤

£

12

Tables, continued

  Tables are also dictionaries 

 

£

 D

¥

3 8

 

  £

 D

¥

9 E F G  @

  H £

¥

£

 $

£

 I

 "

P

# "

¥

# ¢

 

 "

 

 

 

£

 D

¥

9 Q P  R @

S

E

 T ¤ '  I

 

¤

 

¥

 T  " " U

£

¥

 ! S

 

¡ ¢

£

¤

¥

¦

 

£

 D

¥

9

E F G

  @

 

H

£

¥

£

 $

£

 I

 "

P

# "

¥

# ¢

 

 "

An alternative way to access:

 

¡ ¢

£

¤

¥

¦

 

£

 D

¥

Q PR

 

E

 T ¤

'

 I

 

¤

 

¥

 T  " "

U

£

¥

 !

Page 4: Scripting Programs With Lua

8/3/2019 Scripting Programs With Lua

http://slidepdf.com/reader/full/scripting-programs-with-lua 4/9

13

More on table construction

  Often tables are used to describe data orobjects

  Then this construction syntax is moreconvenient

 

 D # ¤  # "

3

U

£

 

¥

 !

¡

(

C

 4

 !

£

 $

 !

¥

(

7 C

 4

¤ # T

S

 T #

£

¤

S

 4

 D ¢

 

£

¤ #

¥

 "

3 §

C C

 4

© C C

 4

C C

8 8

Same as:

 

 D # ¤

 

# "

3 8

 

 D # ¤

 

# "

U

£

 

¥

 !

¡

(

C

 

 D # ¤

 

# "

 D ¢

  £

¤ #

¥

 "

3 § C C

 4

© C C

 4

C C 8

14

Iterating on tables

  To do something with all elements in a table,we can use for loops

 

¢

¢

£

§

 4

¥

#

B

 $

¥

¤

¦

¥

 

 4

§

 

¡ ¢

£

¤

¥

¦ £

 4

¥

9

£

 @

 

 

¤

 

 

¢

¢

£

'

 4

  #

 I

£

¤ D # ¤  # "

 

 

¡ ¢

£

¤

¥

¦ £

'

 4

 

#

 I

 

 

¤

 

15

Functions

  Defining functions¢

 I

¤ D

¥

£

¤ ¡ ¢

£

¤

¥ ¥

#

B

¦

¥

 4

 !

#

 

¢

 

 D #

¤

 I

 T

T ¤

¥

 "

C

¡ ¢

£

¤

¥

¦

 !

#

 

¢

 

¢

¢

£

'

 4

  #

 I

£

¤

¥

 

¡ ¢

£

¤

¥

¦

 

¤

¥

¢

'

 

 4

£

'

 4

 

 

#

 I

 

 

¤

 I

 T

T ¤

¥

 "

¤

 I

 T

T ¤

¥

 "

¨

§

¤

 

¢

¥

 I

¢ ¤ ¤

 I

 T

T ¤

¥

 "

¤

 

 

¤

 I

 T

¡ ¢

£

¤

¥ ¥

#

B

¦

 D # ¤

 

# "

 4

   !

D # ¤

 

# "

   

 

¡ ¢

£

¤

¥

¦

¤

 I

 T

 

16

If statement

  Conditional execution of a block¢

 I

¤ D

¥

£

¤ " $ ¤

¦

#

 

£ ¢

#

¥

C

¥

 !

¤

¢

¥

 I

¢ ¤

§

 "

£ ¢

#

§ 

C

¥

 !

¤

¢

¥

 I

¢ ¤

§

 "

¢

¥

 I

¢ ¤

C

¤

 

¤

 

 

¡ ¢

£

¤

¥

¦

 " $ ¤

¦ ( 

 

¡ ¢

£

¤

¥

¦

 " $ ¤

¦ C 

 

¡ ¢

£

¤

¥

¦

 " $ ¤

¦

)

 

Page 5: Scripting Programs With Lua

8/3/2019 Scripting Programs With Lua

http://slidepdf.com/reader/full/scripting-programs-with-lua 5/9

17

Metamechanisms

  This is all good and well, but nothing fancy sofar

  The real power of Lua comes from extendingand customizing the behavior of tables viametatables 

  Every table has an associated metatable that

controls its behavior

18

Observing changes

  A very common application: Detecting whensomeone wants to create a new entry in atable

  First we create the function that should becalled when we detect a new entry

¢

 I

¤ D

¥

£

¤ ¤

U

¤

¥

¢

'

¦

¥

 4

£

'

 4

 

#

 I

 

¡ ¢

£

¤

¥

¦

 

 F

¢ #

¥

£

¤ $ ¤ U ¤

¥

¢ '

£

¤

¥

#

B

 

 4

¥

 

¡ ¢

£

¤

¥

¦

   

'

 

 4

£

'

 4

  ¡

#

 I

 

 4

 

#

 I

 

¢ #

U

 "

¥

B

'

¡ # " "  "

¥

 !

T

¥

#T  D

 !

# ¤

£

 " T

 4

 "

¥

 !

£

 "

¢

 I

¤ D

¥

£

¤

£

 " ¤

¥

 D #

 

 

¢ # ¤

 

 

¢

¢ #

U

 "

¥

¦

¥

#

B

 4

£

'

 4

 

#

 I

 

¤

 

19

Observing changes

  Next, we create a new metatable thatoverrides the default behavior for creatingnew entries

 T

¥

3

¤

U

£

¤

 

%

¤

U

¤

¥

¢

'

8

 

Now we create a new table, and associatethe metatable with it

¥

3 8

 "

¥

 T

¥

#

¥

#

B

¦

¥

 4

 T

¥

 

20

Observing changes

  Let's try if it works 

¥

¤ # T

S

G

B

 " ¢ 

 

#

B

S

 F

¢ #

¥

£

¤ $ ¤

U

¤

¥

¢

'

£

¤

¥

#

B

¥

#

B

C

%

7 C ¡

#

¢

 D

7

 

'

2 ¤ # T

¡

#

 I

G B

 " ¢

 

 

#

B

 

¥

9 § @

©

 F

¢ #

¥

£

¤ $ ¤

U

¤

¥

¢

'

£

¤

¥

#

B

¥

#

B

C

%

7 C ¡

#

¢

 D

7

 

'

§ ¡

#

 I

©

 

¥

9 § @

This time, the function is not called, because the entry with key 1already exists.

Page 6: Scripting Programs With Lua

8/3/2019 Scripting Programs With Lua

http://slidepdf.com/reader/full/scripting-programs-with-lua 6/9

21

Metamechanisms

  There are many other allowed metamethods,among them:

¡

Arithmetic operations: +, -, *, /, exponents¡ Comparisons: equality, less than¡ Function calls (a table can be treated as afunction)

¡ Reading from a table

22

Use of metamechanisms

  They form the basis for extending Lua to fitthe application's paradigm

¡

e.g. Object oriented, data driven, ... 

They allow binding to and manipulating C/C++structures and objects

 

They allow using tables to control complex

operations¡ e.g., use a table field to control the orientation ofa robot's arm

23

Lua for data description

  Lua is ideally suited to describe data andconfiguration files

 

In fact, it started this way in 1994  XML is all the rage, but Lua has threeadvantages over it:

¡

Much easier to use than an XML parser¡ Much smaller than the monstrous XML librariesout there

¡ The third one will become apparent in this talk

24

Data description

  We want to describe a 3D model of thehuman face, as in the demo

 

The model consists of “faces” and “nodes”  Each face specifies a triangle for threeparticular nodes

 

Each node has a position that can becomputed

Page 7: Scripting Programs With Lua

8/3/2019 Scripting Programs With Lua

http://slidepdf.com/reader/full/scripting-programs-with-lua 7/9

25

Example: a square

¤

C

 

 

¦ C

 4

C

 4

¤

§

 

 

¦ C

6

 4

C

 4

¤

©

 

 

¦ C

6

 4

C

6

 4

¤

 

 

¦ C

 4

C

6

 4

¡

# D

¦

¤

C

 4

¤

§

 4

¤

©

¡

# D

¦

¤

©

 4

¤

 4

¤

26

Binding to C or C++

  Next, we have to create the two functions forNode and Face in C, which do the actualconstruction of the object

  We can use the Lua API for this  Take a look at

¢

£ ¤

¥

§ ¨ ¤

© ¢

¨

¤ ¨

©

¨

 

 

Compared to using XML, this is prettypainless

  The real meat ...

27

Lua API for C and C++£

¤

¥

 F

 

 

¦

 I #

E

¥

#

¥

H

 

3

£

¤

¥

£

 

¤

%

¥

¤

 

£

 

¨ ¨

 

!

§

 "

¥

 4

©

¤

 

 4

# ¤

 

¢

 

# ¢ $ I T ¤

¥ ¥

¢

 I ¤ D

¥

£

¤

!

¤

 

 "

9

£

  @

%

 I

#

¥

¤

 I

 T

B

¢

¦

H

 4

§

 

 

¤

 

 "

9

£

  @

'

 I

#

¥

¤

 I

 T

B

¢

¦

H

 4

© 

 

¤

 

 "

9

£

  @

0

 I

#

¥

¤

 I

 T

B

¢

¦

H

 4

 

 

!

¢

¥

 I ¢ ¤ ¤

 

£

 

¥

H

 I #

!

 I

# ¡

 I

 "

 !

¤

 I

 T

B

¢

¦

H

 4

£

 

 

 

¢

¥

 I

¢ ¤

§

 

8

 I

# ¢  $

£

 "

¥

¢

¦

H

 4

S  

 

S

 4

 F

 

 

 

 

28

Tables in data description

  Of course, we can also pass and manipulatetables from C.

 

So, you will often find data description like:¡

"

#

$

%

&

 '

%

(

)

 0

1 2

%

(

)

 0

3 4

5

# 6

 6 #

$

 9

3 A # #

$

B C

$

# 6 4

 0

#

 9

 6 4 '

D %

 ' (

"

E 3

 0

F

#

 2

 C G

H

 C

$

 4

 9 I I I

P

  See the exampleQ

¤

R ¢

¨

 

 

We can also generate bindings automaticallywith the tolua tool

Page 8: Scripting Programs With Lua

8/3/2019 Scripting Programs With Lua

http://slidepdf.com/reader/full/scripting-programs-with-lua 8/9

29

More fancy stuff

  But remember, Lua is a full-blownprogramming language

  Data description files can be simple, likebefore ...

  ... or they can be full-blown programs  Back to the square example: 

Let's make a circle instead 

Defining all nodes and faces by hand is nasty  So we write our description file as a program

30

Generating a circle

  See

§

¢

¨

 

¢

£

¤

¢ #

  £

 I

 "

§ C

 "

¥

¡

©

 T #

¥

 !

¡

£ !

§

C

 D ¤

¥

¢

 

 

¦ C

 4

C

 4

¢ £

¢ "

¥

¤

 

 

 

¦

¢ #

  £

 I

 "

 T #

¥

 !

 D  "

¦ C 

 4

¢ #

  £

 I

 "

 T #

¥

 !

 "

£

¤

¦ C 

 4

¡ ¢  

£

 I " ¤

 

¢ £

¢ "

¥

¤

 

¢

¢ # ¤ $

 "

¥

¡

 4

©

 T #

¥

 !

¡

£

 4

 "

¥

¡

 

 D #

¤

U

¤

 

 

 

¦

¢ #

 

£

 I

 "

 T #

¥

 !

 D  "

¦

# ¤$

 

 4

¢ #

 

£

 I

 "

 T #

¥

 !

 "

£

¤

¦

# ¤$

 

 4

 D #

¤

U

¢

# D

¡

# D

¦

 D ¤

¥

¢

 4

¡ ¢

 

£

 I

 " ¤

 

 4

¤ U ¤

 

 

¡ ¢

 

£

 I

 " ¤

 

¤

U

¤

 

¤

 

31

Data as programs, conclusion

  This is really the whole trick to defining thecomplex face model with all the deformations

 

The basic idea is:¡

Have a base model of the face at rest¡ Define areas that are affected by a particular

muscle¡ Such as eyebrows or lips¡ The closer a node is to the center of that area,the more it is affected by the deformation

32

More info

  [email protected]  http://www.lua.org  http://www.lua-users.org

Page 9: Scripting Programs With Lua

8/3/2019 Scripting Programs With Lua

http://slidepdf.com/reader/full/scripting-programs-with-lua 9/9

33

Lua vs Python – pro Python

  Python has more high-level data structures,such as lists, arrays, dictionaries, tuples

  Python has explicit syntax for classes andinheritance, whereas Lua users build theirown with metamethods

  Python has a huge standard library, whereas

for Lua, most of these are 3

rd

party addons 

Python has a larger user community

34

Lua vs Python – pro Lua

  Lua is smaller, easier to distribute, easier toembed, and easier to port to differentplatforms

  Lua is generally faster  Lua uses garbage collection, not reference-counting (so no worries about cyclic data

structures)  Lua has coroutines and is ready for multiplethreads, whereas Python uses one Big Lock

  Lua has simple lexical scoping rules and full-blown closures