Death Commands

recipe

Learn more about Animation Controllers

I define Death Effects as "Doing something when an Entity dies". There are a few wrong ways to achieve this that should be avoided, including:

  • Detecting death in the entity file, adding a component, and then trying to detect that component in the animation controller. This is wrong because the entity will be removed from the world before the animation controller has a chance to run.
  • Detecting the entity death from an outside source, such as a ticking command block. This method isn't strictly wrong, and in some circumstances, it may even be preferred. However it is costly and easy to break.

Using q.is_alive

The best way to create death effects is by using the is_alive query.

Simply create an animation controller with a transition based on is_alive. The final on_entry will run before the entity is removed from the world, allowing you to run your commands.

Here is a sample animation controller:

BP/animation_controllers/death.ac.jsonCopy
json
{
	"format_version": "1.10.0",
	"animation_controllers": {
		"controller.animation.death": {
		"initial_state":"default",
			"states": {
				"default": {
					"transitions": [
						{
							"dead": "!q.is_alive"
						}
					]
				},
				"dead": {
					"on_entry": ["/say I am dead!"]
				}
			}
		}
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Use on player entities

In the case of player entities, an additional transition must be added to the second animation state in order to ensure the state resets between deaths:

BP/animation_controllers/death.ac.jsonCopy
json
{
	"format_version": "1.10.0",
	"animation_controllers": {
		"controller.animation.death": {
		"initial_state":"default",
			"states": {
				"default": {
					"transitions": [
						{
							"dead": "!q.is_alive"
						}
					]
				},
				"dead": {
					"on_entry": ["/say I am dead!"],
					"transitions": [
						{
							"default": "q.is_alive"
						}
					]
				}
			}
		}
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

WARNING

Requires Experimental

Using minecraft:on_death

You can also use the minecraft:on_death component in your entity.json file in the Behavior Pack, which is a fairly easy way to accomplish a command on death.

You first add it to your components and make it run an event on self;

json
"minecraft:on_death" : {
	"event" : "wiki:on_death",
	"target" : "self"
}
1
2
3
4

And then, in your events section you add the event;

json
"wiki:on_death": {
	"run_command": {
		"command": [
			"say I have died!"
		]
	}
}
1
2
3
4
5
6
7

TIP

You can add scores and tags to the entity even when it is dead using this method.

Contributors

SirLichSmokeyStack