javascript - Object is created but seems to be sharing a reference -


i building editor, in editor when press play converts bunch of objects prefab (which explains engine how build object). when stop them rebuild prefab again , should original state (before play pressed). issue having when stop pressed items don't go original state. seem stay in state in when stop pressed.

here class using convert prefabs , forth:

class prefab {      public name: string;     public components: prefabcomponent[] = [];      public static create(gameobject: gameobject): prefab {         let prefab = new prefab;         prefab.name = gameobject.name;         gameobject.components.foreach(comp => {             let prefabcomp = new prefabcomponent;             prefabcomp.name = comp.name;             (let c in comp) {                 let prefabprop = new prefabproperty;                 prefabprop.name = c;                 if (typeof comp[c] == 'object') {                     prefabprop.value = object.create(comp[c]);                 } else {                     prefabprop.value = comp[c];                 }                 prefabcomp.properties.push(prefabprop);             }             prefab.components.push(prefabcomp);         });         return prefab;     }      public static toobject(prefab: prefab): gameobject {         let gameobject = new gameobject(prefab.name);         prefab.components.foreach(comp => {             let newcomp;             if (comp.name.tolowercase() != 'transform') {                 newcomp = gameobject.addcomponent(comp.name);             }             if (!newcomp) {                 newcomp = gameobject.getcomponent(comp.name);             }             comp.properties.foreach(prop => {                 if (prop.value instanceof sprite) {                     newcomp[prop.name] = sprite.create(prop.value.path);                 } else if (typeof prop.value == 'object') {                     newcomp[prop.name] = object.create(prop.value);                 } else {                     newcomp[prop.name] = prop.value;                 }             });         });         return gameobject;     }  }  class prefabcomponent {      public name: string;     public properties: prefabproperty[] = [];  }  class prefabproperty {      public name: string;     public value: any;  } 

here how starting , stopping game:

play.addeventlistener('click', (event) => {     event.preventdefault();     // game has started     // stop pressed     if (game instanceof spynginmain) {         game.stopgame();         game = null;         play.classlist.remove('active');         pause.classlist.remove('active');         // reset editor         editorobjectmanager.clear();         prefabs.foreach(prefab => {             editorobjectmanager.additem(prefab.toobject(prefab));         });         updatescene();     }     // game has not started     // play pressed     else {         game = new spynginmain();         prefabs = [];         editorobjectmanager.items.foreach(item => {             prefabs.push(prefab.create(item));         });         game.init(scene, prefabs);         game.startgame();         play.classlist.add('active');     } }); 

the issue seems when try convert object prefabproperty class. think isn't creating instance creating reference original, not sure...

your create method retain references original object because of following code:

    if (typeof comp[c] == 'object') {         prefabprop.value = object.create(comp[c]);     } else {         prefabprop.value = comp[c];     } 

the object.create call creates new object original object prototype, if subsequently add/modify/delete property in original object, changes appear in newly created object.

i think meant here clone original object? there many existing implementations cloning (eg lodash's clonedeep).

i replace 5 lines quoted above single clone(comp[c]) call, since typeof check not reliable in other ways (eg typeof null == 'object' true, typeof [1,2,3] == 'object' true, etc).


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -