What is the next step I need to take concerning my Item Database?

Hey guys, I’m working on an item database for an RPG. It works (or doesn’t work) as follows:

public static class ItemDatabase{
	
	private static Item[] entryList;
	private static int entryID = 0;	
	
	static ItemDatabase(){
		entryList = new Item[500];
		entryID = 0;
		
		item = new Item();
		item.idNo = entryID;
		item.name = "steel kukri";
		entryList[entryID] = item;
		entryID++;
	
    }	

}

Basically, I create each Item as a new Item, fill the variables, and add it to an array of all the possible items and access those array indexes when I need to refer to an item. It works pretty well but I know it isn’t complete.

My problem is, when I have an item such as a container, that stores other items, adding an item to 1 container, adds a copy of that item to all containers.

I tried to create a workaround for this problem by passing each request through the following function:

	public static Item Request(int id){
		int i = serializedItems.Count;
		serializedItems.Add(ItemDatabase.entryList[id]);
		return serializedItems*;*
  • }*
    This is designed to add a copy of the item to a List of serialized items upon request. However, I have found that “editing” 1 item still just points back to the original data reference.
    I believe this is because these are all just references pointing back to the original object, but I’m not sure what to do to create original instances of each item I add to my database.
    Any guidance on how to proceed would be helpful. Thanks :slight_smile:

My guess is you’re either copying the reference to that container item, or you’ve copied a reference to the array which keeps the items that is contained in that container item.

To make unique instances of container item, each instance needs to be created with a “new”, and it’s array should be created using a “new”.

Your workaround function is also just adding a reference to an existing instance, so that’s also not creating anything new.