Skip to content

YaDamageAPI

YaDamageAPI gives the damageable the functionality of being damaged. Here are a few important concepts to understand before you start using this API.
Concept Explanation
The damageable The entities with DamageableComponent attached
The undemageable The entities without DamageableComponent attached
The immortal The damageable entities who will never die even it it's HP reaches 0. Make a damageable entity immortal using SetImmortal.
The invulnerable The damageable entities whose hit points will never decrease, which means they are immune from damage. Make a damageable entity invulnerable using SetInvulnerable.

Functions

boolean YaDamageAPI.IsDead(YaEntity target)

ReturnsReturns false if the entity is neither alive nor damageable.
Checks if the damageable YaEntity is alive. A YaEntity is alive when its HP is no less than 0. Code sample
1
2
3
local isDead = YaDamageAPI.IsDead(targetEntity)
-- Returns false if the entity is neither alive nor damageable.
print("Damageable", targetEntity.EntityId, "is dead:", isDead)
  • target The YaEntity to check

boolean YaDamageAPI.IsImmortal(YaEntity target)

ReturnsReturns false if the targetEntity is neither immortal nor damageable.
Checks if the damageable entity is immortal. Code sample
1
2
3
local isImmortal = YaDamageAPI.IsImmortal(targetEntity)
-- Returns false if the targetEntity is neither immortal nor damageable.
print("Damageable", targetEntity.EntityId, "is immortal:", isImmortal)
  • target The YaEntity to check

void YaDamageAPI.SetImmortal(YaEntity target, boolean immortal)

SERVER ONLY 
Sets the damageable entity immortal. Code sample
1
2
-- Set the targetEntity immortal.
YaDamageAPI.SetImmortal(targetEntity, true)
  • target The damageable YaEntity
  • immortal True if the damageable entity is immortal.

boolean YaDamageAPI.IsInvulnerable(YaEntity target)

ReturnsReturns false if the targetEntity is neither invulnerable nor damageable.
Checks if the damageable entity is invulnerable. Code sample
1
2
3
local isInvulnerable = YaDamageAPI.IsInvulnerable(targetEntity)
Returns false if the targetEntity is neither invulnerable nor damageable.
print("Damageable", targetEntity.EntityId, "is invulnerable:", isInvulnerable)
  • target The YaEntity to check

void YaDamageAPI.SetInvulnerable(YaEntity target, boolean invulnerable)

SERVER ONLY 
Sets the damageable YaEntity invulnerable. Code sample
1
2
-- set targetEntity invulnerable.
YaDamageAPI.SetInvulnerable(targetEntity, true)
  • target The target YaEntity to set
  • invulnerable A Boolean value that determines if the YaEntity is invulnerable. Set it true if you want the YaEntity invulnerable.

number YaDamageAPI.GetHitPoints(YaEntity target)

ReturnsThe hit points
Gets the hit points (HPs) of the YaEntity. Code sample
1
2
-- O is returned if the targetEntity is not damageable.
local hp = YaDamageAPI.GetHitPoints(targetEntity)
  • target The YaEntity from which you want to get HPs

void YaDamageAPI.SetHitPoints(YaEntity target, number amount)

SERVER ONLY 
Sets the hit points for the YaEntity. You can only set it within the range between 0 and MaxHitPoints. Code sample
1
YaDamageAPI.SetHitPoints(targetEntity, 100)
  • target The YaEntity to set hit points for
  • amount The value of the hit points

number YaDamageAPI.GetMaxHitPoints(YaEntity target)

ReturnsThe maximum hit points
Gets the maximum hit points. Code sample
1
2
local maxHp = YaDamageAPI.GetMaxHitPoints(targetEntity)
Get the maximum hit points of the targetEntity. 0 will be returned if the YaEntity is not damageable.
  • target The YaEntity from which you want to get maximum hit points

void YaDamageAPI.SetMaxHitPoints(YaEntity target, number amount)

SERVER ONLY 
Sets the maximum hit points (HP) of the YaEntity. The maximum hit points should be no less than 1. Code sample
1
2
3
-- range: >= 1
-- HitPoints will be set to MaxHitPoints regardless of the current HitPoints is greater than MaxHitPoints.
YaDamageAPI.SetMaxHitPoints(targetEntity, 200)
  • target The YaEntity that you want to set the maximum HP for
  • amount The value of the maximum HP

void YaDamageAPI.ApplyDamage(YaEntity target, YaDamage damage)

SERVER ONLY 
Applies damage to a YaEntity. Code sample
1
2
3
4
5
-- killerEntity deals 100 points of damage to targetEntity.
local damage = YaDamage.New()
damage.Enemy = killerEntity
damage.Amount = 100
YaDamageAPI.ApplyDamage(targetEntity, damage)
  • target The YaEntity to apply the damage
  • damage Information of the amount of the damage and where it comes from

void YaDamageAPI.Die(YaEntity target, YaDamage damage)

SERVER ONLY 
ReturnsReturns false if the YaEntity is invulnerable or undamageable.
Kills the damageable entity. Code sample
1
2
3
4
5
6
-- killerEntity kills dieEntity.
local damage = YaDamage.New()
damage.Enemy = killerEntity
YaDamageAPI.Die(dieEntity, damage)
The targetEntity won't die if it is invulnerable or immortal.
The targetEntity's hit points will be reduced to 0 if it's immortal.
  • target The YaEntity to killl
  • damage Information of the amount of the damage and where it comes from

void YaDamageAPI.Revival(YaEntity target, YaRevival revival)

SERVER ONLY 
Revives the damageable entity with the full hit points. Code sample
1
2
3
4
5
local revival = YaRevival.New()
revival.Revivaler = revivalerEntity
revival.Position = PositionEntity
YaDamageAPI.Revival(targetEntity, revival)
-- revivalEntity revives targetEntity at positionEntity.
  • target The YaEntity to revive
  • revival Information of the position to revive the YaEntity and who revives it

void YaDamageAPI.Hook(YaEntity target, function action<YaDamageHook>)

SERVER ONLY 
The hook called before applying damage Code sample
1
2
3
4
5
6
7
8
-- Double the damage in the hook method.
local function DamageHook(damage)
    local newDamage = YaDamage.New()
    newDamage.Amount = damage.Damage.Amount * 2
    newDamage.Enemy = damage.Damage.Enemy
    damage.Damage = newDamage
end
YaDamageAPI.Hook(targetEntity, DamageHook)
  • target The YaEntity to be damaged
  • action The hook method
    1. YaDamageHook This object only contains a paramater of YaDamage.

void YaDamageAPI.OnDamage(YaEntity target, function action<YaDamage>)

Fired when a damageable entity is damaged. Code sample
1
2
3
4
local function OnDamage(damage)
  print(targetEntity.EntityId, "get damage", damage.Amount, "from", damage.Enemy.EntityId)
end
YaDamageAPI.OnDamage(targetEntity, OnDamage)
  • target The YaEntity that gets the damage
  • action The callback function
    1. YaDamage The YaEntity who causes the damage and the amount

void YaDamageAPI.OnDead(function callback<YaEntity, YaDamage>, [YaEntity target])

Fired when a damageable entity is dead. Code sample
1
2
3
4
5
6
7
8
YaDamageAPI.OnDie(function (dead, damage)
    print(dead.EntityId, "die by entity", damage.Enemy.EntityId)
end)

 -- or just listening targetEntity's die event
YaDamageAPI.OnDie(function (dead, damage)
    print(dead.EntityId, "die by entity", damage.Enemy.EntityId)
end, targetEntity)
  • callback The callback function
    1. YaEntity The dead YaEntity
    2. YaDamage The YaEntity who causes the damage and the amount of the damage
  • target The dead YaEntity

void YaDamageAPI.OnRevival(function callback<YaEntity, YaRevival>, [YaEntity target])

Fired when a damageable entity is revived. Code sample
1
2
3
4
5
6
7
8
YaDamageAPI.OnRevival(function (target, revivalInfo)
 print(target.EntityId, "revivaled by entity", revivalInfo.Revivaler.EntityId, "on position", revivalInfo.Revivaler.EntityId)
end)

-- or just listening targetEntity's revival event
YaDamageAPI.OnRevival(function (target, revivalInfo)
    print(target.EntityId, "revivaled by entity", revivalInfo.Revivaler.EntityId, "on position", revivalInfo.Revivaler.EntityId)
end, targetEntity)
  • callback The callback function
    1. target The revived YaEntity
    2. revival info The information of who revives it and its position
  • target The revived YaEntity