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 » Sat Jul 27, 2019 2:06 pm
Scripting 10 - Listening on explosions
Scripting in SFD assumes you have a fair knowledge of C#.
The following code demonstrates how to listen on triggered explosions in v.1.3.0.
Code: Select all
Events.ExplosionHitCallback m_explosionHitEvent = null;
public void OnStartup() {
m_explosionHitEvent = Events.ExplosionHitCallback.Start(OnExplosionHit);
}
public void OnExplosionHit(ExplosionData explosion, ExplosionHitArg[] args) {
// explosion triggered
for(int i = 0; i < args.Length; i ++) {
switch (args[i].HitType) {
case ExplosionHitType.Damage:
Game.WriteToConsole(string.Format("Explosion {0} hit {1} {2} for {3} damage", explosion.InstanceID, (args[i].IsPlayer ? "player" : "object"), args[i].ObjectID, args[i].Damage));
break;
case ExplosionHitType.Shockwave:
Game.WriteToConsole(string.Format("Explosion {0} pushed {1} {2}", explosion.InstanceID, (args[i].IsPlayer ? "player" : "object"), args[i].ObjectID));
break;
case ExplosionHitType.None:
Game.WriteToConsole(string.Format("Explosion {0} overlapped {1} {2}", explosion.InstanceID, (args[i].IsPlayer ? "player" : "object"), args[i].ObjectID));
break;
}
}
}
Note: If you only want to listen on player damage you could use the PlayerDamageCallback event instead. See
viewtopic.php?f=22&t=3771
ScriptAPI Implementation
► Show Spoiler
Code: Select all
/// <summary>
/// ExplosionHitCallback.Event explosion data
/// </summary>
[Serializable()]
public struct ExplosionData
{
/// <summary>
/// Instance ID of the explosion.
/// Note that the same explosion can affect different objects under several updates, so track unique explosions using this InstanceID.
/// </summary>
public readonly int InstanceID;
/// <summary>
/// Gets the source explosion position.
/// </summary>
public readonly Vector2 Position;
/// <summary>
/// Gets the radius of the explosion.
/// </summary>
public readonly float Radius;
/// <summary>
/// Gets the max damage of the explosion
/// </summary>
public readonly float MaxDamage;
}
/// <summary>
/// ExplosionHitCallback.Event explosion hit argument for one object
/// </summary>
[Serializable()]
public struct ExplosionHitArg
{
/// <summary>
/// Gets the affected object ID.
/// </summary>
public readonly int ObjectID;
/// <summary>
/// Gets the hit object.
/// </summary>
public readonly IObject HitObject;
/// <summary>
/// Gets if the affected object is a player.
/// </summary>
public readonly bool IsPlayer;
/// <summary>
/// Gets the registered hit position.
/// </summary>
public readonly Vector2 HitPosition;
/// <summary>
/// Gets the explosion hit type.
/// </summary>
public readonly ExplosionHitType HitType;
/// <summary>
/// Gets the taken damage caused by the explosion after modifiers and distance to the center.
/// </summary>
public readonly float Damage;
/// <summary>
/// Gets the explosion force direction vector.
/// </summary>
public readonly Vector2 ForceDirection;
/// <summary>
/// The explosion impact percentage power in the range [0.0 - 1.0]
/// </summary>
public readonly float ImpactPercentage;
}
/// <summary>
/// Explosion hit type
/// </summary>
public enum ExplosionHitType
{
/// <summary>
/// 0, None. Explosion doesn't affect the object.
/// </summary>
None = 0,
/// <summary>
/// 1, Damage. Explosion affects the object directly, causing damage.
/// </summary>
Damage = 1,
/// <summary>
/// 2, Shockwave. Explosion affects the object but doesn't damage it (maybe it's behind something blocking the explosion).
/// </summary>
Shockwave = 2
}
Last edited by
Gurt on Sun Jul 28, 2019 1:04 pm, edited 1 time in total.
Reason: Updated implementation details
4 x
Gurt