Game Development Reference
In-Depth Information
c3 = coroutine.create(function()
for i=10,1,-1 do
print( "function 3: ", i)
end
end)
while true do
co1 = coroutine.resume(c1)
co2 = coroutine.resume(c2)
co3 = coroutine.resume(c3)
if coroutine.status(c1)=="dead" and coroutine.status(c2)=="dead" and
coroutine.status(c3)=="dead" then break end
end
t and then assign b as t , we do not get a
t in b ; instead, b would point to the same table as t .
In the case that we need to copy the table, not just provide a pointer to the same memory address,
we need to do what is called a deep copy . Here's an example:
function deepcopy(t)
if type(t) ~= "table" then return t end
local res = {}
local mt = getmetatable(t)
for k,v in pairs(t) do
if type(v)=="table" then
v = deepcopy(v)
end
res[k] = v
end
setmetatable(res,mt)
return res
end
This code first creates a new blank table object, res . Next, it saves the metatable signature, which is
what identifies a table to be of a particular type. When this metatable is modified, the object may fail
to be recognized as a particular type. Next, it copies all the data into the new table, and if the type of
an element is another table, it recursively goes through every element and subtable present in that
table, and when done, saves that data to corresponding new table data elements. Then it sets the
metatable for this new table object and returns it a copy of the table object.
 
Search WWH ::




Custom Search