Confusion re: Scripts Relationship (DefenderSpawner, StarDisplay, etc.)

Hi,

I’m still relatively new in my coding journey and, as such, am only really beginning to grasp the concept of scripts communicating/passing data to each other via public methods and parameters, etc.

Looking at Rick’s diagram of the four scripts needed to get a resource system up and running for this game, I couldn’t help but think… How would I ever figure out:
a) That 4 such scripts were needed?
b) The specific functionality to be found inside each script

E.g. The Defender script, to me, feels almost superfluous - couldn’t this information (re: Defender cost) be incorporated within the StarDisplay.cs (since StarDisplay also deals with spending currency/stars, etc.)

I imagine if I were to have tried to implement this resource system myself, I wouldn’t have reached the conclusion that 4 scripts were necessary. Instinctively, I might have just used 2…

If anyone can offer any guidance re: how these sorts of coding decisions are made, I’d really appreciate it.

Thanks!

2 Likes

These things are generally acquired through experience, but general rule is to follow your instinct and focus on getting code to work first and foremost, as opposed to trying to overthink/over-engineer it right away.

For a more theoretical discussion, you may look into the SOLID principle, and in particular the S, which stands for “Single Responsibility Principle”:

In short, you want to have your class have one responsibility: this is often misinterpreted to mean “do one thing”, “have one method”, etc… which is not what it is. It means it should have just one main reason to change, one responsibility. So if you have a DefenderSpawner class, it’s responsibility is to spawn defenders: it may provide multiple helps and interactions within that framework - like provide information about spawning (e.g. a method like GetDefenderSpawningInfo(), CalculateNextRoundSpawn(), etc), or like Save/Load operations, or perform some init or clean-up functions within the overall running of your game, etc - but all within the one responsibility of spawning defenders… that’s its one reason to exist and change.

1 Like

Thanks so much for the explanation :slight_smile: It’s really appreciated!

1 Like

Privacy & Terms