Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help,
read this .
Positive07
Party member
Posts: 1014 Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina
Post
by Positive07 » Tue Oct 14, 2014 3:24 am
Yeah that is exactly what deepCopy does, your simpler shallow copy does the following
Code: Select all
t = {
a = {
b = "hello"
}
}
for k,v in pairs(t) do
x[k] = t[k]
end
x.a.b = "bye"
print(t.a.b) --"bye"
print(t==x) --"false"
print(t.a == x.a) --"true"
while deep copy
Code: Select all
t = {
a = {
b = "hello"
}
}
x = deepCopy(t)
x.a.b = "bye"
print(t.a.b) --"hello"
print(t == x) --"false"
print (t.a == x.a) --"false"
I think that Robins code breaks when you do this though:
Since it can't handle table loops, if you wanted to do this you would first have to detect the loops with parsing... and that is really hard... maybe there is a library to do this
for i, person
in ipairs (everybody)
do
[tab] if not person.obey
then person:setObey(
true )
end
end
love.system.openURL(
github.com/pablomayobre )
khamarr3524
Prole
Posts: 41 Joined: Thu Sep 05, 2013 8:48 pm
Post
by khamarr3524 » Tue Oct 14, 2014 4:42 am
Right well, it should suffice for my usage. The data files for Units are static and formatted by hand. Nothing strange should be going on in there. I will simply use a recursion method. I'll do something like
Code: Select all
function copyTable(t)
local tbl = {}
for k, v in pairs(t) do
if type(v) == "table" then
tbl[k] = copyTable(v)
end
tbl[k] = v
end
return tbl
end
I'm assuming this would work just fine?
Positive07
Party member
Posts: 1014 Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina
Post
by Positive07 » Tue Oct 14, 2014 5:02 am
Yeah indeed, it is really similar to deep copy
for i, person
in ipairs (everybody)
do
[tab] if not person.obey
then person:setObey(
true )
end
end
love.system.openURL(
github.com/pablomayobre )
ivan
Party member
Posts: 1918 Joined: Fri Mar 07, 2008 1:39 pm
Contact:
Post
by ivan » Tue Oct 14, 2014 5:35 am
Minor bug:
Code: Select all
if type(v) == "table" then
tbl[k] = copyTable(v)
end
tbl[k] = v -- overwrites the copied table reference
Should probably be
Code: Select all
if type(v) == "table" then
tbl[k] = copyTable(v)
else
tbl[k] = v
end
Robin
The Omniscient
Posts: 6506 Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:
Post
by Robin » Tue Oct 14, 2014 10:36 am
Positive07 wrote:
I think that Robins code breaks when you do this though:
Since it can't handle table loops,
Actually , that's exactly the sort of thing my code fixed, compared to some more naive attempts at deep copying.
(Also, that should really be something like
t = {}; t.a = t , but yeah.)
Users browsing this forum: Ahrefs [Bot] , Google [Bot] and 15 guests