0

Learning Lua Scripting- Part 5 – Math and Math Library

Posted by Dr. Burton on March 15, 2017 in Amazon Lumberyard, Corona, Lua, Lumberyard, Tutorials |

In this Learning Lua Scripting tutorial we will examine how to use the Lua Math Library.

Lua has the basic math operations that you would expect to find in a modern scripting language.
^ – exponential
* – multiplication
/ – division
% – modulus or modulo
+ – addition
– – subtraction or unary

As of version 5.3 of Lua, numbers are stored internally as either integers or double (64 bit) by default.
Prior to 5.3, all numbers were stored as doubles.

Lua follows the standard order of precedence for operations: ^, not, #, unary, *, /, %, +, –

Library:
The math library can be accessed with the math keyword without special loading:

local myPi = math.pi
print ( myPi )
									


Results in 3.1415926535…

The math library has all the functions that you would expect including sin, cos, random, randomseed, etc.

If you wish to generate a random number, be sure to randomize the number generator:

math.randomseed( os.time( ) )
myNumber = math.random( 1, 10 )
print ( myNumber )
									

Resources

If you would like print resources, there are several books on Lua available.

Programming in Lua by Roberto Ierusalimschy, one of the lead architects of Lua.  Great technical intro to the language

Lua 5.2 Reference Manual also by Roberto Ierusalimschy, is, as the name implies, a less expensive reference manual.  Useful for the experienced coder who just needs to look up some of the details of the language.

Lua Programming Gems by L. de Figueiredo, W. Celes, and R. Ierusalimschy is an older (2008) collection of code snippets that can be useful.


Editor –

We used the Zerobrane editor in all of our video demonstrations.

Our books:
We have several books on Corona and Amazon Lumberyard (both of which use Lua as their scripting language):
Learning Mobile Application & Game Development with Corona – Learn to program in Lua and how to make mobile apps! eTextbook for those who are new to programming.

Beginning Mobile App Development with Corona – Introduces mobile application development for those who already know how to program.

Game Design Fundamentals with Amazon Lumberyard – For those who are new 3D Game development, this eTextbook introduces how to make a game using Blender, GIMP, and Amazon Lumberyard.

The idea of writing a textbook on the Lua Scripting language has been floated to me.  While I greatly value Dr. Ierusalimschy, our styles of instruction are very different.  Leave me a comment if you would like to see a Lua Scripting textbook.

 

Next Lesson: Part 6 – Functions

Tags: ,

0

Learning Lua Scripting- Part 4: Working with Strings

Posted by Dr. Burton on March 14, 2017 in Amazon Lumberyard, Corona, Lua, Tutorials |

In this tutorial, part 4 of our series on learning Lua scripting, we will look at some of the tools available for working with Strings in Lua.

Tutorial:

Concatenation in an important concept in all programming languages.  In Lua, concatenation is accomplished by using two periods in a series or ..

local myString = "Hello"
print (#myString)  -- the # (hashtag or pound sign) returns the number of elements in a table or string. i.e. how many characters are in the string

local myOtherString = " World"
print (myString .. myOtherString)
									

There are many escape sequences available in Lua including newline:

local myNewLine = "This is a string ntwith a "newline""
print (myNewLine)
									

The example adds a new line (\n), a tab at the beginning of the newline (\t), and quotes around the word “newline”.  One caveat to using escape sequences; they do not require any spaces around them.

One last tip for creating strings in Lua.  If you want to create a multi-line string, you can use [[ ]] to encapsulate the string.

local myThirdString = [[ Multi-line
string
that 
contains 
newline ]]
print (myThirdString)
									

 

Resources

If you would like print resources, there are several books on Lua available.

Programming in Lua by Roberto Ierusalimschy, one of the lead architects of Lua.  Great technical intro to the language.



Lua 5.2 Reference Manual
also by Roberto Ierusalimschy, is, as the name implies, a less expensive reference manual.  Useful for the experienced coder who just needs to look up some of the details of
the language.

Lua Programming Gems by L. de Figueiredo, W. Celes, and R. Ierusalimschy is an older (2008) collection of code snippets that can be useful.


Editor –

We used the Zerobrane editor in all of our video demonstrations.

Our books:
We have several books on Corona and Amazon Lumberyard (both of which use Lua as their scripting language):
Learning Mobile Application & Game Development with Corona – Learn to program in Lua and how to make mobile apps! eTextbook for those who are new to programming.

Beginning Mobile App Development with Corona – Introduces mobile application development for those who already know how to program.

Game Design Fundamentals with Amazon Lumberyard – For those who are new 3D Game development, this eTextbook introduces how to make a game using Blender, GIMP, and Amazon Lumberyard.

The idea of writing a textbook on the Lua Scripting language has been floated to me.  While I greatly value Dr. Ierusalimschy, our styles of instruction are very different.  Leave me a comment if you would like to see a Lua Scripting textbook.

 

Next Lesson: Part 5 – Math and Math Library

Tags: , ,

0

Learning Lua Scripting – Part 3: Variables

Posted by Dr. Burton on March 14, 2017 in Amazon Lumberyard, Corona, Lua, Lumberyard, Tutorials |

Part 3 of our tutorial series on the Lua scripting language.  In this tutorial we explore the variable system in Lua.

Lua uses an untyped variable declaration system.  That means that you do not need to decide on what type a variable is, just declare it with the keyword local.

Lua variables can store nil, number (floating point), strings, boolean, tables, functions, userdata, and threads.

By default, any new variable has the value of nil until given a value.

That doesn’t mean that Lua doesn’t type its variables! It does.  If you use the command type, you can see what variable type is being used in the storage:

local myVariable = 10
print ( type( myVariable ) )
									

The print command in this code example will result in “number” being displayed in the console.  All numbers are stored as double precision floating point variables.

Boolean can be a little tricky for evaluation.  If the variable has no value (or nil), it will evaluate (ie. when using if..then or while structures) to FALSE.

We will explore tables in depth later, but for now, you should know that tables are declared with the curly brackets {}:

local myTable = {"a string", 12, 42, false}
print myTable[1]
									

To work with the contents of a table, you can reference the object based upon the index.  Note that table indexes start counting at 1, not 0!
Thus the above code example would output the first object in the table: “a string”

 

Resources

If you would like print resources, there are several books on Lua available.

Programming in Lua by Roberto Ierusalimschy, one of the lead architects of Lua.  Great technical intro to the language.



Lua 5.2 Reference Manual
also by Roberto Ierusalimschy, is, as the name implies, a less expensive reference manual.  Useful for the experienced coder who just needs to look up some of the details of
the language.

Lua Programming Gems by L. de Figueiredo, W. Celes, and R. Ierusalimschy is an older (2008) collection of code snippets that can be useful.


Editor –

We used the Zerobrane editor in all of our video demonstrations.

Our books:
We have several books on Corona and Amazon Lumberyard (both of which use Lua as their scripting language):
Learning Mobile Application & Game Development with Corona – Learn to program in Lua and how to make mobile apps! eTextbook for those who are new to programming.

Beginning Mobile App Development with Corona – Introduces mobile application development for those who already know how to program.

Game Design Fundamentals with Amazon Lumberyard – For those who are new 3D Game development, this eTextbook introduces how to make a game using Blender, GIMP, and Amazon Lumberyard.

The idea of writing a textbook on the Lua Scripting language has been floated to me.  While I greatly value Dr. Ierusalimschy, our styles of instruction are very different.  Leave me a comment if you would like to see a Lua Scripting textbook.

 

Next Lesson: Part 4 – Working with Strings

Tags: , , , ,

0

Learning Lua Scripting – Part 2: Print and Comments

Posted by Dr. Burton on March 14, 2017 in Amazon Lumberyard, Corona, Lua, Lumberyard, Tutorials |

In this Learning Lua Scripting tutorial, we will cover the use of print for displaying information to the console and how to comment a line.

Tutorial

While I go into a much longer explanation in the video, here is the least you need to know:
Using print, you can easily see where you are at in your program and the value of variables:

print ("Made it to line 20 in game.lua")
print (varable1, variable2, "Values of Variable1 and Variable2")
									

As all good programmers know, comments are an essential to being able to keep track of where you are at on a large program or just to remember why you did something a specific way when you resume coding after a long weekend:

-- Inline comment
local variable1 = "Example of code"   -- Everything after 2 dashes is a comment

--[[
This is an example of a block comment
]]
									

Resources

If you would like print resources, there are several books on Lua available.

Programming in Lua by Roberto Ierusalimschy, one of the lead architects of Lua.  Great technical intro to the language.



Lua 5.2 Reference Manual
also by Roberto Ierusalimschy, is, as the name implies, a less expensive reference manual.  Useful for the experienced coder who just needs to look up some of the details of
the language.

Lua Programming Gems by L. de Figueiredo, W. Celes, and R. Ierusalimschy is an older (2008) collection of code snippets that can be useful.


Editor –

We used the Zerobrane editor in all of our video demonstrations.

Our books:
We have several books on Corona and Amazon Lumberyard (both of which use Lua as their scripting language):
Learning Mobile Application & Game Development with Corona – Learn to program in Lua and how to make mobile apps! eTextbook for those who are new to programming.

Beginning Mobile App Development with Corona – Introduces mobile application development for those who already know how to program.

Game Design Fundamentals with Amazon Lumberyard – For those who are new 3D Game development, this eTextbook introduces how to make a game using Blender, GIMP, and Amazon Lumberyard.

The idea of writing a textbook on the Lua Scripting language has been floated to me.  While I greatly value Dr. Ierusalimschy, our styles of instruction are very different.  Leave me a comment if you would like to see a Lua Scripting textbook.

 

Next Lesson: Part 3 – Variables in Lua

0

Learning Lua Scripting – Part 1

Posted by Dr. Burton on March 14, 2017 in Amazon Lumberyard, Corona, Lua |

Lua has become an important player in the world of scripting. It is the scripting language used in Amazon Lumberyard, Corona SDK, World of Warcraft, Adobe Lightroom, Love2D and many, many other systems.

Why Lua?

Lua has become so popular because it is very light weight to include as a scripting language (37K!), it is easy to learn (usually less than 20 minutes if you know any other programming or scripting language), yet powerful enough to create complex applications.

Tutorial

Part 1 of our tutorial series covers how to setup your system to do Lua development (don’t worry, it’s short and painless) and provides a little history of the Lua scripting language:

Resources

If you would like print resources, there are several books on Lua available.

Programming in Lua by Roberto Ierusalimschy, one of the lead architects of Lua.  Great technical intro to the language.



Lua 5.2 Reference Manual
also by Roberto Ierusalimschy, is, as the name implies, a less expensive reference manual.  Useful for the experienced coder who just needs to look up some of the details of
the language.

Lua Programming Gems by L. de Figueiredo, W. Celes, and R. Ierusalimschy is an older (2008) collection of code snippets that can be useful.


Editor –

We used the Zerobrane editor in all of our video demonstrations.

Our books:
We have several books on Corona and Amazon Lumberyard (both of which use Lua as their scripting language):
Learning Mobile Application & Game Development with Corona – Learn to program in Lua and how to make mobile apps! eTextbook for those who are new to programming.

Beginning Mobile App Development with Corona – Introduces mobile application development for those who already know how to program.

Game Design Fundamentals with Amazon Lumberyard – For those who are new 3D Game development, this eTextbook introduces how to make a game using Blender, GIMP, and Amazon Lumberyard.

The idea of writing a textbook on the Lua Scripting language has been floated to me.  While I greatly value Dr. Ierusalimschy, our styles of instruction are very different.  Leave me a comment if you would like to see a Lua Scripting textbook.

 

Next Lesson:

Part 2: Print and Comments in Lua

 

Tags: , , ,

Copyright © 2010-2017 Burtons Media Group All rights reserved.
This site is using the Desk Mess Mirrored theme, v2.5, from BuyNowShop.com.