Thursday 4 May 2017

Outside the Box Project Alpha Work - Defense Building Script

Originally the building of defences ran off 2 scripts, a Place Defences Script and m_Defences Manager Script.
They referenced each other, with the manager script organising how much it would cost to build a defence and which defence would be placed and when, such as the level 01 defence being built first, not last.
The place defence script organised which defence would be built (at the time either a levelled cannon or a levelled outpost/tower) and would instantiate it.
The trouble we had for a long time was that the first level of defence's instantiated clone would spawn switched off and thus wouldn't do anything until the second level was bought, at which point the shooting script on the second level of defence would run.


We found that the problem was that the prefab saw the level 01 defence as being switched off always, and so when it instantiated the game object, it spawned switched off. We fixed this by turning the game object on in the prefab of the build spot, however the next problem occurring was that the level 01 defence was just on the game board all the time.


We tried setting the active boolean of the level 01 defence to false in the void Start function, which worked, but then when we tried to buy the level 01 defence it would no longer spawn, despite having paid for the level 01 defence and that when you tried to upgrade, the second level of defence would spawn.
Ibby suggested it may have been a problem with the code trying to interact with each other between the Place Defences Script and the m_Defences Manager Script, and that we should try merging the two piece of code to work as a single unit, which we set about doing.


Prior to Ibby's suggestion of merging the code, we also tried duplicating the code we had already built and setting it to just build the towers, because we found that the cannon's were building just fine, and so we sought to rectify our prior trouble by duplicating the code to build the cannon's and just make the code build the towers, however this didn't fix the issue and the level 01 defence was still not being spawned properly.


By merging the code, as Ibby suggested, we found that most of our troubles with building the defences was gone, the only remaining trouble, which we have yet to fix, is that the box collider (which detects mouse clicks, which in turn allows the player to buy and build defences) would regularly not detect mouse clicks. It didn't make the game unplayable, but frustrating as the player can sometimes click repeatedly without buying a single defence, but can on other occasions, buy the defence without trouble.





With the defence building functionality all on one script, we found that it became tillable. Basically, the only difference between build spots is the tag of the build spot, cannon build spots are tagged as "Cannon Build Spot" and the same for the towers except the tag has Towers in place of Cannon.
This means we can use this code for most of our defence building by simply repeating the code in the script to build the new defence and changing the referred to Tag, for example if we wanted to use this to build our bubble wrap AoE game object to slow down the enemies, we'd just copy the code again that governs both the cannon's and towers and just replace the game objects in the list with the bubble wrap game object, and then make a new script that would be on the bubble wrap game object, as a prefab, that provided the bubble wrap with it's AoE, slow down, functionality.

No comments:

Post a Comment