0

I have a class character that I'd like to create a copy of before a certain event so that if the user screws up he can retry with the copy. To implement this I use the copy constructor like this:

public class character
    implements Serializable
{
private String                          name;
private HashMap<String,Integer>         values = new HashMap<>();
private ArrayList<String>               traits = new ArrayList<>();
private enums.playerRelations           playerRelation;
private static character                playerCharacter = new character();
private transient Runnable              combatAI;
private transient Runnable              tradingAI;
private ArrayList<character>            companions = new ArrayList<>();
private ArrayList<item>                 inventory = new ArrayList<>();
private int                             maximumInventorySize = 20;
private int                             maximumSkillSlots = 10;
private transient ArrayList<Pair<skill,Boolean>>  skills = new ArrayList<>();
private int                             portraitResource = R.drawable.ic_bear;
private event                           eventContext;

public character(character o)
{
    name                    = o.name;
    values                  = new HashMap<>(o.values);
    traits                  = new ArrayList<>(o.traits);
    playerRelation          = o.playerRelation;
    combatAI                = o.combatAI;
    tradingAI               = o.tradingAI;
    companions              = new ArrayList<>(o.companions);
    inventory               = new ArrayList<>(o.inventory);
    maximumInventorySize    = o.maximumInventorySize;
    maximumSkillSlots       = o.maximumSkillSlots;
    skills                  = new ArrayList<>(o.skills);
    portraitResource        = o.portraitResource;
    eventContext            = o.eventContext;
}

// ...
}

However, I don't know how to deep clone a Runnable object. How can I deep copy those? Is that at all possible using the copy constructor?

I notice that Runnable is different from the other members of character insofar that the part of concern is stored within a function rather than an attribute...

1
  • 1
    Don't use Runnable but a strategy interface of your own which includes a proper clone method. As a side note, your class looks very overburdened with properties, splitting and/or grouping them would be an improvement. Commented Oct 14, 2018 at 19:28

1 Answer 1

1

If the only thing you know about, say, combatAI is that it is Runnable then you can't deep copy it - it has no depth so to speak - it's just Runnable.

You may want to rethink this approach and explicitly have check points in the game (?) so that you could create a combatAI and the rest of the state from the check point data.

Also note that your copy of inventory is just one level deep, the original and the new characters share the same items. Same deal with companions

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.