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

Slides:



Advertisements
Similar presentations
Lua By: Michael Coffman Peter Sussman. History Lua means ‘moon’ in Portuguese First appeared in 1993 Created by Roberto Ierusalimschy, Luiz Henrique de.
Advertisements

The Web Warrior Guide to Web Design Technologies
Objectives Using functions to organize PHP code
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
CS 898N – Advanced World Wide Web Technologies Lecture 14: JavaScript Chin-Chih Chang
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
C++ fundamentals.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
From C++ to C#. Web programming The course is on web programming using ASP.Net and C# The course is on web programming using ASP.Net and C# ASP.Net is.
CSE 380 – Computer Game Programming Scripting and Lua Lua.
References: 1. “Programming in LUA, 2 nd ed.”, Roberto Lerusalmschy Chapters “Lua Reference Manual” (included in Lua installation or online)
PHP Overview CS PHP PHP = PHP: Hypertext Preprocessor Server-side scripting language that may be embedded into HTML One goal is to get PHP files.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
CSCI 130 Scope of Variables Chapter 6. What is scope Parts of program which can access a variable –accessibility –visibility How long variable takes up.
©Xiaoying Gao, Peter Andreae First Java Program COMP 102 #2 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
First Java Program COMP 102 #2 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Lua: The Programming Language. Some Things that Need to be Said Because of increasing demand for customizable applications, the trend nowadays is to split.
Games Development 2 Scripting for Games CO3301 Week 5.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Basic Semantics Associating meaning with language entities.
Week 1 Algorithmization and Programming Languages.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Python – May 11 Briefing Course overview Introduction to the language Lab.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Built-in Data Structures in Python An Introduction.
ALBERT WAVERING BOBBY SENG. Week 4: JavaScript  Quiz  Announcements/questions.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
RUBY by Ryan Chase.
More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Web Database Programming Using PHP
Week 3-4 Control flow (review) Function definition Program Structures
Introduction Python is an interpreted, object-oriented and high-level programming language, which is different from a compiled one like C/C++/Java. Its.
Web Database Programming Using PHP
بسم الله الرحمن الرحیم.
Chapter 2 - Introduction to C Programming
Web DB Programming: PHP
Catharine West Bo Aye Saljin LaRocca references
Python Primer 1: Types and Operators
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Compiler vs linker The compiler translates one .c file into a .o file
The Lua Chunk Vault, an enhancement to epics base
SPL – PS1 Introduction to C++.
Storage Classes.
Presentation transcript:

Games Development 2 Lua Scripting CO3301 Week 6

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

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

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++)

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

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

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)

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)

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

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 = 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

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,"

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

Libraries Several built in libraries: -- Mathematics print(math.cos( math.pi / 3 )) --Output 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

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