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!
How to use TDE OnDeathEvent
Re: How to use TDE OnDeathEvent
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:
(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.)
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>());
});
}
-
- Posts: 39
- Joined: Sat Apr 20, 2024 7:03 pm
Re: How to use TDE OnDeathEvent
Thank you Tony!
I created this for now, is that an ok solution or perhaps better to tap into the health component?
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
That's totally fine!