Reputation: 41
Im making a game,an rpg (yeah again another dude trying to make one of those), and I've managed to make most of it except the inventory and how it behaves with the rest of the world, right now I want to move items between lists in a logic way, for example I kill a monster, the monster drops a tomato, Tomato() will be created inside the ArrayList worldInventory, and drawn at x,y, if the player walks by the tomato, that tomato should be moved into ArrayList Inventory, if the player desires to equip the tomato, the tomato should be moved into the ArrayList equipped.
So to make things more understandable I have 3 lists:
ArrayList<Item> worldInventory
ArrayList<Item> Inventory
ArrayList<Item> equipped.
I want to move copies of Tomato()
between them, and If a Tomato()
has its values changed inside a list and it's moved I want that Tomato()
to retain its values.
;D I'll give a big sized internet choco-cookie for whoever helps me out, much appreciated. Thanks n.n
Upvotes: 1
Views: 973
Reputation: 3409
If you add an object to a list, it will retain all its field values. Just:
Tomato yourTomatoObject = otherList.remove(index);
inventory.add(yourTomatoObject);
Upvotes: 0
Reputation: 13222
You can do like this:
public static void main(String[] args) {
ArrayList<String> test = new ArrayList<String>();
ArrayList<String> test1 = new ArrayList<String>();
test.add("test");
test1.add(test.get(0));
test.remove(0);
System.out.println("" + test.size());
System.out.println("" + test1.get(0));
}
You can add it to the one arraylist and remove it from the other.
Upvotes: 0
Reputation: 9162
on item drop:
Tomato tomato = new Tomato();
worldInventory.add(tomato);
on pickup:
worldInventory.remove(tomato);
inventory.add(tomato);
on equip:
inventory.remove(tomato);
equipped.add(tomato);
Just wondering why do you need equipped ArrayList ?
you can have a boolean flag inside your WorldObject ( boolean equipped = false
)
and just set it to true, when the item is equipped.
Upvotes: 1