Game Development Reference
In-Depth Information
y = param4
position = param5
end
end
This can help create very flexible parsing methods. The idea is to check the type of parameters to
determine the parameters passed. While the preceding example was somewhat complex, there are
simpler examples. For instance, take the following function, in which the first parameter is optional:
parseParams( [parent, ] theMessage, theTime )
We can easily parse this as follows:
function parseParams( param1, param2, param3 )
local lstPassed = {param1, param2, param3}
local parent = nil
if type(lstPassed[1]) == "table" then
parent = lstPassed[1]
table.remove(lstPassed, 1)
end
theMessage = lstPassed[1]
theTime = lstPassed[2]
end
In this function, if the first parameter is the parent, we remove it from the table, and the rest of the
function can access the parameters as required.
Making Read-Only Tables
There may be times when you want portions of your table to be read-only and other portions
modifiable. This is all fine till the numerous if...then statements start to plague your code. The
metatable modifications make for a lot of interesting hacks and tricks.
function readonlyTable(theTable)
return setmetatable({}, {
__index = table,
__newindex = function (table, key, value)
--Do not update or create the key with the value
end,
__metatable = false
})
end
If you want to keep a portion of your table unlocked, you can keep a table -type member that is not
locked via the readonlyTable function, and you can add data to this as follows:
local myRO = readonlyTable{
name = "Jayant",
device = "iOS",
 
Search WWH ::




Custom Search