1

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

3 Answers 3

1

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.

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

6 Comments

Thanks Funtik for your help it is very appreciated, regarding your question, I was thinking of keeping stuff divided, and I had made a boolean called equipped, which is set to true if the player selects equip and then adds the object into the list equipped, guess I'm a newbie and that sounds more easy to do, but I had done a for loop that checked on the equipped items list and added values to the player, but it uses the arraylist equipped to index them and apply the bonuses.
Guess I can swap the for loop to use the inventory list and search for equipped items and then apply the bonuses of the items that have the boolean equipped set to true only.
Have a plate of choco cookies :) (Y)
thanks, I just love that taste of an internet cookie. BTW, got any link for your game? I'd love to see some screenshots / alpha gameplay
:D Soon I'll have links, just trying to finish the crafting system and then making some hand-made levels + drawing animations xD, after that I'll be putting up a website with a download option for the game, it'll be free :), once it's up I'll give you a link! thanks for asking that, really makes me want to finish the game faster!
|
0

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.

1 Comment

Thanks for your efforts in replying, have a Choco Cookie!
0

If you add an object to a list, it will retain all its field values. Just:

Tomato yourTomatoObject = otherList.remove(index);
inventory.add(yourTomatoObject);

2 Comments

Thanks for that important bit "If you add an object to a list, it will retain all its field values.", now I want to know, If I remove the object from the list and then add it to another one, will still keep its values?
The remove method returns the removed object, so yes, it will. See edits

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.