Search Unity

Feedback ¿Bad Object Polling? Incorrect extension method.

Discussion in 'Scripting' started by jocker_88, Aug 20, 2020.

  1. jocker_88

    jocker_88

    Joined:
    Jun 12, 2019
    Posts:
    6
    Hello every body,

    I maked a algorithm to generate enemies in my game. This called using a generic type and there is a extension method. Is like a pool to catch existing enemy or instantiate a new enemy. Runs well, but I don't like the extension method.

    Ok, there is a static List to save enemies when they die, before when I need generate a enemy I call to RecoveryEnemy, it search a enemy using other method if in the list have any enemy of this type catch the enemy, relocate in the correct position (passed in parameters), restore the health and active it, in case that not exist a enemy in the List Iit call to CreateEnemy<R>() (static extensión method below the class), this create a new instance of the GameObject, to know what enemy need create I use method Find to search a enemy of this type in the Scene, after this the method return the InstanceID(), with the instance edit I use EditorUtility to instance object and relocate the enemy, not need restarthealth or activeEnemy because is a new instance of the prefab.

    Ok, I don't like the second part (extension method), currently runs well but I need a active object (enemy) in the scene and I think that there are better forms to instantiate the prefabs. I spent a lot of time searching on internet, in this forums, I read a lot of documentation and I didn't found the correct form to do this.

    ¿This is a correct way to create a Object Polling?
    ¿How would you do it?

    My current code:
    Code (CSharp):
    1. public class EnemyRecovery : MonoBehaviour
    2. {
    3.     private static List<Enemy> enemiesDied = new List<Enemy>();
    4.  
    5.     public void SaveEnemy(Enemy enemy) => enemiesDied.Add(enemy);
    6.  
    7.     public void RecoveryEnemy<T>(float x, float y) where T : Enemy
    8.     {
    9.         int index = GetEnemy<T>();
    10.  
    11.         if (index == -1)
    12.         {
    13.             int reference = RecoveryExtension.CreateEnemy<T>();
    14.             GameObject o = (GameObject)EditorUtility.InstanceIDToObject(reference);
    15.             o.GetComponent<T>().Relocate(x, y);
    16.         }
    17.         else {
    18.             Enemy e = enemiesDied[index];
    19.             e.Relocate(x,y);
    20.             e.RestartHealth();
    21.             e.ActiveEnemey();
    22.             enemiesDied.RemoveAt(index);
    23.         }
    24.     }
    25.  
    26.     private int GetEnemy<T>()
    27.     {
    28.         for(int i = 0; i < enemiesDied.Count; i++)
    29.             if (enemiesDied[i].GetType() == typeof(T)) return i;
    30.      
    31.         return -1;
    32.     }
    33. }
    34.  
    35. static class RecoveryExtension
    36. {
    37.     public static int CreateEnemy<T>() where T : Enemy
    38.     {
    39.         GameObject obj = GameObject.Find(typeof(T).ToString());
    40.         return UnityEngine.Object.Instantiate(obj).GetInstanceID();
    41.     }
    42. }
    Regards, Jocker. And thank you a lot of yout comments.

    P.D: I'm so sorry to my mistakes, I'm beginer in English.
     
    Last edited: Aug 20, 2020
  2. jocker_88

    jocker_88

    Joined:
    Jun 12, 2019
    Posts:
    6
    Ok, I resolve my problem using Resource.Load in my mehtod.

    Now looks like:

    Code (CSharp):
    1. public class EnemyRecovery : MonoBehaviour
    2. {
    3.     private static List<Enemy> enemiesDied = new List<Enemy>();
    4.  
    5.     public void SaveEnemy(Enemy enemy) => enemiesDied.Add(enemy);
    6.  
    7.     public void RecoveryEnemy<T>(float x, float y) where T : Enemy
    8.     {
    9.         int index = GetEnemy<T>();
    10.  
    11.         if (index == -1)
    12.         {
    13.             string pathPrefab = $"Prefabs/Enemys/{typeof(T)}";
    14.             GameObject enemy = Instantiate(Resources.Load(pathPrefab, typeof(GameObject)), gameObject.transform) as GameObject;
    15.             enemy.GetComponent<Enemy>().Relocate(x, y);
    16.  
    17.         }
    18.         else {
    19.             Enemy e = enemiesDied[index];
    20.             e.Relocate(x,y);
    21.             e.RestartHealth();
    22.             e.ActiveEnemey();
    23.             enemiesDied.RemoveAt(index);
    24.         }
    25.     }
    26.  
    27.     private int GetEnemy<T>()
    28.     {
    29.         for(int i = 0; i < enemiesDied.Count; i++)
    30.             if (enemiesDied[i].GetType() == typeof(T)) return i;
    31.      
    32.         return -1;
    33.     }
    34. }
    Currently run very well and the code is more dinamic, but I think so the comunity knows better forms to do this and I hope to read diferents opiniones and forms to do.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,749
    If your form works it is the best form.

    If you want to see other ways to do something, I suggest googling Youtube for Unity tutorials.
     
  4. jocker_88

    jocker_88

    Joined:
    Jun 12, 2019
    Posts:
    6
    Thank you a lot Kurt-Dekker, I navigated throught Unity tutorials a bit, I suposs that I need search more.