|
1 | 1 | # DeepCopy.gml |
2 | 2 |
|
3 | 3 | [](https://musnik.itch.io/donate-me) [](#!) |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +**Note:** |
| 10 | +* Copying ds'es won't deep copy them but pass them through as real numbers.<sup>✶</sup> |
| 11 | +* Be mindful about circular references, they will make the algorithm stuck in an infinite loop.<sup>✶</sup> |
| 12 | +* There is no way to mark a field to not be "copiable".<sup>✶</sup> |
| 13 | +* Copying function references will shallow copy the reference to the original method, as I am not aware of any way to deep copy functions. |
| 14 | + |
| 15 | +<sup>✶</sup> — 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)). |
| 16 | + |
| 17 | +## Installation: |
| 18 | +Get the latest asset package from the [releases page](../../releases). Import it into IDE. |
| 19 | +Alternatively copy the code from corresponding scripts into your project. |
| 20 | + |
| 21 | +## Example: |
| 22 | +```js |
| 23 | +var array = [ |
| 24 | + new Class1(), |
| 25 | + [ [0], [1], [2] ], |
| 26 | + { hmm : "hmm", huh : "huh", class : new Class2() } |
| 27 | +]; |
| 28 | + |
| 29 | +var struct = new Class3( |
| 30 | + new Class2(), |
| 31 | + new Class4( |
| 32 | + new Class4( |
| 33 | + [0, 1, 2], |
| 34 | + { three : 3, four : 4, five : 5 } |
| 35 | + ) |
| 36 | + ) |
| 37 | +); |
| 38 | + |
| 39 | +var new_araay = deep_copy(array); |
| 40 | +var new_struct = deep_copy(struct); |
| 41 | + |
| 42 | +// Both have identical values |
| 43 | +show_debug_message(array); |
| 44 | +show_debug_message(new_araay); |
| 45 | +show_debug_message(struct); |
| 46 | +show_debug_message(new_struct); |
| 47 | + |
| 48 | +// But aren't holding the same references |
| 49 | +show_debug_message(array == new_araay); // false |
| 50 | +show_debug_message(array[0] == new_araay[0]); // false |
| 51 | +show_debug_message(array[1] == new_araay[1]); // false |
| 52 | +show_debug_message(array[2] == new_araay[2]); // false |
| 53 | +show_debug_message(struct == new_struct); // false |
| 54 | +show_debug_message(struct.thing == new_struct.thing); // false |
| 55 | +show_debug_message(struct.stuff == new_struct.stuff); // false |
| 56 | +show_debug_message(struct.stuff.thing == new_struct.stuff.thing); // false |
| 57 | + |
| 58 | +// And structs are preserving its types so you can use static fields |
| 59 | +new_araay[0].func(); |
| 60 | +new_struct.thing.func(); |
| 61 | +``` |
| 62 | + |
| 63 | +## Author: |
| 64 | +Nikita Musatov - [MusNik / KeeVee Games](https://twitter.com/keeveegames) |
| 65 | + |
| 66 | +License: [MIT](https://en.wikipedia.org/wiki/MIT_License) |
0 commit comments