Different stats on the same Array?

Hi there JGO,

first of all: happy new year! I’ve an array[] with all the items saved in them. If I change 1 stat of the array all the items with the same id will be changed aswell. So is there a way on having different stats in an Array[]? Please only tell me how to, I want to programm it myself!

Code:

	public static final Items[] tiles = new Items[400];
	
	public static final Items SWORD = new ToolsSword(10, 12); //for example these stats are AttackDamage and AttackSpeed

With this code all the items (with the same id) will have the same AttackDamage and ASpeed. So what is the best way to be able to change the attackDamage without changing them all?

if you use something like this:

Void changeAttackDamage(int attackDamage){
this.attackDamage = attackDamage;
}

then it will change the damage of all the items (with the same id).

If you don’t understood the question, just say it.
Thanks for helping already!
-RoseSlayer

I think you will need to split the whole thing up into ‘Item Definition’-classes (-> basically Singletons) and ‘Item-Instance’ classes.
Or just go and make ‘attackDamage’ static, but that will probably lead to some code-mess later on.

Have a nice day.

  • Longor1996

Are your fields for the attack damage etc. static? You’ll want to make them instance variables, otherwise the stats will be shared between every instance of the item.

They aren’t static, they are protected. All the variables are protected.

You need to realize that an array doesn’t hold objects. It only holds references/pointers to objects.

You probably have multiple references in your array, pointing to the same object. Your observation is that changing one array element changes others, while in reality you are changing one object, which you encounter multiple times when iterating over the references in your array.

What Riven said. In this case, each time you add your SWORD item to the array, you’re not creating a new instance, you’re just pointing the array element at the single instance of SWORD that you’ve already created. You could approach this from a few different angles. For example you could just make a new ID for each sword type that you need:


public static final Items SWORD = new ToolsSword(10, 12);
public static final Items SWIFTSWORD = new ToolsSword(8, 24);

You could use the factory pattern to create instances of ToolsSword objects that have the desired characteristics, or similarly (though less desirably) you could just create new instances of ToolsSword and assign it directly to your array:


Items[x] = new ToolsSword(10,12);
Items[x+1] = new ToolsSword(20, 6);

There are other approaches as well. The choice of which one to implement will depend on how you plan to access/use the Items[] array within your game.

How did minecraft solved this problem? If changing the name of a sword the don’t add another pointer to the Items[]?

First off, don’t call them pointers, call them objects. Two of the same instances cannot have different states. I think you’re getting confused between pass by reference and pass by value.


public class Sword extends Item {

	private int damage;
	private int speed;

	public Sword(int damage, int speed) {
		this.damage = damage;
		this.speed = speed;
	}

	public void changeDamage(int damage) {
		this.damage = damage;
	}

}

You cannot do something like this:


Item[] items = new Item[2];
Sword sword = new Sword(10, 5);
items[0] = sword
items[1] = sword.changeDamage(20); // the damage for items[0] will change; they are the same instance. Java is pass by value, not pass by reference.

You have to do something like this:


Item[] items = new Item[2];
items[0] = new Sword(10, 5)
items[1] = new Sword(12, 4);

So basically I need to make a new instance of them and increase the ID everytime I add another sword?

Yes, that’s exactly how you need to do it.

why use IDs/arrays anyways, you don’t need them.

Also true. You should keep your items contained in each entity’s class. Don’t just have a huge array of weapons that contains all of the player’s gear.

So just make for every item a new class? But for the normal items like for example a leaf, without any customizable id’s should I also make a new class?

You don’t need to add a Class for every item in your game, just make categories.
A sword or a mace are weapons. Weapons are equipment. Armor is equipment too and both share the same stat modifiers (at least in my game).
A leaf on the other hand is maybe used for a recipe (like tea or bandages). So you could make a craftingItem class.