How do I move an object from one player to the other?

How do I move an object from one player to the other? For example in runescape you can trade with other players or place your stuff in the bank, how do I program that in java or can you point me in the right direction on what funtion I need to learn?

Thank you.

UM. That totally depends on the code design of your game.

In general players wil ltypically be repsented as objects that have stat fields and lists of refernences to objects oin their invantory. Moving an object from one player to another involves copying that rerference to the new owner’;s invatory lsit and deleting it from the current owners list.

The bank would also be an object which has a list for each player.

player1.giveItem(player2,item);

Would that work?

:smiley:

Yes.

Then, of course, the giveItem method would go something like this:


public void giveItem(Player player, Item item)
{
    if (items.contains(item))
        items.remove(item);
    player.addItem(item);
}

public void addItem(Item item)
{
     items.add(item);
}

And voila. This assumes you’re storing items in some sort of List.