Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Pooling enemies

Discussion in 'Scripting' started by i4mmanbearpi9, Sep 13, 2014.

  1. i4mmanbearpi9

    i4mmanbearpi9

    Joined:
    Sep 13, 2014
    Posts:
    4
    I'm trying to pool my enemies and I've tried a few different pieces of code and a few codes dot work for me like List and Add (i don't know why), anyways i was wondering if anyone has a good pooling system that they use frequently, that i can give a shot.

    I'm running out of ideas, and hate nagging my teacher (still learning). the game is just a simple shooter, I've got bullets pooling but that system for some reason doesn't work for my enemies.

    cheers :)
     
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    I've created my own one based off of this: http://unitypatterns.com/resource/objectpool/

    that's a really good start, when your game initialised you'd just do something like MyBulletPrefab.CreatePool (100) or whatever you want the the pool to be

    Then to get an object from the pool you'd do: MyBulletPrefab.Spawn (position, rotation)

    then to put back in the pool: MyBulletPrefab.Recycle ()
     
  3. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    I read your post and was inspired myself to try and create a pooling system that would work for most stuff, here's what I came up with:

    Code (CSharp):
    1. //ObjectPool.cs
    2. //C#
    3. using System;
    4. using System.Collections.Generic;
    5. using System.Runtime.Serialization;
    6.  
    7. public interface IPoolableObject
    8. {//this is so that the ObjectPool class can initialize and reset objects as they come in and out
    9.     void OnClaim();
    10.     void OnRecycle();
    11. }
    12.  
    13. public class ObjectPoolException : Exception, ISerializable
    14. {//custom exception to make debugging a little easier
    15.     public ObjectPoolException() { }
    16.     public ObjectPoolException(string message) : base(message) { }
    17.     public ObjectPoolException(string message, Exception inner) : base(message, inner) { }
    18.     protected ObjectPoolException(SerializationInfo info, StreamingContext context) : base(info, context) { }
    19. }
    20.  
    21. public class ObjectPool<T> where T : IPoolableObject, new()
    22. {
    23.     private Stack<T> objectPool;
    24.    
    25.     public int poolQuantity { get; private set; }    //this counts how many objects are in the pool
    26.    
    27.     public T Claim()
    28.     {//use this to grab an object from the pool
    29.         if (poolQuantity > 0)
    30.         {
    31.             T obj = objectPool.Pop();
    32.             obj.OnClaim();
    33.             poolQuantity--;
    34.             return obj;
    35.         }
    36.         throw new ObjectPoolException("object pool exhausted");
    37.     }
    38.    
    39.     public void Recycle(T obj)
    40.     {//use this to place an object back on the pool
    41.         obj.OnRecycle();
    42.         objectPool.Push(obj);
    43.         poolQuantity++;
    44.     }
    45.    
    46.     public ObjectPool(T[] pool)
    47.     {//create a new pool from an array of pre-created objects
    48.         objectPool = new Stack<T>();
    49.         for (int i = 0; i < pool.Length; i++)
    50.         {
    51.             Recycle(pool[i]);
    52.         }
    53.     }
    54.     public ObjectPool(int initialCount)
    55.     {//create a new pool of N count
    56.         objectPool = new Stack<T>();
    57.         for (int i = 0; i < initialCount; i++)
    58.         {
    59.             Recycle(new T());
    60.         }
    61.     }
    62. }
    63.  
    Code (CSharp):
    1. //PooledGameObject.cs
    2. //C#
    3. using UnityEngine;
    4.  
    5. public class PooledGameObject : IPoolableObject
    6. {//this is an example of IPoolableObject
    7.     private GameObject gameObject;            //store the GameObject
    8.    
    9.     public PooledGameObject()
    10.     {//new GameObject
    11.         gameObject = new GameObject("pooled object");
    12.     }
    13.    
    14.     private PooledGameObject(GameObject gameObject)
    15.     {//new GameObject from existing GameObject
    16.         this.gameObject = gameObject;
    17.     }
    18.    
    19.     public void OnClaim()
    20.     {//enable the GameObject
    21.         gameObject.name = "unnamed object";
    22.         gameObject.SetActive(true);
    23.     }
    24.    
    25.     public void OnRecycle()
    26.     {//disable the GameObject
    27.         gameObject.SetActive(false);
    28.         gameObject.name = "pooled object";
    29.     }
    30.    
    31.     public static implicit operator GameObject(PooledGameObject pooledGameObject)
    32.     {//implicitly convert from PooledGameObject to GameObject
    33.         return pooledGameObject.gameObject;
    34.     }
    35.    
    36.     public static implicit operator PooledGameObject(GameObject gameObject)
    37.     {//implicitly convert from GameObject to PooledGameObject
    38.         return new PooledGameObject(gameObject);
    39.     }
    40. }
    41.  
    It's in C# though, as you didn't state what language you're using I'm unaware of if this is useful to you or not, but thought I'd post it to be educational! :eek:

    Hope this helps!
     
  4. Kid_Niki

    Kid_Niki

    Joined:
    Feb 4, 2014
    Posts:
    5
    NICE! Another resource for you si here, there are actually a lot of good videos from the Live sessions!

    http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling

    As for bullets or enemies or whatever, just remember, you are just building a list or array or dicitonary or collection of GameObjects and then setting them to isActive to true or false, as long as they aren't either when you do it. This video is nice because it has an added method that I never had which grows your collection if it needs to.

    Keep up the learning!