How to manage a player with upgradable components?


I'm creating a space game where the players ship has many components that can upgraded as they progress. I keep getting myself in knots as I try to figure out how best to manage this. The player starts of with a basic version of everything e.g.

Basic Engine, Basic Hull, Basic Fuel Tank, etc....

Some of these components would rely on each other for example, the Engine needs to know if the fuel tank is empty. The fuel tank needs to know how much fuel the engine is burning so to reduce it's fuel amount.

What I started doing was to create a prefab for each component which holds the model and script. Each component script would inherit from a base script for each component type.

My idea was to then add and remove each prefab as children from a main Player prefab that would be the parent for all components and remain throughout the game.

I wasn't sure if to have a Component Manager on the Player prefab or maybe a Player Manager in the scene to control the lot. I then start to get stuck when I think about in the future and adding more component prefabs. Where do I reference these from? I'm going to need something that knows what components the player has bought, not just what they have installed.

Obviously the options don't stop there and I'm beginning to get buried in what I was hoping to keep as simple as possible.

Maybe this is a question of game management as a whole, not just a player. I think with Unity it can be very easy to get carried away with how to do graphics, sounds, moving your character around the screen but I realise that for me the most difficult part of game development is how to design the structure of the game. How your GameObjects load, intereact with each other, how they are saved, loaded and so on.

I would be interested on anyone's thoughts on this? Maybe a work flow you follow with Unity or a design pattern?

Thanks for your time, Dan

I don't know if this is the best way to handle it, but I'll tell you how I did it on the game I'm working on.

I have almost the exact same setup. A main character that has 4 components that can be upgraded. My workflow is as follows:

alt text

I hope that's clear enough. Basically you have "Masters" that are gameobjects sitting with scripts that take some of the work out of your character and smaller classes. In my project I have a MusicMaster, a DataMaster, a UpgradeMaster, a GameMaster and more.. most of them do the brunt of the work of actually running the game.

Again this is just one design pattern I've worked on my own from watching lots of Tron. There are different and better ways of handling this as well. For me, it makes the most sense and and keeps me organized which is very important.

Obviously there's more to this than just updating the floats. For example, whenever my size float is updated, I also update my character's scale. In your case you'll want to update your ship's engine model or graphic based on the current Size. If there are say 4 sizes, then just use an int instead, and use a switch: to setup the correct model or graphic.