Class that creates another new superclass at runtime

Hi,

ok topic sucks and this is kinda hard to explain, but here we go:

In my game I have this workshop thing, which should be able to create items. For every item in game there is a different class, for example:

Item (Base class)
ItemDoor
ItemChair
ItemFence
ItemTree

Now, I have this list that my workshop holds, called buildableItems. It currently holds a string like “Build Door” (for the UI) and the information on what is required to build that thing.

Here is an example of the carpenter:


public class W_Carpenter extends Workshop {
    public W_Carpenter(AssetManager assets, Map map) {
        super(assets, "data/carpenter3.png", "data/carpenter0.png", "data/carpenter1.png", "data/carpenter2.png", map);
        MaterialsRequired r1 = new MaterialsRequired(ItemList.ITEM_LOG, 2);
        MaterialsRequired r2 = new MaterialsRequired(ItemList.ITEM_RAWSTONE, 1);
        requiredForConstruction.add(r1);
        requiredForConstruction.add(r2);
        name = "Carpenter";
        // ^^ this is only what's required to build the workshop

        BuildableItem itemDoor = new BuildableItem();
        itemDoor.name = "Build Door";
        MaterialsRequired b1 = new MaterialsRequired(ItemList.ITEM_LOG, 2);
        itemDoor.matsRequired.add(b1);
        buildableItems.add(itemDoor);

Now in that buildableItem object I want to hold the endProduct, which would be the class ItemDoor. How would I go about that?

Here’s buildableItem FYI


public class BuildableItem {
    public String name;
    public ArrayList<MaterialsRequired> matsRequired;
    // HERE I need something like
    // Item endProduct;

    public BuildableItem() {
        matsRequired = new ArrayList<MaterialsRequired>();
        name = null;
    }
}

But I can’t just go “Item endProduct” there, because then I would have to instantiate (?) the ItemDoor, which I don’t want to do (because it holds reference to the texture etc)…yet. If I don’t create the object I can’t even go through thousands of instanceof lines, because it would probably result in a null pointer exception I guess.

The endProduct (ItemDoor) should be created when all the requirements are fulfilled. So that the future code would look something like this


Item endProduct = new buildableItem.endProduct;

I hope I have explained that ok, any ideas on how to accomplish this? :smiley: