Trigger on Death [SOLVED]

Announcements, support questions, and discussion for Quest Machine.
Post Reply
mschoenhals
Posts: 118
Joined: Thu Dec 19, 2019 7:38 am

Trigger on Death [SOLVED]

Post by mschoenhals »

Hi,
In my game, enemies aren't destroyed or disabled on death. They lie there until the player moves into another scene. How can I trigger a counter for killing enemies if I'm not using destroy or disable for defeating enemies?
Last edited by mschoenhals on Sun Dec 22, 2019 6:51 pm, edited 1 time in total.
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Trigger on Death

Post by Tony Li »

Hi,

There are still several ways you can increment a counter. One of the easiest is to send a message using Quest Machine's message system.

1. Add a counter to your quest (e.g., "orcsKilled"). Set Value Mode to Messages. Add a message such as "Killed" + "Orc".

2. Optionally add some HUD text that shows the counter value, such as "{#orcsKilled}/{>#orcsKilled}". You can use the Quest Reference window to copy these counter tags to the clipboard so you don't have to type them manually.

3. Add a QuestControl component to your enemy. If your enemy has a UnityEvent that's invoked when it's killed, such as OnDeath(), configure this event to call QuestControl.SendToQuestMachine with the message that increments the counter, such as "Killed:Orc".

4. If you don't have a UnityEvent, you can skip #3. Instead, call MessageSystem.SendMessage() in code:

Code: Select all

PixelCrushers.MessageSystem.SendMessage(this, "Killed", "Orc");
mschoenhals
Posts: 118
Joined: Thu Dec 19, 2019 7:38 am

Re: Trigger on Death

Post by mschoenhals »

So in steps 3 and 4, is this hard coded in? Is there a way to do it where I wouldn't have to change the code every time I want to have a quest that involves killing a particular type of enemy?
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Trigger on Death

Post by Tony Li »

It doesn't have to be hard-coded. If you have script that identifies the enemy type, you can pass that instead. Something like:

Code: Select all

PixelCrushers.MessageSystem.SendMessage(this, "Killed", GetComponent<Enemy>().EnemyName);
or even pass the GameObject name if that identifies the enemy type.

If you're using Quest Machine's procedural quest generation, you can define an EntityType asset, add a QuestEntity component to the enemy, and assign the EntityType asset to it. Then you can send the entity type:

Code: Select all

PixelCrushers.MessageSystem.SendMessage(this, "Killed", GetComponent<QuestEntity>().entityType.name);
mschoenhals
Posts: 118
Joined: Thu Dec 19, 2019 7:38 am

Re: Trigger on Death

Post by mschoenhals »

Got it!
Turns out that I do have an On Die() event that I could use to track the kill count. I added the Quest Control and added it to the Event and it worked perfectly!
Thanks for your help,
Mike
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Trigger on Death [SOLVED]

Post by Tony Li »

Glad to help!
Post Reply