If I wanted to have more than one resource type, for example: wood, stone, and iron, would I just duplicate the code we added in this lecture? I mean adding an additional private field and public event in the RTS player for each additional resource and adding an additional TextMeshPro field and event handler function in the resources display for each additional resource type. Or would there be a better way to go about this?
Hi there, yes one way to do it would be duplicating the existing code for different types of resources.
If you are planning on doing more than 2 resources though you might wish to think about ways to scale it more efficiently. One way you could this is make a public enum called resource types have a bunch of different categories. Then each type of resource generator could just list the type of resource it produces and pass it along to the player when asking it to increase that type of resource. You could pick it from a serialized field in the editor, and it would save time replicating code for each new resource.
In short you want to think of ways to write the code once, and than just add a new variable and select an option in the editor rather than writing new code each time. Take advantage of unity’s prefabs and scriptable objects, and C#'s enums and classes so you don’t have to rewrite code.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.