Dear forum users! In compliance with the new European GDPR regulations, we'd just like to inform you that if you have an account, your email address is stored in our database. We do not share your information with third parties, and your email address and password are encrypted for security reasons.
New to the forum?
Say hello in this topic! Also make sure to read
the rules.
A smaller forum with a few tutorials how to get started with the ScriptAPI.
-
Gurt
- Lead Programmer

- Posts: 1884
- Joined: Sun Feb 28, 2016 3:22 pm
- Title: Lead programmer
- Started SFD: Made it!
- Location: Sweden
- Gender:
- Age: 35
Post
by Gurt » Fri Jul 26, 2019 5:35 pm
Scripting 09 - Listening on player damage
Scripting in SFD assumes you have a fair knowledge of C#.
The following code demonstrates how to listen on player damage events in v.1.3.0.
Code: Select all
// Example script to read player damage
public void OnStartup()
{
Events.PlayerDeathCallback.Start(OnPlayerDeath);
Events.PlayerDamageCallback.Start(OnPlayerDamage);
}
public void OnPlayerDamage(IPlayer player, PlayerDamageArgs args)
{
if (args.DamageType == PlayerDamageEventType.Melee && args.SourceID != 0) {
IPlayer hitBy = Game.GetPlayer(args.SourceID);
Game.WriteToConsole(string.Format("Player {0} took {1} melee damage from player {2}", player.UniqueID, args.Damage, hitBy.UniqueID));
} else {
Game.WriteToConsole(string.Format("Player {0} took {1} {2} damage", player.UniqueID, args.Damage, args.DamageType));
}
}
public void OnPlayerDeath(IPlayer player, PlayerDeathArgs args)
{
// player just died or was removed (or both if falling outside the map while alive or gibbed while alive).
if (args.Killed) {
Game.WriteToConsole(string.Format("Player {0} died", player.UniqueID));
}
if (args.Removed) {
Game.WriteToConsole(string.Format("Player {0} removed", player.UniqueID));
}
}
ScriptAPI for PlayerDamageArgs:
► Show Spoiler
Code: Select all
/// <summary>
/// PlayerDamageEventType
/// </summary>
public enum PlayerDamageEventType
{
/// <summary>
/// 0, Other
/// </summary>
Other = 0,
/// <summary>
/// 1, Projectile
/// </summary>
Projectile = 1,
/// <summary>
/// 2, Missile (thrown items)
/// </summary>
Missile = 2,
/// <summary>
/// 3, Fall
/// </summary>
Fall = 3,
/// <summary>
/// 4, Melee
/// </summary>
Melee = 4,
/// <summary>
/// 5, Explosion
/// </summary>
Explosion = 5,
/// <summary>
/// 6, Fire
/// </summary>
Fire = 6
}
/// <summary>
/// PlayerDamageCallback.Event arguments
/// </summary>
[Serializable()]
public struct PlayerDamageArgs
{
/// <summary>
/// Gets the type of damage.
/// </summary>
public readonly PlayerDamageEventType DamageType;
/// <summary>
/// Damage amount
/// </summary>
public readonly float Damage;
/// <summary>
/// Gets if this is overkill damage when the player is already dead and takes additional damage.
/// </summary>
public readonly bool OverkillDamage;
/// <summary>
/// Source damage object ID. Usage ID depends on the PlayerDamageEventType. <para/>
/// if DamageType is PlayerDamageEventType.Projectile Use Game.GetProjectile(args.SourceID) to get the projectile. <para/>
/// if DamageType is PlayerDamageEventType.Melee Use Game.GetPlayer(args.SourceID) to get the player. <para/>
/// if DamageType is PlayerDamageEventType.Missile Use Game.GetObject(args.SourceID) to get the object. <para/>
/// </summary>
public readonly int SourceID;
}
ScriptAPI for PlayerDeathArgs:
► Show Spoiler
Code: Select all
/// <summary>
/// PlayerDeathEventType
/// </summary>
[Flags()]
public enum PlayerDeathEventType
{
/// <summary>
/// 0, None
/// </summary>
None = 0,
/// <summary>
/// 1, Killed
/// </summary>
Killed = 1,
/// <summary>
/// 2, Removed
/// </summary>
Removed = 2,
/// <summary>
/// 3, Killed and Removed
/// </summary>
KilledAndRemoved = 3
}
/// <summary>
/// PlayerDeathCallback.Event arguments
/// </summary>
[Serializable()]
public struct PlayerDeathArgs
{
/// <summary>
/// Gets the type of event.
/// </summary>
public readonly PlayerDeathEventType Event;
/// <summary>
/// Gets if the event represent a remove event.
/// Player falling off the edge of the map or being gibbed for example.
/// </summary>
public readonly bool Removed;
/// <summary>
/// Gets if the event represent a death event.
/// Player being killed from an alive state or falling off the edge of the map while alive for example.
/// </summary>
public readonly bool Killed;
}
3 x
Gurt