Scripting 11 - Listening on object creation, damage and termination
Scripting in SFD assumes you have a fair knowledge of C#.
The following code demonstrates how to listen on objects being created, damaged and terminated in v.1.3.0.
Code: Select all
// Example script to listen on objects created, damaged and terminated.
public void OnStartup()
{
Events.ObjectCreatedCallback.Start(OnObjectCreated);
Events.ObjectDamageCallback.Start(OnObjectDamage);
Events.ObjectTerminatedCallback.Start(OnObjectTerminated);
}
public void OnObjectCreated(IObject[] objs) {
foreach(IObject obj in objs) {
Game.WriteToConsole(string.Format("Object {0} ({1}) created", obj.UniqueID, obj.Name));
}
}
public void OnObjectDamage(IObject obj, ObjectDamageArgs args) {
// object took damage
if (args.DamageType != ObjectDamageType.Fire) {
if (args.SourceID != 0) {
Game.WriteToConsole(string.Format("Object {0} took {1} {2} damage from {3} {4}", obj.UniqueID, args.Damage, args.DamageType, (args.IsPlayer ? "player" : "object"), args.SourceID));
} else {
Game.WriteToConsole(string.Format("Object {0} took {1} {2} damage", obj.UniqueID, args.Damage, args.DamageType));
}
}
}
public void OnObjectTerminated(IObject[] objs) {
// objects terminated. Note: This is run just before the object is about to be destroyed or removed. To see if it was destroyed, check the IObject.DestructionInitiated property.
foreach(IObject obj in objs) {
Game.WriteToConsole(string.Format("Object {0} was {1}", obj.UniqueID, (obj.DestructionInitiated ? "destroyed" : "removed")));
}
}
ScriptAPI Implementation for ObjectDamageCallback
► Show Spoiler
Code: Select all
/// <summary>
/// ObjectDamageCallback.Event arguments
/// </summary>
[Serializable()]
public struct ObjectDamageArgs
{
/// <summary>
/// Gets the damage type.
/// </summary>
public readonly ObjectDamageType DamageType;
/// <summary>
/// Gets the damage amount.
/// </summary>
public readonly float Damage;
/// <summary>
/// Gets if the sourceID refers to a player. True when DamageType is Player or PlayerImpact.
/// </summary>
public readonly bool IsPlayer;
/// <summary>
/// Gets the sourceID causing the damage. <para/>
/// if DamageType is ObjectDamageType.Projectile Use Game.GetProjectile(args.SourceID) to get the projectile. <para/>
/// if DamageType is ObjectDamageType.Player|PlayerImpact Use Game.GetPlayer(args.SourceID) to get the player. <para/>
/// if DamageType is ObjectDamageType.Impact Use Game.GetObject(args.SourceID) to get the object. <para/>
/// if DamageType is ObjectDamageType.Explosion the args.SourceID refers to the explosion InstanceID. <para/>
/// </summary>
public readonly int SourceID;
}
/// <summary>
/// ObjectDamageType
/// </summary>
public enum ObjectDamageType
{
/// <summary>
/// 0, Impact. Caused by the object colliding against another object.
/// </summary>
Impact = 0,
/// <summary>
/// 1, PlayerImpact. Caused by players falling onto the object.
/// </summary>
PlayerImpact = 1,
/// <summary>
/// 2, Player. Caused by player melee actions damaging the object.
/// </summary>
Player = 2,
/// <summary>
/// 3, Projectile. Caused by projectiles damaging the object.
/// </summary>
Projectile = 3,
/// <summary>
/// 4, Explosion. Caused by explosion damaging the object.
/// </summary>
Explosion = 4,
/// <summary>
/// 5, Fire. Cause by fire damaging the object.
/// </summary>
Fire = 5
}