Page 1 of 1
Automatically tagging spawned entities
Posted: Fri Mar 08, 2019 2:20 pm
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
Re: Automatically tagging spawned entities
Posted: Fri Mar 08, 2019 3:11 pm
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.
Re: Automatically tagging spawned entities
Posted: Fri Mar 29, 2019 2:34 pm
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!
Re: Automatically tagging spawned entities
Posted: Fri Mar 29, 2019 3:00 pm
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;
}
}
Re: Automatically tagging spawned entities
Posted: Fri Mar 29, 2019 7:06 pm
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.
Re: Automatically tagging spawned entities
Posted: Fri Mar 29, 2019 9:06 pm
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.