Automatically tagging spawned entities

Announcements, support questions, and discussion for Quest Machine.
Post Reply
User avatar
wiverson
Posts: 22
Joined: Wed Feb 13, 2019 7:49 pm

Automatically tagging spawned entities

Post by wiverson »

Hi!

I'd like to have all entities spawned by a spawner automatically get setup to generate a message when they are destroyed based on the spawner.

For example, I have a spawner that will generate a bunch of different kinds of asteroids. I'd like all of the spawned asteroids (based on a bunch of different prefabs) automatically generate the same death message when they are destroyed.

It looks like the easiest thing to do is create a subclass of Spawner and override SpawnEntity(), and then attach the correct component/script there dynamically.

Is that right, or is there another/better way to do it?

Thanks!
-Will
User avatar
Tony Li
Posts: 21925
Joined: Thu Jul 18, 2013 1:27 pm

Re: Automatically tagging spawned entities

Post by Tony Li »

Why not just add a separate script to your prefabs (e.g., asteroid prefabs) with an OnDestroy method that sends the message? Or does the message need to change based on which spawner spawned the asteroid?

In that case, a subclass of Spawner that overrides SpawnEntity() is a fine solution.
User avatar
wiverson
Posts: 22
Joined: Wed Feb 13, 2019 7:49 pm

Re: Automatically tagging spawned entities

Post by wiverson »

>Or does the message need to change based on which spawner spawned the asteroid?

Yup, that's it. I have a bunch of different asteroid prefabs, as well as different enemy ship spawners. I want to decouple the death message from the prefabs.

Thanks!
User avatar
Tony Li
Posts: 21925
Joined: Thu Jul 18, 2013 1:27 pm

Re: Automatically tagging spawned entities

Post by Tony Li »

Okay, great! Make sure you're on Quest Machine 1.1.5+, and make a subclass of Spawner that overrides InstantiateEntity. For example:

Code: Select all

using UnityEngine;
using PixelCrushers;
using PixelCrushers.QuestMachine;

public class MyCustomSpawner : Spawner
{
    public string deathMessage; // Set in Inspector.
    
    protected override GameObject InstantiateEntity(GameObject prefab)
    {
        var instance = base.InstantiateEntity(prefab);
        var questControl = instance.AddComponent<QuestControl>();
        var disappearEvent = instance.AddComponent<DisappearEvent>();
        disappearEvent.onDisappeared.AddListener(() => { questControl.SendToMessageSystem(deathMessage); });
        return instance;
    }
}
User avatar
wiverson
Posts: 22
Joined: Wed Feb 13, 2019 7:49 pm

Re: Automatically tagging spawned entities

Post by wiverson »

I'm on 1.1.4 right now. I wound up having to tweak Spawner to have to pool instances on start - initializing them on demand was causing frame drops.

I did a quick stab at importing 1.1.5 and broke stuff, my fault for going too fast and breaking the wrong stuff. I'm going to take another whack at it next week.
User avatar
Tony Li
Posts: 21925
Joined: Thu Jul 18, 2013 1:27 pm

Re: Automatically tagging spawned entities

Post by Tony Li »

Pooling is on my to-do list. In 1.1.5, I split out InstantiateEntity() and DestroyEntity() so people can make subclasses with their preferred pooling solution. The only breaking change in 1.1.5 is that the format of save data changed. (An extra byte was added to save the quest's "show tracking" on/off bool.) If you run into any issues next time you try to update, just let me know.
Post Reply