Hi Guys and welcome to part 21. Last time we fixed a bug and made our galaxy respawn more efficient. Today we will look at adding resources and having a cost to build our ships.
Let’s start by making a new script called Resources. Add this script to the Empires folder. Make this new script a pure class by removing the “: Monobehavior” next to the class name and the Update and Start methods.
For my game I am just going to have 3 resources of credits (money), minerals and food, but feel free to add as many as you want. Declare 3 public ints (credits, minerals, food) with get and set.
Like usual we need a constructor so add one called Resources. Pass it an int for each of your resources.
Set the resources values using these passed ints.
Now add two public void methods. One called AddResource and the other SubtractResource. Pass them both two ints called resource and amount.
We are going to use a switch/case to check the resource int. In my game I am going to make it so credits is resource 1, minerals 2 and food 3. In each of these cases we either add or subtract (depending on the method) the resource in question. I am also going to add another case for 0. In this case it will add/subtract amount to all the resources. For the default we will just output a Debug.Error “Unknown Resource”.
Our data structure is now complete.
So how do we actually initialise the resources in our game? Let’s add a new script to our Player object. Call it PlayerManager (and move it to the Empires folder). Declare a new public Resources called playerResources (add get and set) and in the Start method set it equal to a new Resourses. I am going to pass it 100 of each of the resources but pass it as much or as little as you want. Also add a Debug.Log to tell the player how many resources they have (we will be creating some GUI for it in a later part)
Let’s also make an instance of this script as there will only be one in the game. Declare a public static PlayerManager called playerManagerInstance. Add an OnEnable method and set playerManagerInstance to this script inside that method.
This causes a slight issue with our FleetManager script. When we enter the galaxy view the FleetManager disables the Player object which may affect us accessing this instance. We can easily get around this by adding a new GameObject as a child to the Player GameObject. Call this Fleets.
Now move the FleetManager script to Fleets from Player (you will need to remove the script from the Player object and then add it to the Fleets object)
We need to change the code in the SolarSystem script so that we now find the Fleets object and not the Player object.
We also need to quickly re-assign the functions on our buttons which handle building ships and disabling them.
Just check you can still build ships and they disappear/reappear when they are meant to before moving on.
We now have a good foundation for our resource management :). We will be coming back to resources later on in the tutorial series but I think we can leave it there for now and move back to our ship building.
So whats next? We now need to take some resources away when we build a ship. Open up the FleetManager script. In the BuildShip method we need to do a check and see if we have enough resources. To keep things simple I am going to say my ships cost 10 credits and 10 minerals.
Note: Later on we will come up with a way to import these values from some sort of file (probably an XML file). This way we can easily have different classes of ship and easily mod our game to add more.
Add an if statement at the top of the method with the conditions credits >= 10 AND minerals >=10. In this if statement subtract 10 credits and 10 minerals. Add a debug.log that tells the player the resources have been subtracted.
Add an else statement immediately after with a Debug.Log that tells the player there are not enough resources.
This now handles the resources but our ship will still build no matter how few resources we have left!
So how do we fix this? We simply move all our current BuildShip code into the if statement.
Now press play. You should only be able to build ships until you run out of resources.
That concludes this part. In the next part we will start looking at ship build points (usually star bases in most 4X games) which will allow us to start having build cues and make our ships appear in one place rather than in random positions around our solar systems :).
Scripts_for_part_21 (zip file)
Scripts_for_part_21 (paste bin)