Page 1 of 1

How to use TDE OnDeathEvent

Posted: Sun Apr 21, 2024 6:12 am
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!

Re: How to use TDE OnDeathEvent

Posted: Sun Apr 21, 2024 9:30 am
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.)

Re: How to use TDE OnDeathEvent

Posted: Sun Apr 21, 2024 5:58 pm
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);
        }
    }
}


Re: How to use TDE OnDeathEvent

Posted: Sun Apr 21, 2024 8:37 pm
by Tony Li
That's totally fine!