Damage
Namespace:
DeadworksManaged.Api
You can deal damage to any entity, and you can watch, change or block damage as it happens.
Damage an entity
pawn.Hurt(50f);
This takes 50 health from the player. It can kill them.
Give an attacker the credit
target.Hurt(100f, attacker: shooter);
If this kills the target, the kill goes to shooter and shows in the kill feed.
Deal damage over time
Use a timer to deal a little damage again and again.
Timer.Sequence(step =>
{
if (step.Run > 10 || !target.IsValid)
return step.Done();
target.Hurt(10f, attacker: attacker);
return step.Wait(200.Milliseconds());
});
This deals 10 damage every 200 milliseconds, and stops once step.Run passes 10 or the target is gone.
Run code when something takes damage
Override OnTakeDamage. It runs for every hit on the server.
public override HookResult OnTakeDamage(TakeDamageEvent ev)
{
Console.WriteLine($"{ev.Entity.DesignerName} took {ev.Info.Damage} damage");
return HookResult.Continue;
}
ev.Entity is whoever is being hurt. ev.Info describes the hit:
ev.Info.Damageis how muchev.Info.Attackeris who did itev.Info.Abilityis the ability or item that did it
Both Attacker and Ability can be null.
Block damage
Return HookResult.Stop and the hit does nothing.
public override HookResult OnTakeDamage(TakeDamageEvent ev)
{
// Patrons can't be hurt
if (ev.Entity.DesignerName == "npc_boss_tier3")
return HookResult.Stop;
return HookResult.Continue;
}
Change how much damage a hit does
Set ev.Info.Damage.
public override HookResult OnTakeDamage(TakeDamageEvent ev)
{
// Everything does double damage
ev.Info.Damage *= 2;
return HookResult.Continue;
}
Only react to one ability or item
Check the name of ev.Info.Ability. The abilities database lists every name.
public override HookResult OnTakeDamage(TakeDamageEvent ev)
{
if (ev.Info.Ability?.SubclassVData?.Name != "upgrade_discord")
return HookResult.Continue;
// This hit came from that item
return HookResult.Continue;
}
Kill an entity
Hurt can't promise a kill. Build the hit yourself and add the ForceDeath flag.
using var info = new CTakeDamageInfo(
damage: 1f,
attacker: null,
inflictor: null,
ability: null,
damageType: 0);
info.DamageFlags = TakeDamageFlags.ForceDeath | TakeDamageFlags.AllowSuicide;
target.TakeDamage(info);
Always create a CTakeDamageInfo with using, as above, so it is cleaned up afterwards.
Change how a hit behaves
Build the hit the same way, with different flags.
info.DamageFlags = TakeDamageFlags.PreventDeath;
Some useful flags:
PreventDeathleaves the target on 1 health at worstForceDeathalways killsSuppressDamageModificationignores armor and resistancesSuppressPhysicsForcedeals no knockbackSuppressEffectsshows no visual effectsSuppressKillCreditgives nobody the kill