Which is the best program architecture for games with multiple buildings?

Hello, I’m currently working on a project like Anno but with more different buildings. I have set up a building class, which contains the variables for every building, like building cost, production time, resource to consume and generate etc.

Now i have to make a choice. The first option is to make for each building a sub class, which inherits from the building class, and override the variables to the values from this specific building, or the second option is I create a container which holds a struct or something for every possible building and by creating a new building i have to tell the building script, which building I want, and then load the data to this building from the container.

I ask this question because I want to know which method is better in the end. I think the first option allows me to make for example different animations for each building, because I can override an animation function or something. (I can do this with the second option too, but i have to check which building is currently loaded to the object and this will result in less readability with a lot of if statements and less performance). The second option is easy to menage. I can change all variables easily if the game was not properly balanced, or I would make some changes in production time, building cost etc.
I want to know how to tackle this problem and get the most advantages out of it.

This is obviously my first game and such decisions are very hard to make. Thanks for any help.

I would strongly advise you to take the data-driven (second) approach. Define your buildings in text files or Google spreadsheets or even as Unity prefabs with different values for the public properties. But they’ll all use pretty much the same code (add an extra component here or there as needed, but that too should be driven by the data).

Note that this should not result in a big pile of if statements. If it does, you’re probably doing something wrong.

1 Like

I’m also thinking that this approach is the better one. But some solutions with this approach are still unclear. But I come back when I have to deal with these. Maybe the solutions are very easy but I can’t see those through the fog.

Thanks for your advice!

Often it can be hard to see the details before you dig in. As you start to build it out, the problems (and perhaps the solutions) will become much clearer.

Good luck!

1 Like

Might be worth looking at delegates with scriptable objects… I’m new to Unity too, but it seems a good option if the differences between buildings lend themselves to an api.