CopyTable what is difference between shallow and deep copy?

Find quick help here to get you started with Hollywood
Post Reply
Bugala
Posts: 693
Joined: Sun Feb 14, 2010 8:11 pm

CopyTable what is difference between shallow and deep copy?

Post by Bugala » Fri Jun 22, 2018 11:39 pm

I was using CopyTable command in my program, and to my surprise it tells me "stack overflow", when i use parameter true for the shallow then it works fine. What is that Deep copy doing that it gets stack to overflow when table isnt that big anyway?

Or does this perhaps have to do with my other problem.

I am having problem with my program and I am in middle of a gamejam, hence i cant really start making any large scale approaches to problem solving with only about 3 days left any more.

I think my problem is following:

I am trying to use copytable to make another copy of the level. Thing is, i realised that when i tried to reset the level (if player used quit button in middle), it didnt put stuff back to original places, but kept them in place. I realised that this was because i had been altering the original table. That while i had put enemyx=3 at beginning, in middle of level i used to change that exact enemyx to new one. Hence when it tried to generate the same level again using that same table, that table had been altered.

To get around this problem without doing a lot of recoding, i tried to make copy of the original table to be used in level. However, I believe i know what the problem is. I think I am sending reference that i try to copy.

Code is basically following:

Code: Select all

function myfunc(T_mytable)
T_mynewcopy = CopyTable(T_mytable)
Looks right, except T_mytable that comes to function, is actually a reference to a table, not a table itself.

Is there a way to make a copy of this table to which i am sending a reference to?

SamuraiCrow
Posts: 173
Joined: Fri May 15, 2015 5:15 pm
Location: Ft. Collins, Colorado USA

Re: CopyTable what is difference between shallow and deep copy?

Post by SamuraiCrow » Sat Jun 23, 2018 1:03 pm

A deep copy copies every single element in the table to the new table including subtables. A shallow copy copies only the top level but duplicates the references of the top level instead of the elements pointed to by those references.

If stack usage and memory is an issue, you might consider using added disk space to save the tables using ReadTable and WriteTable to serialize and deserialize each level as files. If disk space isn't an option, you can still embed files using preprocessor macros once they are created.

Bugala
Posts: 693
Joined: Sun Feb 14, 2010 8:11 pm

Re: CopyTable what is difference between shallow and deep copy?

Post by Bugala » Sat Jun 23, 2018 10:31 pm

darn good idea that read and write table idea, I think that might be quick solution enough for this project.

Post Reply