Hi guys and welcome to part 19. In the last part we created a button to build ships which creates a ship in a random location within the bounds of a solarsystem. In this part we will:
- Make it so we can’t click the build ship button in the galaxy view.
- Link our ship data to the GameObjects.
- Destroying the ship objects so they are not in the galaxy view.
1. Disabling the Build Ship Button
Open up the SolarSystem script and declare a public button called buildShipButton. Assign the button we made last time to this button in the inspector.
In the OnEnable method in the SolarSystem script set the button so it isn’t intractable (just like our galaxy button). We do this because our game starts in the galaxy view :).
Then we need to make it interactable again in the CreateSolarSystem method.
Finally we need to return it to being unclickable in the DestroySolarSystem method.
Now if we hit play we can only build ships in the solar system view.
2. Linking Ship Data to Objects
Open up our FleetManager script. We need to add a Dictionary with key Fleet and value GameObject (like our starToObjectMap dictionary in our Galaxy script but with different key and value types). Call this fleetToObjectMap.
We need to set this equal to a new dictionary in the Start method.
In the BuildShip method after creating the cube add fleet and shipObject to fleetToObjectMap.
The data is now linked to the GameObject. However we cannot see or do anything with this yet. Don’t worry I will cover this in a few parts time when we look at displaying fleet/ship information :).
3. Destroying Ships
In the BuildShip method after we have set the position of the ship, set the parent of the cube to the player object.
Before we continue lets just quickly discuss what we want to happen here. We want the ships to disappear from the game when we enter the galaxy view. However unlike the stars/planets we will not be destroying the ships. Instead what we will do is just disable them. We may comeback and deside to destroy them later however for now I thing just disabling them will be fine. I say this because most 4X games have a fleet or ship limit. I am planning on implementing a fleet limit in this game of 30 fleets. That is not a huge number and therefore I think we can afford to have them kicking around int he computer memory while they are not on the screen.
Having said this however we will need a method to handle destroying ships when they die.
So, after BuildShip, create two public methods. One called DestroyShip and the other called DisableFleets.
We will leave DestroyShip blank for now and cover that later when we implement a combat system.
In DisableFleets disable the player object (this will need to change in the future when we add more objects to the player object which we may not necessarily want to disable but it will do for now)
We now need one last method and that is EnableFleets. It will look exactly the same as DisableFleets but will set enabled to true instead of false.
So how do we get these methods working? Well disabling is easy we just add it to the Galaxy View Button OnClick() menu in the inspector.
But how do we enable them again?
We will need to reference the FleetManager script in our SolarSystem script. For this we will use the Find command. We will use this instead of an instance because we may want to have our AI have FleetManager scripts (i.e there may be more than one in the game).
In the SolarSystem script, declare a new FleetManager called fleetManager. In the OnEnable method use the Find command combined with GetComponent to find the FleetManager script on the Player Object.
In the CreateSolarSystem method call EnableFleets.
Now if you hit play and build some ships they will disappear in the galaxy view and reappear in the solar system view :).
There is an obvious issue which is the fleets appear in any solar system you click on and really they should only appear in the solar system they were built in. We will address this in a future tutorial. For now enjoy filling the galaxy with cubes!
In the next part we will fix a bug which was helpfully pointed out by Infinitea. Turns out on some systems (seems to be ok on my surface but I get the same bug on my PC) where the galaxy respawns differently when some solar systems are selected. I will address this issue and also improve our galaxy respawning efficiency :). After that we will return to building ships and look at how we can make them cost something to build.
Scripts_for_part_19 (zip file)