Game Development Reference
In-Depth Information
local theMixedTable = {}
for i = 1, 5 do
theMixedTable[i] = "item ".. i
end
theMixedTable["name"] = "mixedTable"
theMixedTable["subject"] = "coding"
print(theMixedTable.name, theMixedTable[3])
Functions: An Advanced Look
In the previous chapter we had a look at functions and how Lua sees them. Lua objects and
type = "furry",
name = "Furry_1",
description = "Fluffy furry teddybear",
display = function ()
print(description)
myObj.show = function()
print("Now Showing...")
end
To call the function, you use the following:
myObj.display()
myObj.show()
Tables As Objects
As you've seen, tables can be used as objects. In this next example, we'll create an object called
myObj that has properties of type, name, description, and so on. In this example, you'll create a game
that spawns a series of animals, each with different strengths and different functions for movement.
Since some of the functions might overlap, in order to avoid writing the same code over and over
again, you'll create a new object based on the parent object, such that all of the functions are
available to this object. (We shall create such objects later in the topic.) So, when this object is
passed, you can simply invoke the move function that is common to the animals, but might be
specific to each of the animals depending on its type.
 
Search WWH ::




Custom Search