Let’s Make a 4X Space Game with Unity – Part 27 Ship Production Using Turns
Hi Guys and welcome to part 27. In the last part we started to set up our turn system. In this part we will be using what we did last time to make our ships build one turn at a time.
I left you guys with a challenge last time to code the for loop we will be using to do this. So here is how I am doing it, if yours is different but works however that’s great! Coding is about trying out new things and coming up with different methods.
Open up the TurnManager script so we can complete this for loop.
In the for loop we need to loop through the planets in the players owned planet list, so we need to reference the planet at i in the loop.
We then need to check if that planet has a starBase and if it does we need to add the production points from that planet onto the ship in the first slot of the starBase cue. We do this with an if statement.
Ok so now we need to add a value to our Ship script which tells us how much production we need to build the ship. Open up the Ship script and add a new public float called productionValue. For now just set this equal to 10 as soon as it is declared. This will be the total amount of production our ship will be worth (we will change this later so it scales with ship size and tech level etc…)
But what do we use to keep track of the current production added to the starbase? We need to add another float to our Starbase script; add a public float called currentProduction. This will be the value we add to and then check if it is equal to or over the production required for the ship, before building it. Set this to zero in the StarBase constructor.
Now we can head back to the TurnManager script. To complete the for loop.
Before we start to compare the ship production in the first slot of the build cue with the ship production there is just one last thing we need to check. What if the build cue doesn’t have any ships in it? Add an and condition to the if statement we already have in the for loop to check the buildCue count is not 0.
We are almost there. There is just one more variable we need and that is how much production the planet is adding to the build.
Open up the Planet script. We need to add a new float called production. For now set this equal to 2 when it is declared. Later this will be based on the planets population, buildings and other bonuses.
We now have all the variables we need and we can add the production to the build every turn. We do this by adding the planet production to the current production in the star base.
Add a new if statement to check if the production value is greater than or equal to the production required for the first ship in the buildCue.
What happens if we go above the ship build cost? We don’t want to loose any production points. We need to check the value is above or equal to the production required for our ships. We then take the production required away from the total amount of production added to the starbase. Let’s call this value difference. We then make currentProduction equal to difference.
Finally we spawn the ship object and remove the ship from the starbase cue. To do this we need to add a reference to our FleetManager script. Add a public FleetManger called fm at the top of the script and add the reference to it in the inspector.
Now we can call the BuildShip method and remove the first ship from the cue.
That code is a little messy, it might be a good idea to go back and reference some things with shorter variable names so the lines of code are not so long. For example we could have a variable called sb for our starbase and bc for our build cue. I will leave this up to you however (the code I provide at the end of the tutorial will be tidied up like this if you need to see what I am talking about). I thought for the purposes of the tutorial it would be better to have the full code so it is easier to see where the values are coming from.
We now have the start of a build system and all we need to do is change our build button a bit and update our UI to show how many turns it will take to build the ships in the cue. I will show you how to do this next time (please have a go yourselves). There is also an issue with the way things currently work in that the resources for the ships will be taken when the ship builds (really we want that to happen when it is added to the cue).
That concludes this part. Next time we will fix the resources issue and connect up the UI so that by the end of the tutorial the ship build system will be fully functional. After we are done with that we will be looking at spawn points for our ships and ship movement.
Part 28 Ship Production Using Turns 2 – Coming Soon