Let’s Make a 4X Space Game – Part 26 Setting Up a Turn System

Hi Guys, welcome to part 26. Firstly, sorry for the long delay in getting this part out, life has been very busy. In the last part we sorted out our ship building UI so we can add ships to the build cue on our one and only star base (so far) and remove them again. In this part we will start setting up a basic turn based system.

Open up the project and add a new button to the scene. Rename it Turn Button and change the text to “End Turn”. Place it anywhere you want on the canvas, however I will be adding mine to the top centre of the screen.

Part026Pic003
Turn Button
Part026Pic002.JPG
End Turn Button In Scene

We now need a script to go with this button, so add a new scrip to the Galaxy Manager object (we may make a new object later and change this scripts location) and call it TurnManager.

Part026Pic001.JPG
TurnManager Attached to Galaxy Manager

We will need to make a singleton for this so add an Awake method and a public static TurnManager, called turnManagerInstance, to the script. In the Awake method make turnManagerInstance equal to this.

Part026Pic004.JPG
Singleton for TurnManager

We can also get rid of the Update method.

Next add a new public void method called EndTurn, it doesn’t need anything passed to it at the moment.

Part026Pic005
EndTurn

This method will handle all our functions we need to call  at the end of the turn, but first we should connect it to the Turn Button. In the Turn Button OnClick menu in the editor add this function.

Part026Pic006.JPG
Adding EndTurn to OnClick()

So what are we going to need to put in the EndTurn method? There will probably be a lot of things by the time we are done with the game, it is going to have to handle all the ship movement, production cues, adding/subtracting resources etc…

Let’s just keep it simple for now. First we should make sure the end turn button can only be pressed once and then the end of turn functions are process (in the future we may make some of these co-routines but for now they will just be standard functions). To do this add using UnityEngine.UI at the top of the script and then add a public button called endTurnButton. Add our Turn Button object in the inspector.

Part026Pic007.JPG
Adding Public Button
Part026Pic008.JPG
Adding Turn Button

In the EndTurn method make the button un-interactable using the Button.interactable method.

Part026Pic009.JPG
Making the Button Un-Interactable

Press return a few times so there are a few lines gap and then make the button intractable again.

Part026Pic010.JPG
Making the Button Intractable Again

Now the button will only be able to be pressed once while all the turn calculations and movements are being made. If we hit play and press the button now nothing really happens though because it is disabling and enabling the button almost instantaneously.

We should add a UI element to show which turn we are on. Add a Text UI element to the scene making it a child of the Canvas. Rename the object Turn Text and set the text to “Turn: 0”. Change the colour to white so it stands out against the black background. Move it anywhere you want on the screen, I am putting mine under the Turn Button. Change any of the other settings to what ever you want and maybe consider putting the text on a panel so it really stands out.

Part026Pic014
Text Settings
Part026Pic013
Text in Scene

In the TurnManager script add a new public void method called UpdateTurnText. Do not pass it anything.

Part026Pic015
UpdateTurnText Method

This method will handle updating the UI text. To do this we need to know what turn we are on and have a reference to the UI. At the top of the class add a private int called turnNumber and a public Text called turnText. In the inspector add the Turn Text object to this slot.

Part026Pic016.JPG
turnText and turnNumber
Part026Pic017.JPG
Turn Text

In the Start method make turnNumber equal to zero (this will need to be changed later for when we are loading saved games but it will do for now).

Part026Pic018.JPG
Initialising turnNumber

Now in the UpdateTurnText method increment turnNumber by one and set the text equal to “Turn: ” + turnNumber.

Part026Pic019.JPG
Updating Turn Text

Call this method in the EndTurn method just before enabling the Turn Button again.

Part026Pic020.JPG
Enter a caption

Great so now we have a turn button and some UI to tell us what turn we are on in our game. Before we end this part of the tutorial we will add a little bit more code in preparation for the next part.

In our PlayerManager script we need to add a public list of owned planets. That way we can easily find all the planets the player owns and loop through them to do things like add production and resources.

Part026Pic021.JPG
List of Owned Planets

Add an Awake Method to the PlanetManager script and initialise the new list.

Part026Pic022
Initialising owedPlanets

Back in our TurnManager script add a new void method called ApplyProduction (no need to pass it anything). Add a for loop in this method for each planet in ownedPlanets.

Part026Pic023.JPG
For Loop

We will be taking the planets production value and adding it to the first ship in the starbase cue (if the planet has a starbase and has a ship in the cue). See if you can work out how to do this yourself and I will give you the answer next time :).

That concludes this part. In the next part as you probably guessed we will be setting up our ship production cues so that it takes several turns to build a ship.

Scripts  (Pastebin)

Part 27 Ship Production using Turns

Advertisements

5 Comments »

  1. Thank you Tom. This is a fantastic tutorial. You’re very suited to training, straight to the point, well documented. I look forward to following you through this tutorial.

  2. Shadowplaycoding, thank you so much for posting these tutorials. I find they are better than those found are udemy.com and easier to follow along. Keep up the good work! Looking forward to your next tutorial.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.