From db603ef3e4c191d3ae01a6f3105b33148917a195 Mon Sep 17 00:00:00 2001 From: MusNik Date: Wed, 2 Dec 2020 19:51:27 +0300 Subject: [PATCH 1/6] Update README.md --- README.md | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/README.md b/README.md index 1315c0d..ed9ff3f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,56 @@ # DeepCopy.gml [![Donate](https://img.shields.io/badge/donate-%E2%9D%A4-blue.svg)](https://musnik.itch.io/donate-me) [![License](https://img.shields.io/github/license/KeeVeeGames/DeepCopy.gml)](#!) + + + +## Installation: +Get the latest asset package from the [releases page](../../releases). Import it into IDE. +Alternatively copy the code from corresponding scripts into your project. + +## Example: +```js +var array = [ + new Class1(), + [ [0], [1], [2] ], + { hmm : "hmm", huh : "huh", class : new Class2() } +]; + +var struct = new Class3( + new Class2(), + new Class4( + new Class4( + [0, 1, 2], + { three : 3, four : 4, five : 5 } + ) + ) +); + +var new_araay = deep_copy(array); +var new_struct = deep_copy(struct); + +// Both have identical values +show_debug_message(array); // [ { },[ [ 0 ],[ 1 ],[ 2 ] ],{ class : { a : 0, b : 1, c : 2 }, hmm : "hmm", huh : "huh" } ] +show_debug_message(new_araay); // [ { },[ [ 0 ],[ 1 ],[ 2 ] ],{ class : { a : 0, b : 1, c : 2 }, hmm : "hmm", huh : "huh" } ] +show_debug_message(struct); // { thing : { a : 0, b : 1, c : 2 }, stuff : { thing : { thing : [ 0,1,2 ], stuff : { three : 3, four : 4, five : 5 } } } } +show_debug_message(new_struct); // { thing : { a : 0, b : 1, c : 2 }, stuff : { thing : { thing : [ 0,1,2 ], stuff : { three : 3, four : 4, five : 5 } } } } + +// But aren't holding the same references +show_debug_message(array == new_araay); // false +show_debug_message(array[0] == new_araay[0]); // false +show_debug_message(array[1] == new_araay[1]); // false +show_debug_message(array[2] == new_araay[2]); // false +show_debug_message(struct == new_struct); // false +show_debug_message(struct.thing == new_struct.thing); // false +show_debug_message(struct.stuff == new_struct.stuff); // false +show_debug_message(struct.stuff.thing == new_struct.stuff.thing); // false + +// And structs are preserving its types so you can use static fields +new_araay[0].func(); // hello! +new_struct.thing.func(); // hello! +``` + +## Author: +Nikita Musatov - [MusNik / KeeVee Games](https://twitter.com/keeveegames) + +License: [MIT](https://en.wikipedia.org/wiki/MIT_License) From d3d616e3c59c08a0bcd42a421187604ec849cd9a Mon Sep 17 00:00:00 2001 From: MusNik Date: Wed, 2 Dec 2020 19:53:29 +0300 Subject: [PATCH 2/6] Update README.md --- README.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ed9ff3f..2c540e4 100644 --- a/README.md +++ b/README.md @@ -30,10 +30,14 @@ var new_araay = deep_copy(array); var new_struct = deep_copy(struct); // Both have identical values -show_debug_message(array); // [ { },[ [ 0 ],[ 1 ],[ 2 ] ],{ class : { a : 0, b : 1, c : 2 }, hmm : "hmm", huh : "huh" } ] -show_debug_message(new_araay); // [ { },[ [ 0 ],[ 1 ],[ 2 ] ],{ class : { a : 0, b : 1, c : 2 }, hmm : "hmm", huh : "huh" } ] -show_debug_message(struct); // { thing : { a : 0, b : 1, c : 2 }, stuff : { thing : { thing : [ 0,1,2 ], stuff : { three : 3, four : 4, five : 5 } } } } -show_debug_message(new_struct); // { thing : { a : 0, b : 1, c : 2 }, stuff : { thing : { thing : [ 0,1,2 ], stuff : { three : 3, four : 4, five : 5 } } } } +show_debug_message(array); +show_debug_message(new_araay); +// [ { },[ [ 0 ],[ 1 ],[ 2 ] ],{ class : { a : 0, b : 1, c : 2 }, hmm : "hmm", huh : "huh" } ] +// [ { },[ [ 0 ],[ 1 ],[ 2 ] ],{ class : { a : 0, b : 1, c : 2 }, hmm : "hmm", huh : "huh" } ] +show_debug_message(struct); +show_debug_message(new_struct); +// { thing : { a : 0, b : 1, c : 2 }, stuff : { thing : { thing : [ 0,1,2 ], stuff : { three : 3, four : 4, five : 5 } } } } +// { thing : { a : 0, b : 1, c : 2 }, stuff : { thing : { thing : [ 0,1,2 ], stuff : { three : 3, four : 4, five : 5 } } } } // But aren't holding the same references show_debug_message(array == new_araay); // false From 5c39fd515c1bd050b27207de6536a5506bf07081 Mon Sep 17 00:00:00 2001 From: MusNik Date: Wed, 2 Dec 2020 19:54:15 +0300 Subject: [PATCH 3/6] Update README.md --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index 2c540e4..ee8efd0 100644 --- a/README.md +++ b/README.md @@ -32,12 +32,8 @@ var new_struct = deep_copy(struct); // Both have identical values show_debug_message(array); show_debug_message(new_araay); -// [ { },[ [ 0 ],[ 1 ],[ 2 ] ],{ class : { a : 0, b : 1, c : 2 }, hmm : "hmm", huh : "huh" } ] -// [ { },[ [ 0 ],[ 1 ],[ 2 ] ],{ class : { a : 0, b : 1, c : 2 }, hmm : "hmm", huh : "huh" } ] show_debug_message(struct); show_debug_message(new_struct); -// { thing : { a : 0, b : 1, c : 2 }, stuff : { thing : { thing : [ 0,1,2 ], stuff : { three : 3, four : 4, five : 5 } } } } -// { thing : { a : 0, b : 1, c : 2 }, stuff : { thing : { thing : [ 0,1,2 ], stuff : { three : 3, four : 4, five : 5 } } } } // But aren't holding the same references show_debug_message(array == new_araay); // false From 65db462a0c40bba2cd06a21a1476a12b52634f83 Mon Sep 17 00:00:00 2001 From: MusNik Date: Wed, 2 Dec 2020 23:00:40 +0300 Subject: [PATCH 4/6] Update README.md --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index ee8efd0..dbd7a0e 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,17 @@ [![Donate](https://img.shields.io/badge/donate-%E2%9D%A4-blue.svg)](https://musnik.itch.io/donate-me) [![License](https://img.shields.io/github/license/KeeVeeGames/DeepCopy.gml)](#!) +This simple script lets you recursively deep copy nested arrays, structs and "class" instances. The syntax is pretty straightforward: `deep_copy(thing)` function will return a new instance of "thing" (new array, new anonymous struct or new "constructed" struct) with the same data and the respectful copies of all nested things. +The method behind this is coming from my [finding](https://twitter.com/KeeVeeGames/status/1294268813807099905) of preserving struct's type / constructor. I also realized that I may create my own [serialization protocol](https://twitter.com/KeeVeeGames/status/1294988076553510912), but that will be released in future. + +**Note:** +* Copying ds'es won't deep copy them but pass them through as real numbers. +* Be mindful about circular references, they will make the algorithm stuck in an infinite loop. +* There is no way to mark a field to not be "copiable". +* Copying function references is also not working: it will shallow copy the reference to the original method, as I am not aware of any way to deep copy functions. + + — you may consider using DeepCopy+ and Protoclasses as a part of GMProto framework, that are solving the problem of deep cloning ds'es, preventing circular references, adding the way to mark fields "unserializable" and having better type checking (not currently released, [stay tuned](https://twitter.com/KeeVeeGames)). ## Installation: Get the latest asset package from the [releases page](../../releases). Import it into IDE. From 5ee97d69504209afcfc94922516525961fc2fc42 Mon Sep 17 00:00:00 2001 From: MusNik Date: Wed, 2 Dec 2020 23:04:06 +0300 Subject: [PATCH 5/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dbd7a0e..b42abba 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ The method behind this is coming from my [finding](https://twitter.com/KeeVeeGam * Copying ds'es won't deep copy them but pass them through as real numbers. * Be mindful about circular references, they will make the algorithm stuck in an infinite loop. * There is no way to mark a field to not be "copiable". -* Copying function references is also not working: it will shallow copy the reference to the original method, as I am not aware of any way to deep copy functions. +* Copying function references will shallow copy the reference to the original method, as I am not aware of any way to deep copy functions. — you may consider using DeepCopy+ and Protoclasses as a part of GMProto framework, that are solving the problem of deep cloning ds'es, preventing circular references, adding the way to mark fields "unserializable" and having better type checking (not currently released, [stay tuned](https://twitter.com/KeeVeeGames)). From a36de3f0b38c8c46440c4ef8b5a18f317126d183 Mon Sep 17 00:00:00 2001 From: MusNik Date: Wed, 2 Dec 2020 23:04:34 +0300 Subject: [PATCH 6/6] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b42abba..254eae4 100644 --- a/README.md +++ b/README.md @@ -56,8 +56,8 @@ show_debug_message(struct.stuff == new_struct.stuff); // f show_debug_message(struct.stuff.thing == new_struct.stuff.thing); // false // And structs are preserving its types so you can use static fields -new_araay[0].func(); // hello! -new_struct.thing.func(); // hello! +new_araay[0].func(); +new_struct.thing.func(); ``` ## Author: