Game Development Reference
In-Depth Information
rawset(tbl, name, function() end)
end
return rawget(tbl,name)
end
})
what(was, this)
Now we are able to create as many relationships as we want—the preceding code will check if the
words begin with uppercase or lowercase letters; if uppercase, then it will create a function, and
if not, then it will simply create an empty table. However, despite all of this, we still need to create
relationships between the two elements.
setmetatable(_G, {__index =
function(tbl, name)
if name:match("^[A-Z]") then
rawset(tbl, name, {} )
else -- create a rule here
local rule = create_rule(name)
rawset(tbl, name, rule)
end
return rawget(tbl,name)
end
})
function create_rule(name)
return function(a,b)
a[name .. "_of"]=b
b[name]=a
end
What this little tweak does is create two link keys: one that has an _of suffixed to the end of the
relationship we are setting.
If we are unable to query the relationships that we are attempting to create, then the code will return an
error. So, we add a little bit of checking and see if we have a prefix of is_ to query the relationship.
function make_predicate(name)
local rule = name:match("^is_(.*)")
return function(a,b)
return b[rule]==a
end
end
We also add a little change to our metamethod:
setmetatable(_G, {__index =
function(tbl, name)
if name:match("^[A-Z]") then
rawset(tbl, name, {} )
Search WWH ::




Custom Search