This forum is locked and will eventually go offline. If you have feedback to share you can find us in our Discord channel "MythoLogic Interactive" https://discord.gg/nECKnbT7gk

Forum 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.
Locked
User avatar
Gurt
Lead Programmer
Lead Programmer
Posts: 1887
Joined: Sun Feb 28, 2016 3:22 pm
Title: Lead programmer
Started SFD: Made it!
Location: Sweden
Gender:
Age: 36

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

Locked