Sunday, December 7, 2014

Unreal Engine 4 Dev Update #5: HUD based Command UI, Grid based movement system & Dynamically Spawning Grids

Welcome back to the blog, it's time for another update. Starting with this post, I'll be posting only the details regarding the new development in the dev updates. I'll post tutorials for some of them as separate posts. I'm doing this since I don't have time to write the tutorials for each and every one of my updates. So I'll be making tutorials only for those topics which aren't available elsewhere. With that out of the way, let's get to this week's dev update. I'm already behind schedule by a few posts.

In my last update, I had written about how to implement keyboard input based camera rotation for Top Down games. You can find it here:

Unreal Engine 4 Dev Update #4: Camera Rotation for Top Down Games

Moving on to the updates, these are the main changes that I have made:
HUD based Command UI
Following my last update, I worked on implementing a basic HUD based Command UI. Since I hadn't worked on HUD Blueprints before, I looked into the HUD based Blueprint examples available in the UE4 Marketplace. After tinkering around with Blueprints, I came up with a basic Command UI. Since I'm trying to work towards a Turn-Based shooter, I added two buttons that serve the basic purposes - Movement and Firing. The 'Move' button basically ask the player character to move to the location under mouse click, while the 'Fire' button fires a projectile in the direction of the mouse click. It's pretty basic stuff, you can see it in action in this video:
Grid based movement system
Next up, I implemented a Grid based movement system. Initially my plan was to manually add box meshes around the level and then concentrate on the player movement separately. But since it was too much redundant work, I decided to spawn the meshes dynamically at level start. Well, not strictly dynamic since I hard coded the boundary of the playable area. Using the boundary data, I created an array by dividing the total game space grid by 100. For each of these 100x100 grids in game space, an element was added to the array. In order to store both X and Y coordinate data in the array, I used two 'For loops' while making the calculations. The vector data thus obtained was added to the array at level start, thus giving me a list of locations that the player can move to. As a temporary measure, simple box meshes are spawned along the grid at level start to depict the grid system. The data from the grid array is used every time the player character is issued a move command. Based on the click location and the array data, I was thus able to restrict the player movement to the grids. Here is a short video demonstrating the system:

Dynamically Spawning Localized Grids
After the UE4 update from v4.4 to v4.5, my level got a massive frame rate hit. With nearly 1600 grid meshes at level start, my system couldn't even run it at 10 fps. Hitting up the forums and UE4 Answer Hub didn't get me a satisfactory solution. As a result, I decided to move on to localized grids instead of an all-encompassing grid system. Instead of spawning the grids at level start, I switched to dynamically spawning localized grids based on player's mouse input. This helped reduce the number of grid meshes from 1600 meshes to just 9 meshes, and thus helped bring the frame rate back to speed. It also helped in keeping the game space clean. Now every time the player clicks on the floor, grids are spawned in the region corresponding to and surrounding the mouse click location. I'll leave you with a video for the same in action: 

Alright that's it for this update. If you're interested in seeing more of my work, feel free to check out my Youtube channel, as I generally upload my work over there before the weekly blog update.