Game Development Reference
In-Depth Information
'
Lua provides three logical operators: and , or , and not . They are analogous to C++
s
logical and (&&), or (jj) and not (!) operators and generally behave like you would
expect them to.
One very handy operator Lua provides is the string concatenation operator (..). This
concatenates two strings and returns the results, like so:
x=
the brown
..
dog went home.
print(x) -- > prints
the brown dog went home.
-- It works on numbers as well
y=10
print(x .. y) -- > prints “the brown dog went home. 10”
What
s Next?
The goal of this section was to familiarize you with basic Lua syntax. As I said pre-
viously, this is nowhere near the full breadth of the language, and I strongly urge
you to check out some resources online. I
'
ve provided a few helpful links at the
end of this chapter that should help. If there were some things you didn
'
tquite
understand, now would be a good time to go reread those sections or check out
online samples. The rest of this chapter talks about some pretty advanced stuff and
from this point on, I ' ll assume you are relatively comfortable with the basics of Lua
programming.
'
Object-Oriented Programming in Lua
Lua doesn
s
possible to plug it in using tables. Tables give you a way to group data together and
map chunks of data to names (string keys). Since functions are really just another
form of data, you can easily group them all together to create encapsulation. Let
'
t have any direct support for object-oriented programming, although it
'
'
s
start by attempting to make a vector object in Lua.
-- Note how the table is defined across multiple lines, just like you might do
-- for a C array or parameter list. Lua doesn
'
t care. This is much more
-- readable for our purposes.
vector =
{
-- This is the vector data
x=0,y=0,z=0,
-- Here
s the first attempt at a Length() function. Note the use of the
-- math table for math.sqrt(). This works exactly like the table functions
-- you saw above.
'
 
 
 
 
Search WWH ::




Custom Search