Skip to content

Instantly share code, notes, and snippets.

@McTwist
Last active March 1, 2018 23:48
Show Gist options
  • Save McTwist/56853ab0f98f2fe00dbcdbe3a6ee9f59 to your computer and use it in GitHub Desktop.
Save McTwist/56853ab0f98f2fe00dbcdbe3a6ee9f59 to your computer and use it in GitHub Desktop.
Clones an object
// Clone current object
function SimObject::clone(%this, %name)
{
%this = %this.getID();
%oldName = %this.getName();
%name = %name !$= "" ? %name : %oldName;
%this.setName(__CLONE_OBJECT__);
%obj = new (%this.getClassName())(%name : __CLONE_OBJECT__);
%this.setName(%oldName);
return %obj;
}
@qoh
Copy link

qoh commented Mar 27, 2017

function SimObject::clone(%object)
{
    %name = %object.getName();
    %object = %object.getID(); // Needed to support `name.clone()`
    %object.setName("__CLONE_OBJECT__");
    %clone = new (%object.getClassName())(%name : __CLONE_OBJECT__);
    %object.setName(%name);
    return %clone;
}

Avoid eval at all costs :)

@McTwist
Copy link
Author

McTwist commented May 6, 2017

I had no idea that you could do it like that. I also was not aware that the first variable could send in the name as well. That is indeed interesting and useful. I will modify mine according to your directions.

Edit: I also made it cleaner and added so you can change the name of the object at the same time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment