Java – how to copy objects while maintaining consistent references?
I want to implement a checkpoint system for the game I'm developing. In order to create an independent copy of a level, I need to copy all variable objects in a level In a simplified example, my class looks like this:
public class GameObject { ... private Level level; ... } public class Level { ... private List<GameObject> gameObjects; ... }
But there is a problem: when I want to copy a level and its objects, the references become inconsistent For example, I can deeply copy my level instance and all GameObjects But when I do, the level reference in GameObject no longer points to the "right" level In this case, I can call each object's method and reset its level However, more and more types of objects will be started, which will become more and more complex, because objects may or may not have their own set of objects, so these references must also be updated
Is there any design pattern like this, or any Java specific, that makes this problem easier?
edit
I just have an idea: how to create some similar wrapper classes for each GameObject, as shown below:
public class GameObjectWrapper { ... private long gameObjectID; private Level level; ... }
This will serve as a reference to all objects that need to be referenced And each GameObject will get a unique ID, and the level will have a mapping that links each ID to the actual GameObject (similar to GameObject level. GetObject (long ID)) Although this may work, I still feel that there must be a better solution
Editor: further clarification
It seems that I have not made my question clear enough, so I will simplify and summarize it:
I have two sample objects (objecta and objectb): objecta contains a reference to objectb and objectb contains a reference to objecta
Such a thing
public class MyObject { private MyObject partner; ... public MyObject shallowcopy() { return new MyObject(partner); } }
I want to copy two Using shallow copy, I get two new objects: newobjecta and newobjectb The storage reference of newobjecta (should point to newobjectb), but still points to the original object B, and vice versa
MyObject objectA = new MyObject(),objectB = new MyObject(); objectA.setPartner(objectB); objectB.setPartner(objectA); MyObject newObjectA = objectA.copy(); //newObjectA's partner Now is objectB (instead of newObjectB) MyObject newObjectB = objectB.copy(); //newObjectB's partner Now is objectA (instead of newObjectA)
Now, I can "just" run all my objects and map their old objects to new objects, but this seems to be a slow and too complex solution What is the simplest and most effective solution?
Note: I think this is released to GameDev stackexchange. COM, but I find it more like a specific problem of programming and design than a specific problem of game development
Solution
If you only copy level objects and want references in their list to point to new copies, you cannot iterate through them in duplicate
Such a thing
class Level { List<GameObject> gameObjects; Level copy() { Level level = new Level(); level.gameObjects = new ArrayList<>(gameObjects.size()); for (GameObject gameObject : gameObjects) { level.gameObjects.add(gameObject.copyWithReference(level)); } return level; } } class GameObject { Level level; GameObject copyWithReference(Level level) { GameObject gameObject = new GameObject(); gameObject.level = level; return gameObject; } }
Edit:
You can have a container of GameObjects:
class GameObjectSet { Level level; List<GameObject> gameObjects; GameObjectSet copy(Level newLevel) { GameObjectSet gameObjectSet = new GameObjectSet(); gameObjectSet.level = newLevel; // copy all gameObjects and make them point to new gameObjectSet return gameObjectSet; } }
Now you will reference gameobjectset. In the level class and GameObject class