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.

Scripting 11 - Listening on object creation, damage and termination

A smaller forum with a few tutorials how to get started with the ScriptAPI.
Forum rules
By using the forum you agree to the following rules.
Post Reply
User avatar
Gurt
Lead Programmer
Lead Programmer
Posts: 1884
Joined: Sun Feb 28, 2016 3:22 pm
Title: Lead programmer
Started SFD: Made it!
Location: Sweden
Gender:
Age: 31

Scripting 11 - Listening on object creation, damage and termination

Post by Gurt » Sat Jul 27, 2019 10:37 pm

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
2 x
Gurt

Post Reply