Visit the new site! Link Search Menu Expand Document

Death Effects

Table of contents
  1. Death Effects
    1. Using query.is_alive
    2. Use on player entities
    3. Wait, but how do Animation Controllers work?

Intermediate

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 query.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.json

{
    "format_version": "1.10.0",
    "animation_controllers": {
        "controller.animation.death": {
            "states": {
                "default": {
                    "transitions": [
                        {
                            "dead": "!query.is_alive"
                        }
                    ]
                },
                "dead": {
                    "on_entry": [
                        "/say I am dead!"
                    ]
                }
            }
        }
    }
}

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.json

{
    "format_version": "1.10.0",
    "animation_controllers": {
        "controller.animation.death": {
            "states": {
                "default": {
                    "transitions": [
                        {
                            "dead": "!query.is_alive"
                        }
                    ]
                },
                "dead": {
                    "on_entry": [
                        "/say I am dead!"
                    ],
                    "transitions": [
                        {
                            "default": "query.is_alive"
                        }
                    ]
                }
            }
        }
    }
}

Wait, but how do Animation Controllers work?

Learn about ACs here!