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 09 - Listening on player damage

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 09 - Listening on player damage

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

ScriptAPI for PlayerDeathArgs:
► Show Spoiler
3 x
Gurt

Locked