Let’s Make a 4X Space Game with Unity – Part 21 Resources

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.

Part021Pic001.JPG
Declaring the Resources

Like usual we need a constructor so add one called Resources. Pass it an int for each of your resources.

Part021Pic003.JPG
Constructor

Set the resources values using these passed ints.

Part021Pic004.JPG
Setting Resources

Now add two public void methods. One called AddResource and the other SubtractResource. Pass them both two ints called resource and amount.

Part021Pic012.JPG
Two New Methods

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”.

Part021Pic013.JPG
AddResource
Part021Pic014.JPG
SubtractResource

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)

Part021Pic005
Initialising Resources

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.

Part021Pic011
PlayerManagerInstance

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.

Part021Pic009.JPG
Fleets GameObject

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)

Part021Pic010.JPG
FleetManager on Fleets

We need to change the code in the SolarSystem script so that we now find the Fleets object and not the Player object.

Part021Pic006
Find Fleets

We also need to quickly re-assign the functions on our buttons which handle building ships and disabling them.

Part021Pic007
Galaxy View Button OnClick
Part021Pic008
Build Ship Button OnClick

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.

Part021Pic016
If Statement

Add an else statement immediately after with a Debug.Log that tells the player there are not enough resources.

Part021Pic017
Else Statement

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.

Part021Pic018.JPG
Moving the Code to If Statement

Now press play. You should only be able to build ships until you run out of resources.

Part021Pic019.JPG
No More Credits or Minerals

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)

Part 22 Ship Building Points – Coming Soon

 

 

 

Advertisements

3 thoughts on “Let’s Make a 4X Space Game with Unity – Part 21 Resources

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s