- Home /
Script architecture for tile-based tower-defense game
Working on a simple 2D tile-based tower-defense game just to learn. However, I'm really bothering with script architecture (had this on previous projects too). Currently able to place a 'turret' from prefab on the grid. this grid contains two tilemaps, 'ground' and 'highlighted'. If I click on a turret I want to highlight all the tiles around the turret as this is its attack range. can do this with setTile on the 'highlighted' tilemap. I figured it all out how to do it, but is already starting to get messy with how scripts/objects reference each other, and which functions should be in what class.
What would be the cleanest script structure, and placed on what object? what I currently thought are the scripts: "Turretplacement" on a manager to move and place the turret to a location. "Turret" on the turret prefab with all its data and behavior. "hoverTile" on a manager which takes coordinates from "Turret" for setting tiles and removing tiles in the highlighted tilemap depended on whether the turret is selected. Should i create a sort of click/selected script on a manager? does the "HoverTile" even have to be there or could it be in the "Turret" script. but then I would need that to have references to a tilemap and a tile while I would want it to keep solely about the turret, and give its coordinates to another script to deal with this highlighting. How should these scripts reference each other? (no need for very detailed explainations, but just the general idea)
Answer by logicandchaos · Feb 08, 2021 at 02:28 PM
There is no easy answer for this, there are a lot of preference as well. What you need to do is study programming architecture and programming patterns. If your references are getting out of control that is a good indication you need to use some more advanced programming patterns, delegates and events. In unity there is also scriptable object architecture with I love! It solves a lot of referencing issues, but it's a bit to get used to, instances live in the projects folders instead of the scene, seems weird at 1st but really simplifies referencing.
https://www.youtube.com/watch?v=6vmRwLYWNRo&t=81s
https://www.youtube.com/watch?v=raQ3iHhE_Kk&t=1052s