How to use TDE OnDeathEvent

Announcements, support questions, and discussion for Love/Hate.
Post Reply
dkrusenstrahle
Posts: 39
Joined: Sat Apr 20, 2024 7:03 pm

How to use TDE OnDeathEvent

Post by dkrusenstrahle »

Hello,

I am using TopDown Engine with Love/Hate and have installed the Topdown integration package.
How can I use PixelCrushers.TopDownEngineSupport OnDeathEvent? I want to automatically report when an NPC is dead / object is destroyed.

Thank you!
User avatar
Tony Li
Posts: 22085
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to use TDE OnDeathEvent

Post by Tony Li »

Hi,

Make sure your NPC has a (TDE) Health component.

If you add an OnDeathEvent component to the NPC, it will hook into the Health component's OnDeath and OnRevive events. When the NPC dies or is revived, the OnDeathEvent component will run its corresponding UnityEvent.

You can configure the UnityEvent in code or in the inspector. For example:

Code: Select all

void Start()
{
    // When the NPC dies, find the player's DeedReporter and report the deed "Killed":
    GetComponent<OnDeathEvent>().OnDeath.AddListener(() =>
    {
        FindObjectOfType<DeedReporter>().ReportDeed("Kill", this.GetComponent<FactionMember>());
    });
}
(Note: I kept the code short for clarity. In practice, you'll want to make sure GetComponent() and FindObjectOfType() actually return valid values and not null -- i.e., proper error checking.)
dkrusenstrahle
Posts: 39
Joined: Sat Apr 20, 2024 7:03 pm

Re: How to use TDE OnDeathEvent

Post by dkrusenstrahle »

Thank you Tony!

I created this for now, is that an ok solution or perhaps better to tap into the health component?

Code: Select all

using UnityEngine;
using PixelCrushers.LoveHate;

public class DeedReportDeath : MonoBehaviour
{
    void OnDestroy()
    {
        ReportDeed();
    }

    private void ReportDeed()
    {
        var deedReporter = GetComponent<DeedReporter>();
        var factionMember = GetComponent<FactionMember>();
        if (deedReporter != null && factionMember != null)
        {
            deedReporter.ReportDeed("Killed", factionMember);
            Debug.Log("Deed reported: Killed " + gameObject.name);
        }
        else
        {
            Debug.LogError("DeedReporter or FactionMember component is missing on " + gameObject.name);
        }
    }
}

User avatar
Tony Li
Posts: 22085
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to use TDE OnDeathEvent

Post by Tony Li »

That's totally fine!
Post Reply