The title may be a bit confusing so allow me to elaborate. I’m making an RTS game with a few friends and I have come across a problem. I want to have easy access to changing how the buildings work but keeping a “template” for possible attributes for the building (i.e. can spawn troops). How I did it originally was use a switch statement in the initialization method (called Start() in Unity) to check which value was assigned to the building via an enumeration (i.e. Barracks) and if its Barracks is selected then switch to run code designed for barracks.
While in theory that should work; I have not tested it. I programmed it and then decided to redo all of the code. How would any of you do it? I can’t help but feel as if the switch statement is in poor use for this sort of problem.
having a switch case statement would basically mean all your functionality would be in a single script, not particularly efficient or easy to maintain if nothing else.
It is hard to determine what is best since you have not explained the template parameters you have in your buildings, or the variety of actions they do. It is also hard to tell if what you are asking for is convenience of development, or dynamically changing building functionality at runtime. On the simplest level, changing the unit spawned can simply be changing the prefab unit spawned by the building, but beyond that it’s hard to tell what else you might need.
As general a solution i can think of is something i use on a regular basis, to have a control script which aside from common functionality, invokes delegate events or UnityEvents based on its parameters, like building speed maybe.
The other components which control the actual spawning of units or other functionality would then link up to the control script’s delegate events/UnityEvent, and be called when the delegate event/UnityEvent is invoked by the control script.
The buildings will have attributes such as being able to spawn troops or deposit resources. I have heard that using derivations of the class would be best but also that making component scripts would be easier on Unity.