GithubHelp home page GithubHelp logo

keeveegames / deepcopy.gml Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 1.0 45 KB

Deep clone GameMaker class instances / constructed structs, anonymous structs and arrays nested in any order!

License: MIT License

Game Maker Language 100.00%
gamemaker gml gms gms2 gamemaker-studio gamemaker-studio-2 deepcopy deep-copy deepclone deep-clone

deepcopy.gml's Introduction

DeepCopy.gml

Donate License

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.

There are two version of the script, post-2023.1 users should use the version from the main 'DeepCopy' subfolder. The principle of working here is updated and based on a Static Struct functionality intriduced in 2023.1 version. The 'Legacy' version is for the pre-2023.1 users (mainly 2.3 version) and is based on the method coming from my finding of preserving struct's type / constructor. The legacy version will not be updated in the 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 will shallow copy the reference to the original method, as deep copying functions is not possible (and doesn't make much sense).

— you may consider using 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).

Installation:

Get the latest asset package from the releases page. Import it into IDE.
Alternatively copy the code from corresponding scripts into your project.

Example:

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_array = deep_copy(array);
var new_struct = deep_copy(struct);

// Both have identical values
show_debug_message(array);
show_debug_message(new_araay);
show_debug_message(struct);
show_debug_message(new_struct);

// But aren't holding the same references
show_debug_message(array == new_array);                                     // false
show_debug_message(array[0] == new_array[0]);                               // false
show_debug_message(array[1] == new_array[1]);                               // false
show_debug_message(array[2] == new_array[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();
new_struct.thing.func();

Author:

Nikita Musatov - MusNik / KeeVee Games

License: MIT

deepcopy.gml's People

Contributors

keeveegames avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

darkenigma12

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.