Game Development Reference
In-Depth Information
decrease values. While there are many reasons Lua doesn't have that functionality, a simple one
is that Lua was written to be easy to use. However, this type of functionality is not very difficult to
achieve in Lua; it is as simple as this:
var=var+1
or
var=var - 1
If this is critical, the best part about Lua is that you can create your own functions that provide you
that secure feeling of working with the languages of your choice by integrating their syntax.
There are quite a few limitations on how much you can modify Lua via code. We shall attempt to
create inc and dec functions that will increment or decrease the value as per the value passed to
the functions. We shall create two functions, inc and dec . These functions will take the variable and
the value that we want to increase or decrease the variable value by. The function shall return the
increased or decreased value.
function inc(theVar, byVal)
local byVal=byVal or 1
return theVar+byVal
end
function dec(theVar, byVal)
local byVal=byVal or 1
return theVar - byVal
end
Now you can just call the inc or dec as follows:
local theValue=20
print("The Value=", theValue)
theValue=inc(theValue, 5)
-- Equivalent of theValue=theValue+5
print("The Value=", theValue)
theValue=dec(theValue)
-- Equivalent of theValue=theValue+5
print("The Value=", theValue)
The function dec does not get a value for byVal and therefore the value of byVal is nil . In the
function we first declare a local variable and assign it the value of byVal , and since it is nil , we
assign it the value of 1. Then we return the value of theVar less byVal .
Introducing the Point
We need to hold two-dimensional coordinates; namely, the x- and y-coordinates. In most languages,
we use two variables: x and y . In C and other such languages, we use a structure that has the
members x and y . With Lua we can use a table to hold this structure. Let us call this structure a point .
local myPoint={x=0,y=0}
myPoint.x=160
 
Search WWH ::




Custom Search