Game Development Reference
In-Depth Information
elseif name:match("^is_") then
local pred = make_predicate(name)
rawset(tbl, name, pred)
else --create a rule here
local rule = create_rule(name)
rawset(tbl, name, rule)
end
return rawget(tbl,name)
end
})
This could be daunting, especially given that this is actually quite advanced-level manipulation of
metatables. However, it also demonstrates how metatables can be used to change the way the app
works with Lua.
But What's the Object?
In the preceding exercise, we had a bit of fun with metatables and created our own metafunctions to
manage the objects. Depending on how you use them, metatables can be useful in your apps. Next,
I'll discuss another thing that is most certainly useful in your apps: working with objects. Before we
embark on making objects, however, let's look at what objects are and how they are managed in Lua.
Here are some important things to remember about objects:
All Lua objects are tables.
All objects that have functions have them as their member functions.
Objects can have a custom metatable.
Using these few bits of information, let's create an object:
vehicle = {}
function vehicle:isVehicle()
print("Yes!")
end
 
Search WWH ::




Custom Search