Sorry, but I can not ask a question without any introduction. (If you don’t know whether to read all this, I’m still trying to ask a question, the problem is that I’m trying to change the properties of an element, does this apply to all the other same elements, how can I fix this?)
I have an Item class with some properties and variables like itemCost , durability , etc.
The Weapon class inherits Item .
I have an ItemGenerator class that initializes all items in a single array. It contains functions such as:
private static Weapon CreateUniqueWeaponForItemList(int ID, int itemLevel, ..... string description) { Weapon item = new Weapon(); item.ID = ID; item.ItemLevel = itemLevel; ... ... item.Description = description return item; }
something like that. This list of items is initialized at the beginning of the game. I have an array of elements containing all the elements.
public static Item[] ItemList = new Item[200];
Listed below are all the unique items in the game created by the function above:
ItemList[1] = CreateUniqueWeaponForItemList(1, 5, ....., "this is item!"); ItemList[2] = ....
etc. At the moment this works great. When I create an item, I simply use the Item ID to indicate which item I want to create. It is also easy to save and load, just save the ID element in PlayerPrefs .
But when I start adding additional functionality (for example, updating items, updating damage, and something), I realized that this architecture is bad.
If a player has two or more of the same elements, problems begin here. If I try to change the Item properties, they apply to the ItemList[ID] , and not to the item I want.
I think they need to paste the code here to be clear. I have an inventory system, with private List<Item> _inventory = new List<Item>(); in the Player class. I have a treasure chest that gets an item from an ItemList to create
loot.Add(ItemGenerator.CreateUniqueItem(2));
loot is the Item variable in the Chest class. Below are explanations of CreateUniqueItem
public static Item CreateUniqueItem(int ID) { if (ID > ItemList.Length) { return null; } if (ItemList[ID] != null) { return ItemList[ID]; } else { return ItemList[0]; } }
When an item is created, the player can capture it in inventory. I am just _inventory.add(item); , for example Item is equal to ItemList[2]
Player.Insntance.Inventory.Add(chest.loot[2]); chest.loot.RemoveAt(2);
breast .loot
public List<Item> loot = new List<Item>();
which contain all the elements to capture.
I think the problem is here.
-----------------------------------------
So, here is the question itself. If I want to make an update item, I use
_inventory[0].MaxDamage++
but MaxDamage increases on all other elements of the Player’s inventory, I can’t understand why? I do not use ItemList[ID].MaxDamage++
I think that I should save all created unique elements in a file or something like that, and refer to them from the inventory, and not give a link to the inventory.
Or save in the file only the element identifier and add the int variable to the elements, like upgradeLevel , and save it. So due to upgradeLevel items can get buffs for damage.
But right? And what is the best way to do this?
-----------------------------------------
Here is a short insertion of the Item class:
public class Item { private int _ID private int _itemLevel; ... ... public Item(){ _ID = 0; _itemLevel = 0; ... ... } public ID{ get { return _ID; } set { _ID = value; } public int ItemLevel { get { return _itemLevel; } set { _itemLevel = value; } } ... ... ... }
The weapon class is the same, but there are additional variables, such as damage.
public class Weapon : Item { private int _maxDamage; public Weapon() { _maxDamage = 0; } public int MaxDamage { get { return _maxDamage; } set { _maxDamage = value; } } }
I can provide a complete list of code on GitHub if necessary. But I think the code I have inserted is higher than enough. I hope I don’t forget anything.
I will not be surprised if the problem is trivial, but for some reason I cannot understand it and it causes a headache.
I apologize if there is too much text, but I could not shorten it.
Also sorry for my poor English spelling.
Thanks in advance.