Applying the correct damage in combat

In battle, you will notice that the enemy and the player both do 10 points of damage no matter what. The current attack power and defense do not seem to be calculated. This is because, in Chapter 3, Exploration and Combat, when we created the combat actions, we hardcoded the damage to be target->HP -= 10, which means that no matter who is attacking, they will deal 10 points of damage to the player. We can easily fix this to use the actual stats of enemies and players by navigating to Source | RPG | Combat | Actions and opening TestCombatAction.cpp. Find target->HP -= 10; and replace it with target->HP -= (character->ATK - target->DEF) >= 0 ? (character->ATK - target->DEF):0;.

This is a ternary operator. When a target is attacked, whether it is a party member or an enemy, the target's HP will go down by the attacker's attack power minus the target's defense power only if this result ends up being the same or greater than 0. If the result is less than 0, then HP will default to 0. When you are done, void TestCombatAction::BeginExecuteAction( UGameCharacter* character ) will look like this:

void TestCombatAction::BeginExecuteAction( UGameCharacter* character )
{
  this->character = character;

  // target is dead, select another target
  if( this->target->HP <= 0 )
  {
    this->target = this->character->SelectTarget();
  }

  // no target, just return
  if( this->target == nullptr )
  {
    return;
  }

  UE_LOG( LogTemp, Log, TEXT( "%s attacks %s" ), *character->CharacterName, *target->CharacterName );

  target->HP -= (character->ATK - target->DEF) >= 0 ? (character->ATK - target->DEF):0;

  this->delayTimer = 1.0f;
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.234.188