- Home /
The question is answered, right answer was accepted
Best way to create children once
So i'm creating a simple genetic algorithm game, and i have come to the point to implement the breeding of my creatures. The creatures should trigger the breeding when a WillingToBreed boolean is true and they come in contact with another creature. I only want the genetic algorithm to trigger once, and for both parents, to produce children. How can i make it so the two functions end up running one function?
Currently I have this:
void OnCollisionEnter2D(Collision2D collision)
{
if (willingToBreed)
{
if (collision.otherCollider.gameObject.GetComponent<Creature>().willingToBreed)
{
//Do the breeding
}
}
}
This is in every creature, so when they touch the "//Do the breeding" will be called twice, however i want code do be run ONCE, per both creatures.
I tried making a different class that revives breeding requests, filters out the duplicates then breeds, however if i put the filtering code in the update method, im not sure in what order the updates get called, so if the update gets called in between the creature updates, it could breed twice. Not sure if thats how it works, so im asking how the best way do it.
Answer by Lairinus · Oct 17, 2017 at 06:03 PM
So the problem is that when two Creatures touch each other, it's impossible to call something only once inside of the event because both events fire simultaneously. You need a Singleton class (BreedingSingleton) and you need to add it to a GameObject.
The following code adds the breeding creatures to the list, and then uses a Coroutine to wait every frame until the Creature count is greater or equal to 2 and calls the "HandleBreeding" method. BreedingSingleton.HandleBreeding() is where your code should go when two animals breed
This code will only allow animals to breed once. If you want it to do more, extend it yourself :)
public class Creature: MonoBehaviour
{
public bool willingToBreed = true;
void OnCollisionEnter2D(Collision2D collision)
{
if (willingToBreed)
{
if (collision.otherCollider.gameObject.GetComponent<Creature>().willingToBreed)
{
BreedingSingleton.AddCreatureToBreedingList(this);
}
}
}
}
public class BreedingSingleton : MonoBehaviour // Add this in your game to one object
{
public static List<Creature> currentBreedingCreatures = new List<Creature>(); // Animals that currently want to breed
public static List<Creature> closedBreedingCreatures = new List<Creature>(); // Animals that can no longer breed
IEnumerator Start()
{
// Dump old references in case this is used between scenes
currentBreedingCreatures = new List<Creature>();
closedBreedingCreatures = new List<Creature>();
while (true)
{
yield return null;
// Check every frame for animals that are waiting to breed. If there are at least 2, we continue.
if (currentBreedingCreatures.Count >= 2)
{
// Breed them.
HandleBreeding(currentBreedingCreatures[0], currentBreedingCreatures[1]);
}
}
}
// Put your code that handles the breeding inside of here.
private void HandleBreeding(Creature creature1, Creature creature2)
{
RemoveCreatureFromBreedingList(creature1);
RemoveCreatureFromBreedingList(creature2);
// Add whatever you need to finish breeding the creatures here. Whatever is here will only happen once.
}
public static void AddCreatureToBreedingList(Creature creature)
{
if (creature == null)
{
Debug.Log("Attempted to add a null creature into the currentBreedingCreatures list. Path: BreedingSingleton.AddCreatureToBreedingList(Creature creature)");
return; // Creature is null. We HATE nulls.
}
if (closedBreedingCreatures.Contains(creature))
return; // Creature already bred. We don't want this one.
if (currentBreedingCreatures.Contains(creature))
return; // We already added this creature. We don't want this one, either.
currentBreedingCreatures.Add(creature);
}
private void RemoveCreatureFromBreedingList(Creature creature)
{
if (currentBreedingCreatures.Contains(creature))
currentBreedingCreatures.Remove(creature);
closedBreedingCreatures.Add(creature);
}
}
Answer by Bunny83 · Oct 17, 2017 at 05:23 PM
Uhm when you have breed one creature, don't you want to reset the "willingToBreed" variables back to false?