Wednesday, December 28, 2016

Tower Defense Starter Kit Tutorial: How to create custom Enemy Units

[The following information is based on the v1.5 edition of Tower Defense Starter Kit & hence may not remain entirely relevant in later versions. For more information about the toolkit, check out the official support thread in the Unreal Engine forums:]

The Tower Defense Starter Kit comes equipped with four types of Enemy AI units that all derive from the parent blueprint 'BP_EnemyAI_Parent'. As the name suggests, this tutorial goes over the process of adding your own new customized AI units into the mix.

1. First add a new entry to the enum 'ECreepType. I'm going to call it Exploder as I want this new AI unit to explode when destroyed, thus dealing heavy damage to all Tower Guardians/Towers caught in the explosion.

2. Now create a new child blueprint derived from the 'BP_EnemyAI_Parent' class. By inheriting from this class, our new AI unit will have all the basic capabilities that will be required of it including damage & threat response mechanisms, movement logic, etc. At this point, all you have to do is to set the skeletal mesh & animation blueprint from it's components tab based on your requirements.

3. Set values for the default 'User Defined' attributes of this unit & add new variables named 'Explosion Damage' & 'ExplosionDamageRadius' as shown in the screenshot below:

4. Now override & extend the 'End Play' event of the parent class by adding the explosion damage application logic as shown in the previous screenshot.

[Note: Steps 5 & 6 are only required if you intend to use the Group Based Wave Generation System from the Wave Manager.]

5. Head over to the 'BP_EnemySpawnPoint' blueprint & add a custom event similar to the other SpawnCreepBatch events present in it's Event Graph. Check out the next screenshot to see how it's done. Basically this takes care of the creep spawning mechanism, once the wave generator has decided to spawn this particular type of creep.

6. Now the Wave Manager needs to know that it can call this Event to spawn a batch of Exploder bots. So make the changes marked in red in the next screenshot, within your wave manager blueprint:

7. All that's left now is to add our new Exploder bot into the Wave Data Array as shown in the example below:

With that, we have covered the steps involved in creating a very basic Enemy AI template. All classes derived from the EnemyAI_Parent class will be capable of engaging in melee combat with the Tower Guardians. If you want your custom AI bot to engage in ranged combat with the Towers as well, just follow the logic implemented within the BP_EnemyAI_Ranged blueprint.

Wednesday, December 21, 2016

FPS Tower Defense Toolkit Tutorial: How to use Towers without Tower Bases

[The following information is based on the v1.9 edition of FPS Tower Defense Toolkit & hence may not remain entirely relevant in later versions. For more information about the toolkit, check out the official support thread in the Unreal Engine forums:]

The FPS Tower Defense Toolkit lets players construct mazes using Tower Bases to control the paths of AI bots. The same Tower Bases also act as platforms for placement of Towers, which help prevent the AI bots from reaching the Power Core. However since most Tower Defense games allow players to directly place towers on the ground instead of a separate platform like Tower Bases, I've received a few queries about how to implement the aforementioned design with this toolkit. This tutorial goes over the modifications required for achieving the same.


1. Set the variable 'TowerZOffset' to 0.0. This variable determines the offset of the towers along the z axis relative to the ground location. The offset ensured that towers were spawned physically on top of the Tower Bases. Since we're not going to use Tower Bases in this particular scenario, a value of 0.0 ensures that the towers are spawned directly on top of the ground.

2. Now head over to the 'Destroy Tower' function & replace the value of 'Grid Status' input parameter for the 'Update Grid Data Array' function call from 'Tower Base' to 'Empty' [check screenshot below]. Having it set to Tower Base ensured that when a Tower is destroyed, the grid status would revert back to the Tower Base on which it was placed. In our case, we can directly set it to Empty as we're not dealing with Tower Bases.

3. Remove the function call 'Add Tower Base Entry to Selected Tower Data Array' within the 'Create Selected Tower Data Array' function & connect the nodes as shown in the screenshot below:


1. Within the Construction Script, add the segment marked with red border in the next screenshot [check below]. This code ensures that the nav paths are rebuilt every time a tower is placed in the level. Since towers used to be placed on tower bases, we only had to do this when tower bases were spawned. And now since towers are going to be placed directly on the ground, we're calling the same functionality here as well.

2. In the Event Graph, add a new 'Event End Play' node & add the segment marked with red border in the screenshot below. This performs the same function as last step, the difference being that we're requesting the nav mesh to rebuild itself everytime a tower is destroyed.

3. Now add a 'Box collision' component to this class. This will act as the nav modifier volume that can block enemy AI paths, thus forcing them along alternate paths. Update the parameters of this component from it's details panel to match the information specified in the following screenshot.


1. Now go to the folder 'Holographic Towers'. You should see separate holographic tower classes for Tower Base & Towers. All holographic towers currently inherited from the 'BP_AttackTowerHolo_Parent' blueprint. 

Now the easiest way to modify the Holographic Towers based on our requirements would be to have them inherit from the 'BP_TowerBaseHolo' class. We choose to do this because this class already checks if the tower/towerbase can be placed on the focused grid cell based on the availability of paths from the enemy spawn points to the power core. And with towers being placed at the ground level, this has become a requirement for us. 

However, we still need to make a couple of minor changes within the blueprint. Go to 'Evaluate Tower Resource Requirements' & replace it's logic with the nodes shown in the screenshot below. Basically we're replacing the check for availability of tower bases with a check for availability of tower points.

[Note: You can copy paste these nodes from the same function implementation in  'BP_AttackTowerHolo_Parent' blueprint.]

2. Within the 'Create New Actor Transform' function, replace the 'TowerBaseZOffset' with 'TowerZOffset' as shown in the screenshot below:

3. Now create child blueprints from the 'BP_TowerBaseHolo' class for all the Towers. For example, I'm going to create one child blueprint & name it BP_LaserTowerHolographic. Within the blueprint, we need to set the 'Tower' mesh component to be that of the associated tower. So in this case, I'm just going to set the Laser Tower mesh to this component.


1. The 'Tower Data Array' variable contains user specified information about all the Towers. If you expand it's elements, you would notice that the first element is a Tower Base entry. Delete this element first.

2. Now expand the array element associated with the Laser Tower & set it's 'TowerHoloClass' parameter as the new holographic laser tower we created a while ago [check next screenshot]. Similarly change the same parameter for every other tower element within the array.

Alright, so now you should be able to use Towers without the need for Tower Bases in your projects. Just make sure to remove references to the Tower Bases & associated enums/blueprints if they're not required. And finally here's a sample screenshot with the final result:

Top Down Stealth Toolkit Tutorial: How to create customized Laser Security Systems

[The following information is based on the v1.4 edition of Top Down Stealth Toolkit & hence may not remain entirely relevant in later versions. For more information about the toolkit, check out the official support thread in the Unreal Engine forums:]

The Laser Security Systems in Top Down Stealth Toolkit function as an auxiliary layer of AI Threat Detection by alerting all nearby patrol bots upon being intercepted by the player. The underlying logic is a simple combination of Timeline based movement & Collision Overlap checks. These actors support drag & drop functionality from the editor & can be easily customized using the variables exposed to the editor from the blueprints. This post goes over the impact of the aforementioned parameters on the working of Laser Security Systems.

1. Length: The Laser Security System uses a box collision component to determine if the player is within it's range. The length parameter determines the total length of this box & also controls the area covered by the laser particle system.

2. Max Height: As described in the tooltip, this variable determines the max height up to which the lasers move relative to the default position at which the actor has been placed in the level.

3. Player Height: The Player Height parameter is used to control the color of the laser based on it's position relative to the ground. Red color indicates that players passing through the laser will trigger an alarm while white color indicates that the laser tracker has moved up beyond the player's height & thus enable safe passage below it. This can be quite useful in top down games, as perception of the location of actors that move along the z plane may not always be intuitive to the player.

4. Alarm Timer: The Alarm Timer value determines how often the alarms are triggered while the laser system has an active target. Since the range of alarm noises are limited, having a timed alarm system can improve the chances of alerting nearby AI patrol bots.

5. Movement Timeline Starting Time: This parameter determines the starting location of the laser along it's movement trajectory. It has a range of 0.0 to 4.0, with 0.0/4.0 representing the starting position [i.e. the location at which the actor was placed] & 2.0 representing the highest position along it's movement trajectory. Anything between 0.0 & 2.0 implies that the laser will have an upward motion going towards the highest point along the trajectory, while a value between 2.0 & 4.0 means that it will have a downward motion going towards the lowest point or the starting position along the trajectory.

Tuesday, December 20, 2016

Tower Defense Starter Kit Tutorial: How to create new Global Abilities

[The following information is based on the v1.5 edition of Tower Defense Starter Kit & hence may not remain entirely relevant in later versions. For more information about the toolkit, check out the official support thread in the Unreal Engine forums:]

The Tower Defense Starter Kit is equipped two default inbuilt Global Abilities: Airstrike & Tower Guardians. This tutorial goes over the steps involved in creating & adding a new custom Global Ability.

1. First we need to add a new entry to the enum 'EGlobalAbilities' to depict our new custom Ability. I'm going to add an entry named Shockwave, & this particular sample ability will reduce the health of all enemy units in the level.

2. Now we will create an actor component that contains the core functionality of this ability. Basically these ability components will be added to an actor named 'Global Ability Deployer' [which as the name suggests acts as a container for the component], when the ability is activated by the player at runtime. 

As shown in the screenshot below, I've added the logic for retrieving references to all enemy bots in the level, & then reduce their health by a certain amount. Once the job is done, the component requests the owning actor [Global Ability Deployer as mentioned above] to destroy itself. We're doing this because the deployer comes into action only when the ability has been activated.

3. With the custom enum & actor component set in place, it is now possible to add a new entry into the Global Abilities Array within the BP_GameInstance class. For this new member of the array, set the 'AbilityType' parameter to the new enum entry we created earlier; the 'AbilityComponentClass' to the new custom actor component; 'AbilityNameAbbreviation' to the text displayed on the Global Ability Button; 'RechargeDuration' value to the amount of time it takes for the ability to recharge; & finally the 'Unlocked' parameter [it does not do anything at the moment & is reserved for potential future uses] to true. Here's a screenshot depicting the array entry for my new Shockwave ability:

The In-Game HUD takes the data from this array & dynamically displays the Global Ability buttons based on the specified information, thus reducing the need to make any changes to the associated HUD blueprints when abilities are added or removed.

4. Now all that's left is to tell the Global Ability Deployer to handle this new ability. As depicted in the screenshot below, the Deployer will have information regarding the type of ability activated by the player. Using this information, it adds the associated ability component to itself. So when the switch case encounters the Shockwave ability, we just have to add our new Shockwave ability component.

Wednesday, November 23, 2016

Tower Defense Starter Kit Tutorial: How to add new levels to the Level Selection Menu

The Tower Defense Starter Kit comes equipped with a level selection menu that enables players to choose from a list of levels as shown below:

The toolkit contains three playable maps & one main menu map by default, & supports usage of new custom maps as well. The following steps go over the process of adding your own custom levels into the toolkit:

1. Create a new custom level from the Content Browser.

2. Open the 'BP_GameInstance' blueprint within the Blueprints folder & select the variable 'LevelDataArray'. It should have four entries by default: one for the main menu & three for the sample maps. To add our new custom map, first add a new element to this array.

Each element of this struct array contains the following attributes: ID, ScreenName, FileName, Image, & Unlocked. The ID is used to set the level number, starting with '0' for the main menu level. It is not used for any calculations & serves only to provide a means for classification of levels. The ScreenName determines the name that would be displayed for the corresponding entry in the level selection menu. The FileName is used to store the address of the level asset. The Image is used to display a preview shot of the map within the level selection menu, & the Unlocked attribute determines if the level would be unlocked by default. If it isn't, it will be unlocked, as soon as the player completes the level linked to the previous entry within the Level Data Array.
3. Fill out the attributes mentioned above for the new level data element. Make sure to copy the name of the new custom map asset within the content browser & paste it into the FileName attributes. Here is a sample entry for a new level that I created:

4. Now close the project & delete the 'Saved' folder within the project folder. This ensures that the changes made to the Level Data Array do not get overridden by information specified in an existing save game file.
5. Now open the project, open up the main menu level from the 'Maps' folder within the Content Browser, & launch an instance of the game. The level selection menu will now display information about the new level, provided that it's been set to Unlocked. Here is a sample screenshot with the new custom level:

Sunday, February 7, 2016

Unreal Engine Tutorial: How to use curves to control the prices of in-game items

Hi, welcome back for another tutorial. This time around, we're going to learn how to use curves to control the price of an item over time. I'm going to start with the Third Person template that is available by default in the Unreal Engine. 

First of all, we need to create a Float Curve along which we will plot the price of the item relative to time. To do so, just right click on the content browser, then select Miscellaneous >> Curve & choose the Float Curve option. Now open it up & add some points in the graph to define a curve. This can be done by right clicking anywhere on the graph & selecting 'Add key to curve'. Also in order to set accurate values, just click on the point & set 'Time' & 'Value' according to your requirements on the top left corner. I'm using the following values for this tutorial:

Time = 0; Value = 1
Time = 2; Value = 2
Time = 4; Value = 1

You may notice that it will generate a very sharp curve by default. In order to change this behavior, just try out the other interpolation methods available on the menu bar in the top. 

Next I just setup the scene with a desk and a couple of glowing cube meshes on it. They're all purely for visual purposes. There's really no need to do so.

Now we're going to create an actor blueprint named 'BP_ItemManager'. Within this blueprint, we'll add script to control the price of items based on the game time. But before that let's create a struct [Struct_ItemAttributes] to list the properties of the items. I've added a Name parameter of type 'String' plus Cost & GameTimeMultiplier parameters both of type 'Float'. The Game Timer Multiplier will be used to control the relation between the price of an item & game time by multiplying the game time with this value.

Now open the Item Manager blueprint & create an array of type Struct_ItemAttributes. Then give some default values to the different items. For example, these are the elements of my Item Array:

Element 0 >> Name = Green; Cost = 100; GameTimeMultiplier = 1
Element 1 >> Name = Orange; Cost = 100; GameTimeMultiplier = 3.6

The higher GameTimeMultiplier value of the orange item means that it's price will vary along the curve at a much faster pace than the green item.

We also need another variable of type Float Curve named 'PriceCurve' to hold a reference to our price curve. Make sure that it's default value is set to 'Price Curve'. 

Now we're going to create a function that can retrieve the curve float value at any point of time based on the game time as well as gametimemultiplier. First we find out the time range for the price curve. Then we do a modulo of the game time divided by this time range. The modulo operation will make sure that we will always get the required curve value, even if the game time has exceeded the max curve time plotted in the graph, thus essentially using the curve like a repeating wave pattern. The gametimemultiplier will be taken as an input parameter while the curvefloatvalue will be returned as an output parameter.

In order to retrieve the prices of all the items in the item array, we're going to add another function in the Item Manager named 'RetrieveCurrentPrice'. This function will loop through the items array, call the 'GetCurrentCurveValue' by passing in the GameTimeMultiplier from the struct & then multiply the returned curve value with the cost element. It then appends the result with the item name & prints it on to the screen.

All that's left now is to call this function from the player character. We can use 'Get all actors of class' at event begin play to store a reference to the Item Manager actor. When we press the required input button, we'll use this actor reference to execute the 'RetrieveCurrentPrice' function.

If we go ahead and the press the F button now, the function will print out the price of both items based on the game time at that point.

Alright, so that's it for this tutorial. I'll be posting a new Unreal Engine Diaries episode soon to continue on with Part II of the Basics of Particle Systems series. Until then, goodbye & have a great weekend. :)

Sunday, January 31, 2016

Unreal Engine Tutorial: Create Circular Ring Material without Textures

Hi, welcome back for another tutorial. It's been quite a while since I put up a tutorial as I've been busy working on toolkits for the Marketplace. One of my earlier tutorials [link: Unit Selection Decals for RTS Games] had focused on how to create ring materials using textures, as well as using them to display unit selection decals for RTS games. However, a drawback associated with using textures was that it prevented the user from changing the inner & outer radius of the ring. And during prototyping stages of a product, it's far better to implement structures that can be changed easily from within the engine so that you don't have to create a new asset from an external package every time you need to test out a new idea. I've found this approach quite useful while working on my own products. Hence, this tutorial focuses on creating a ring material that can be adjusted both from within the material editor as well as dynamically at runtime.

Alright before we get started, I'm going to use the default Top Down Template that comes free with the Unreal Engine, as the top down view is a good example for showcasing the uses of a ring material. We're going to be creating a decal material here. So the first step is to create a new material from the content browser & adjust it's default properties from the detail panel. Under the 'Material' category of the details panel, set 'Material Domain' to 'Deferred Decal' & 'Decal Blend Node' to 'Emissive' as shown in the screenshot below.

Now right click within the material editor & create two 'RadialGradientExponential' nodes. This node is normally used to produce radial gradients, but we're just going to use them to create two circles. The gradient effect can be negated by increasing the density of both these gradients. For that, just create a new constant expression & set it's value to 100. Connect it to the 'Density' input of both the gradient nodes. Now with that taken care of, we need to specify the outer & inner radius of the ring material. Create two scalar parameters 'OuterRadius' & 'InnerRadius' with the values 0.5 & 0.4 respectively. Connect these two to the 'Radius' input of the gradient nodes. Now subtract the inner radius gradient node from the outer radius gradient node & connect this to the 'Opacity' parameter of the material.

We have thus implemented the core logic for the ring material. Now on to the emissivity & opacity to wrap it up. First, create a vector parameter named 'BaseColor' & a scalar parameter named 'Emissivity' with the default value of 2. Multiple both of them & then multiply it's output with the result of the subtract node we added for the gradients. Connect the result to the 'Emissive' parameter of the material & we have created a circular ring material.

This next step is not necessary, but I created two material instances using this material: one for the unit selection decal & the other for displaying attack range. 

Alright, now select the Top Down Character & add two 'Decal' components to it: a Unit Selection Decal & a Range Display Decal. Set the Z value of both to -98 cm & pitch value to -90 degrees. The pitch value is changed because the default orientation of the decal component spawns it in an invisible plane. Since the default dimensions are too small for our needs, we can scale up the Unit Selection & Range Display Decal to (1, 50, 50) & (1, 400, 400). As you may have noticed, we do not scale up the X value. This is because we don't want the decal to be drawn on surrounding objects. Setting it to one allows it to behave in such a way that it gets cut off by the obstacles in it's path instead of painting over them.

Now if you play in the editor, you should be able to see both the unit selection & range display decals display & move along with your character as shown in the screenshot below:

Alright, so that's it for this tutorial. I'll be continuing on with the Unreal Engine diaries series for concepts that do not require a whole tutorial. But I have some ideas for new tutorials as well & if possible, will try to upload them some time during this coming week. Until then, goodbye & have a great weekend. :)

Wednesday, January 6, 2016

Unreal Engine Diaries #11: Nav Mesh Configuration

[This episode of Unreal Engine Diaries focuses primarily on nav mesh configuration, nav areas & configuration of agent types.

Source: Unreal Engine 4 AI Support Twitch Broadcast with Mieszko:]

1. The Recast NavMesh has a couple of attributes named 'CellSize' & 'CellHeight' under the 'Generation' section in it's details panel. Together they determine the resolution of the nav mesh & lowering these values can create more precise nav meshes. It could be especially useful when there are lot of holes in the nav mesh due to the surface properties of the terrain. However, lowering them also makes the nav mesh calculations more expensive.

2. If runtime nav mesh generation is enabled, it would be best to set the 'Tile Size' attribute of Recast NavMesh to the minimum viable amount.

3. The 'Min Region Area' parameter which can also be found under 'Generation' section of Recast NavMesh can be increased to get rid of small nav mesh islands that are isolated from the rest of the nav mesh regions.

4. When moving around objects that can influence the nav mesh with the runtime nav mesh generation enabled, it is far more efficient to use the actors as dynamic obstacles (with custom nav area settings) when compared to moving around actors in their default state. For static meshes this can be done from the static mesh editor as shown:

Just tick the 'Is Dynamic Obstacle?' checkbox & set the nav area type from the dropdown right above it. 

Alternatively, the same can be set up from the collision components in actor classes as shown below: [same procedure as mentioned in the static mesh editor workflow]

5. If you have tall characters in your game that are having issues with the nav mesh pathing, go to 'Navigation System' category under Project Settings. Add a new 'Supported Agents' & then try increasing the z value of 'Default Query Extent' of the new agent. 

On the other hand, in order to create separate nav mesh pathing for the bigger AI characters, add a new element to 'Supported agents' array & specify custom agent radius, query extent, agent height, etc. Build paths again to see a new nav mesh recast in the Scene Outliner tab. The 'Enable Drawing' parameter under Recast Navmesh can be ticked On to visualize the new nav mesh. It seems that the navigation system will automatically determine which agent type is suitable for the bot based on it's collision capsule size.