Enemy AI Constructor

Unreal Engine 4 blueprint system which allows you to create enemies for your single player games

Platforms:

All

Engine versions:

4.12+

Enemy AI Constructor allows you to create enemies with various behavior in minutes. It is 100% done in blueprints. There are three enemy archetypes you can use for your enemies as a base

  • Melee — chases and attacks player in melee;
  • Range — attacks the player and runs away from the player if he/she comes too close;
  • Magician — has the same behavior as range enemies, but instead of using projectiles they will use a magic spell as an autoattack. Magicians can be customized to use multiple skills in various situations. For example, they can cast Ring of Light, which will damage the approaching player.
  • Space
  • Space
  • Space
  • Space
  • Space

You can customize parameters, such as: name, description, HP, MP, damage, movement speed (separate for patrol and combat mode), radius in wich enemy will detect the player, and so on.

You can set enemies to patrol the whole level or a certain area, or they can stand still and wait for the player. You can create social enemies (i.e if you attack one, the rest of the group will start to attack you as well).There is a blueprint to spawn specific or random enemies. You can define, how many and what type of enemies will spawn, and place the customized spawner on your level.

In Enemy AI Constructor you will find basic skills for your enemies. To show what you can achieve with AI constructor, we made several skills, including a magic projectile and a meteor, falling from above. Player also can use skills and do damage to the AI enemies.

Also, there is a widget, which displays enemy name, description and current health points.

Video Tutorial

Documentation

Specifications

  • BP_Enemy (Blueprint) — Enemy Class;
  • EnemyController (DetourCrowdAIController) — Controller;
  • EnemyBlackboard (Blackboard) — Contains global parameters for all Enemies;
  • NPCStats (Struct) — Structure, where all Enemies parameters are stored;
  • NPCTable (Datatable) — Enemies table, where all parameters are set;
  • BFL_UtilitiesLibrary (FunctionalLibrary) — library, which contains utility AI logic methods;
  • WB_TargetWidget (Widget) — Widget, shows Enemy’s information: name, HP and description;
  • BP_CharacterBaseClass (Blueprint) — Parent character blueprint, contains interaction logic between Player and Enemies;
  • BP_Spawner — unite Enemies into groups;
  • BP_Skill (Blueprint) + Requirements (Struct) + SkillTable (Datatable) + 4 skills logic;
  • BP_Weapon (Blueprint) + BP_Projectile (Blueprint) + Bullet projectile example (Blueprint);

Troubleshooting

AI stops working after you place certain number of Enemies on the map

To fix this, edit Max Agents parameter in Edit → Project Settings → Engine → Crowd Manager.

Can’t go through doors on Showroom.umap

To fix this, enable dynamic NavMesh generation: Edit → Project Setting → Navigation Mesh → Runtime Generation → Dynamic.

How to add Enemy AI Constructor to the new Project

To add new functionality to your project, it is necessary to establish interactions between Enemies and the Player. There are two methods:

  • To make BP_CharacterBaseClass parent to character blueprint in your project, or to use BP_Player (from Showroom map) as character blueprint;
  • To reconfigure BFL_UtilitiesLibrary methods to character blueprint in your project.

Method #1

  • Open File → Reparent Blueprint in your character blueprint;
  • Choose BP_CharacterBaseClass from the list.
  • If you use Event Begin Play in your character blueprint, you should call Parent: Begin Play.

You may need to remove extra components, such as Camera duplicate, if something appears after Reparent.

Method #2

Open Constructor\Blueprints\Utility\BFL_UtilitiesLibrary and recover the following library methods:

  • GetPlayer must return the character;
  • GetPlayerHitBox must return the Character component with it’s HitBox (when projectile or melee attack overlaps it, Character will take damage);
  • GetPlayersTarget must return Actor from Characters TargetActor variable. If you don’t have it or it’s analog — make the new variable with ActorReference type;
  • SetPlayersTarget must set Character’s TargetActor variable;
  • GetPlayersTargetWidget must return TargetWidget, which displays information about Enemy in the target;
  • IsPlayerDead? must return true if Character is dead;
  • GetPlayersState must return Character’s current state (HP, Stamina etc.) by the name of each parameter;
  • GetPlayersStateMax and GetPlayersStateMin must return minimum and maximum of Character parameters (HP, Stamina etc.) by name;
  • SetPlayersState must set Character parameters by name;
  • allowDamage, allowTaskChange, allowRotation should be the same.

Usually you don't need to touch those methods, but just in case here they are:

  • DistanceBetweenActorAndPlayer must return distance between the Character and Actor (parameter);
  • GetRotationToPlayer must return Rotator from Actor (parameter) to Character;
  • GetPlayersLocation must return Character location vector;

How to create new Enemy and set parameters

How to create Enemy #1

Make new blueprint as a child to the BP_Enemy blueprint.

How to create Enemy #2

Open it and change the ID in variables section. It must contain unique ID for each enemy. To check if the ID is used, just look it up in NPCTable.

Setting Parameters

All AI NPCs get parameters from NPCTable. After you create a new Enemy, you must create a new record about it in the NPCTable. Just press ‘Add a new row’ and put the Enemy ID in ‘Row Name’ and fill the EnemyBP and BegaviorTree cells.

List of parameters

  • EnemyName — Enemy name, displayed in widget, when the enemy is in target;
  • EnemyDescription — Enemy description, displayed in widget, when the enemy is in target;
  • EnemyBP — Enemy blueprint, which SpawnAIFromClass uses;
  • BehaviorTree — Enemy behavior tree, which SpawnAIFromClass uses;
  • Weapon — Weapon blueprint;
  • WeaponSocketName — Socket name, where weapon should be attached;
  • EnemyHealth — Health Points;
  • EnemyMana — Mana Point (if AI doesn't use magic spells, set it to 0);
  • EnemyDamage — damage, applied with both melee and ranged attacks;
  • DefaultWalkSpeed — Enemy move speed, when it doesn't see the player;
  • MaxWalkSpeed — Enemy move speed, when it chases the player;
  • SightRadius — If player is in this radius, AI will leave the passive mode. This radius must be greater than other radius parameters;
  • StartChaseRange — If player is in this radius, AI will start to chase the player;
  • EndChaseRange — If player is not in this radius, AI will stop to chase him. Must be greater or equal to StartChaseRange;
  • AttackRange — If player is in this radius, AI will attack him;
  • AlarmRange — If player is in this radius, AI will start to retreat (if you don’t want AI to retreat, just set it to 0);
  • RetreatRange — Radius, on which AI will seek the point to retreat to.

Instance parameters

There are some parameters, that you can change for each child AI. If you want to change them for all instances, change them in the class blueprint.

  • MaxRetreatTime — Amount of time AI can be in retreat mode (0 — unlimited). After this period of time AI will attack the player regardless the distance between the player and the Enemy AI. The time resets, when the player leaves AlarmRange radius;
  • EnemyPassiveRegenManaValue — Amount of MP Enemy regenerates each N seconds (you can set it in EnemyManaRegenTickValue). If EnemyMana = 0, this parameter will be ignored;
  • EnemyManaRegenTickValue — amount of seconds between each Mana regeneration tick. If EnemyMana = 0, this parameter will be ignored;
  • OutlineColor and BlinkColor — stencil numbers to outline Enemy in target and fill the Enemy with particular color after it has been hit. This functionality works with Outliner. If you don’t have Outliner in your project, these parameters will be ignored;
  • PatrolRadius — Patrol radius. If IsGuardian is set to true — this parameter will set radius around the spawn point. If you set it to 0, AI will stand still. If IsGuardian is false — this radius defines the radius around the AI, where AI will seek the next point to move to.
  • Patrol Waypoints — If there are BP_Waypoint elements in the array, AI will patrol from one waypoint to another. Otherwise it will patrol the whole level;

Setting AI

There are 4 stages — setting skeleton, animation blueprint, AI blueprint and behavior.

Setting Skeleton

If you want your Enemy AI to have a weapon, you should make a socket for it. To make a socket, press RMB on desired bone, choose Add Socket and enter the socket name. This name will be used in WeaponSocketName in NPCTable. You can preview any mesh in this socket, just press RMB on the socket and then Add Preview Asset.

Setting AnimBP

In Enemy AI Constructor we sync AI behavior with animation with these 3 parameters:

  • AI uses range attack — While Enemy AI fires a shot, it can’t change it’s state (i.e. to retreat);
  • AI should damage the player — Notifies, when Enemy AI is ready to attack. This parameter syncs the damage that needs to be dealt to the player in the moment of hit animation, and notifies that the damage value can be applied;
  • AI stops rotating towars the player — Notifies, that Enemy shouldn’t rotate towards the player. We use it with ranged attacks, in the moment before the shot. We just don’t want the Enemy to rotate during the shot.

Set notifies in animation, call methods (allowTaskChange, allowDamage and allowRotation) and pass them PawnOwner using TryGetPawnOwner. There is Mannequin example in the project.

BP_<NewEnemy>

Here you can change hitbox, target sphere and melee attack hemisphere.

The last step — set the type of Enemy AI. Just call IMeleeEnemy, IRangeEnemy или IMagicianEnemy after Event BeginPlay. Also, you must pass a projectile type to IRangeEnemy, and a skill used as autoattack for IMagicianEnemy.

Behavior

There are three behavior tress — MeleeBehavior, RangerBehavior и MageBehavior. If you want to create new unique behavior tree, then it’s Blackboard must be a child of EnemyBlackboard.

Mechanics

Below, you will find the description of all mechanics of Enemy AI Constructor and a list of events for each one.

Patrol

If Enemy AI is in Patrol mode, it will choose a random point on NavMesh in Radius (PatrolRadius parameter) and move to it. If IsGuardian is set to true, AI will be bound to spawn point. If PatrolRadius = 0, AI will stand still util player arrives.

Events:

  • Walk — sets the movement speed (from NPCTable) while AI is in Patrol mode.

Chase (BP_Enemy > Chasing)

If AI is in chase mode, it follows the player and tries to reduce the distance to AttackRange until player leaves EndChaseRange.

Events:

  • StartChasing — enables Chase mode;
  • EndChasing — disables Chase mode;
  • Run — sets the movements speed (from NPCTable) in Chase mode.

Melee attack (BP_Enemy > MeleeAttack)

If AI is in melee attack mode, it will attack the player in melee until he dies or leaves AttackRange.

Events:

  • MeleeStartAttack — enables and disables melee attack mode;
  • MeleeRotation — rotates AI Enemy to the player;
  • MeleeAttackDamage (method) — deals damage in melee attack mode;
  • RefreshWeapon (method) — utility logic.

Range attack (BP_Enemy > RangeAttack)

If AI is in range attack mode, it will attack the player until he leaves it’s AttackRange or enters AlarmRange. Range attacks do not interrupt, AI leaves range attack mode after attack animation has ended.

Events:

  • RangersAttack — enables and disables range attack mode;
  • RangerFireProjectile — fires projectile from the weapon;
  • IsEnemyChasing? — utility logic to enable range attack;
  • RangersRotation — rotates AI Enemy towards the player before the attack.

Range magic skill attack (BP_Enemy > RangeAttack)

This mode differs from basic range attack — weapon is not needed. AI uses magic skill as an autoattack.

Events:

  • RangersAttack — enables and disables magic skill attack mode;
  • RangerFireProjectile — fires the auto attack skill;
  • RangersRotation — rotates AI Enemy to the player before the attack.

Retreat (BP_Enemy > RetreatMechanics)

If AI is in retreat mode, it will seek and move to the most distant point from the player in RetreatRange until player leaves AlarmRange or MaxRetreatTime timer is over.

Events:

  • StartRetreat — starts retreat mode;
  • EndRetreat — ends retreat mode;
  • RetreatBlock — sets MaxRetreatTime timer.

How to change existing mechanics

If you want to change something in existing mechanics and make a completely unique logic, just override its events. For example, to make an AI Enemy which uses Meteor and Ring of Light skills we overridden StartRetreat and RangersAttack events.

If you want to completely override existing event, don’t use CallToParentFunction after Event. It’s shown on the screenshot above.