Since UE4 requires a game mode blueprint for their levels, I decided to use this blueprint as the game controller. Since all the data from the game is saved in this blueprint, I can use it to look up or change data in other blueprints through a reference to the game mode. It is also robust enough to handle levels with varying levels of ships and hex tiles so that it works regardless of the level's scenario. The game mode also handles the game's major events such as ending a turn, ending a game, and when additional ships are added to the level.
- Event Graph: The game mode is the blueprint that knows everything about the objects in the level. For example, when a player clicks a ship, that ship references the game mode in order to remove highlights on hex tiles and disable the move and attack modes of the previously activated ship . Additionally, the game mode makes object look up more efficient by storing all the player ship, enemy ship, and hex tiles in arrays; the game mode searches all the objects in the scene by class when the game starts and then stores them into arrays for easy lookup during run time.
- End Turn Function: The game mode is also in charge of the end turn functionality. This function switches the data between the enemy ship and player ship arrays. This solution allowed me to add the hot-seat gameplay without having to rewrite any of my existing code. This is because the game now treats all the enemy ships as player ships and player ships as enemy ships.
- Move Active Ship Function: The game mode also contains the function to move the ship. It exists in the game mode since I created it before I took the object oriented approach to the game's scripts. This function is called when a player chooses a new hex tile to move to. The mid point of the hex is sent as an input to this function and the activated ship moves to that point.
I used the parent ship as the parent blueprint of all the ships of the game. The attack, support, and defense crafts all have their own blueprints, however they inherit all the functions and variables from their parent - the parent ship blueprint. The only additional function the children classes have is that they use a struct to set their stats when the level loads. Each struct is unique and contains the data of each ship's stats which gets applied to the correct ship. I also set the ship models in the viewport of the child classes so that the player can distinguish each ship class from one another. The parent ship handles the rest of the functionality. Using inheritance made it easy to have unique ship classes, however I only had to edit one blueprint when I needed to change the functionality of the ships in the game.
- Event Graph: The event graph only handles click events, however each ship handles each click on a case-by-case basis. When a player clicks on a player ship (or the hex tile below it), it disables the move and attack modes of other ships and the execution goes to the Activate Ship function. If the clicked ship is an enemy ship, the execution will reduce that enemy ship's health if the player is in attack mode and if the attacking ship as enough AP.
- Activate Ship: This function 'activates' the ship by sending the ship's stats to the ship info widget to become displayed on the screen. The widget also enables the move, attack, and rotate buttons so that the player can use these features with their selected ship. The activate ship function also sets the dynamic material of the hex tile underneath the ship to blue so the player has a better idea of what ship they have selected.
- Detect Enemy Ships In Range: When the player is in attack mode, the game highlights the tiles of all the enemies the player can attack to red. This function activates those hex tiles and ships so that the ships take damage when the player clicks them. This function also removes the highlight and deactivates the enemy ships and tiles as well. The function determines what to do based on the isInAttackMode boolean input.
- Set Hex Underneath: The ship casts a line trace downward to get a reference to the hex tile underneath the ship. The referenced hex is used in the ship's blueprint to change the dynamic material of the hex underneath the ship as it moves. This function also sets the isOccupied boolean within the hex tile underneath the ship. Player's cannot move their ships to tiles that have isOccupied set to true.
- Update Hex Underneath: Whenever a player clicks a location to move a ship, this function removes the highlight underneath the old ship location and adds it to the hex tile the player clicked.
- Rotate Ship: The player can change the location their ships face by clicking the rotate right or rotate left button. This function recieves an integer input and a switch determines which direction the ship rotates. if the integer is a 0, the ship rotates 60 degrees (right) and if the integer is 1, the ship rotates -60 degrees (left). 60 degrees is a hard-coded value since there are 6 sides in a hexagon and I know that each map will always be made of hexagons.
The level in Space Command is comprised of a 6 x 11 grid of hex tiles. Each hex tile is its own blueprint. The game mode stores all these hex tiles and their data in an array so I can easily look up data about the hex tiles such as whether they are occupied, what color their dynamic material is, or if it can receive click events. The hex grid blueprint also contains algorithms to find other hex tiles around them in order to create an array of tiles the player is allowed to move to whenever they are in move mode.
- Event Graph: Similar to the event graph in the parent ship blueprint, the event graph of the hex grid detects click events. A click event on a hex tile can mean many things depending on whether or not the player clicks on an occupied tile or whether or not they are in attack or move modes. If there is a ship above the tile, an event is sent to the event graph of the parent ship to figure out what to do. If the player is in move mode and they click on an activated tile, the Move Active Ship function is called within the game mode and the player's ship is moved to the clicked tile.
- Create Near Hex Array: Fires a line trace from each cardinal direction from the center of the hex tile calling this function. This creates an array of all the objects directly adjacent to the hex tile calling this function. This function is used in the Detect Near Hexes blueprint.
- Detect Near Hexes: Uses the array from the Create Near Hex Array blueprint, finds the objects in the array that are hex tiles, activates the tiles, and changes their dynamic material so that the player knows that those hex tiles are the places they can click to move. A boolean input determines whether or not the detected hexes should be activated or deactivated depending on whether or not the player in in move mode.
Ship Info Widget
The widget displays the HUD of the game. The HUD shows the player their ship stats as well as give them buttons to interact with the functionality built into the parent ship and hex tile blueprints. The HUD was not meant to be the focus of my project, however it is extremely important to the game's design. A turn based game like this needs an adequate way of interacting with the game's pieces as well as display the appropriate information to the player when they need it. This is a bare-bones representation of Space Command's HUD and I designed it to display enough information for play testers during play test sessions. Throughout this project, I learned that it is not enough to just have buttons for actions, but it is important that the player has an interface where the they can easily take actions without stumbling over a complicated interface. Additionally the interface needs to display information and stats to the player before they take an action so they understand the consequences of their actions. The HUD and the game's interactions go hand-in-hand and the HUD needs to be designed along side with a game like this. How the player interacts with the game should be conceptualized early in the game's production cycle so that it can be improved and iterated on early.
Event Graph: The widget contains many setter functions for the variables it displays however, the bulk of the work is done in the event graph. The event graph handles four major execution chains: setting up bind events and creating click events for the move button, attack button, and both rotate buttons. The bind events allow me to call specific events within the ship info widget from any blueprints. This allows me to change ship stats like health and AP and display those changes on the HUD in real time. The click events for all the buttons are located in the ship info widget, which call functions and events from other blueprints. The ship info widget also keeps track of whether or not a player is in attack or move mode and the boolean gets toggled every time the player presses the attack or move buttons.