Eli Foster
Eli Foster

Reputation: 68

Storing game object constants without a giant "Constants" class

Basically, my question is the same as this one, except that I have to execute code on every single constant at very specific points in time.

To be more specific, I am attempting to get my game addon to adhere more to object oriented and good design principles and standards. Formerly, we had giant classes containing game object constants, with gigantic static register methods (eg registerFirearms, registerArmor) that would register those objects in their appropriate registries, and then assign them to their global constants.

For example:

public class Items {
    public static Item firearm;

    public static void register() {
        registerFirearms();
    }

    public static void registerFirearms() {
        firearm = new FirearmItem();
        Registry.register(firearm);
    }
}

At the appropriate time in the game loading process, Items.register(); would get called, and all of the values in that class would be ready to be used throughout the addon.

I'm just not really sure how I should go about storing these object instances without having some monstrous unmaintainable several-thousand-line class.

Upvotes: 1

Views: 128

Answers (2)

Zachary
Zachary

Reputation: 323

It appears that every item eventually needs to be registered with the Registry. As such, a long list of stuff is unavoidable in one way or another. Because of this, I don't think you are necessarily looking at a code organization issue. In my opinion, this an issue of: not having adequate data management tools to handle the data.

If possible, I would recommend managing the data outside of java code, exploring options like SQLite. In this way, you can configure the database to make maintaining the item data a much more efficient process. Then change your java code so that it registers items by iterating through the results of a query to your database. This should make your data easier to manage and make your java code more concise.

Upvotes: 1

Bruno Zamengo
Bruno Zamengo

Reputation: 860

What about storing all your items into a configuration file (or even better an XML) and then having your Items.register(); read the file and initialize all the items?

I would do something like

public class Item {
    public Item(String type /* any other thing about the item*/ ) {
        // do your stuff...
    }
}

public interface ItemCollection {
    public void register() throws YourProjectException;
}

public interface ItemSet implements ItemCollection {
    private final java.util.Set<Item> items;

    public void register() throws YourProjectException {
        try {
            // open the file
            // read each item and store it in "i"
            items.add(i); // you must override the equals method if you want each item to be added just once...
            // close the file 
        } catch(Exception ex) { // even better if you manage each exception separately
            // manage the exception 
            throw new YourProjectException();
        }
    }
}

Upvotes: 0

Related Questions