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 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.
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: 34

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

Post Reply