Search Unity

Better storage solution

Discussion in 'Scripting' started by Emolk, Jan 22, 2020.

  1. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    I have a gameobject in my scene called 'Storage' which holds 5 scripts:

    1. Tile Storage
    2. Item Storage
    3. Agent Storage
    4. Attack Storage
    5. LayerMask Storage

    Basically these scripts hold variables that i want to access by many different scripts but don't want to manually input into all those different scripts.

    For example, instead of manually assigning a LayerMask everytime, i have assigned a bunch of different layermasks to my storage script and retrieve them from there. Example:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LayerMaskStorage : MonoBehaviour
    6. {
    7.     public static LayerMaskStorage instance;
    8.  
    9.     public LayerMask agents;
    10.     public LayerMask groundPlayerAlly;
    11.     public LayerMask playerAllyMask;
    12.     public LayerMask enemyMask;
    13.     public LayerMask enemyBlockMask;
    14.     public LayerMask ground;
    15.     void Awake(){
    16.         instance = this;
    17.     }
    18. }
    19.  
    Code (CSharp):
    1. LayerMaskStorage.instance.agents;
    An example of my agent storage, which stores the different enemy prefabs:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AgentStorage : MonoBehaviour
    6. {
    7.     public static AgentStorage instance;
    8.     [HideInInspector]
    9.     public List <GameObject> enemies = new List <GameObject>();
    10.     [HideInInspector]
    11.     public List <GameObject> enemiesSurface = new List <GameObject>();
    12.     public List<GameObject> launchMenuEnemies = new List<GameObject>();
    13.     public List<GameObject> caveEnemies = new List<GameObject>();  
    14.     public GameObject Bat;
    15.     public GameObject Bird;
    16.     public GameObject Ghost;
    17.     public GameObject Golem;
    18.     public GameObject Imp;
    19.     public GameObject Slime;
    20.     public GameObject Slime2;
    21.     public GameObject Slime3;
    22.     public GameObject Zombie;
    23.     public GameObject ZombieMiner;
    24.     public GameObject CircleEnemy;
    25.     public GameObject Spider;
    26.     public GameObject BoulderEnemy;
    27.  
    28.     // Bosses
    29.  
    30.     public GameObject WormBoss;
    31.     public GameObject SquidBoss;
    32.     public List <GameObject> bosses = new List <GameObject>();
    33.     void Awake(){
    34.         instance = this;
    35.         addEnemiesToList();
    36.         addEnemiesToListSurface();
    37.         addBossesToList();
    38.         addLaunchMenuEnemies();
    39.         addCaveEnemies();
    40.     }
    41.  
    42.     void addCaveEnemies(){
    43.         caveEnemies.Add(Bat);
    44.         caveEnemies.Add(Spider);
    45.     }
    46.  
    47.     void addLaunchMenuEnemies(){
    48.         launchMenuEnemies.Add(Zombie);
    49.         launchMenuEnemies.Add(Golem);
    50.         launchMenuEnemies.Add(Slime);
    51.     }
    52.  
    53.     void addBossesToList(){
    54.         bosses.Add(WormBoss);
    55.         bosses.Add(SquidBoss);
    56.     }
    57.  
    58.     void addEnemiesToList(){
    59.         enemies.Add(Bat);
    60.         enemies.Add(Bird);
    61.         enemies.Add(Ghost);
    62.         enemies.Add(Golem);
    63.         enemies.Add(Imp);
    64.         enemies.Add(Slime);
    65.         enemies.Add(Slime2);
    66.         enemies.Add(Slime3);
    67.         enemies.Add(Zombie);
    68.         enemies.Add(ZombieMiner);
    69.         enemies.Add(CircleEnemy);
    70.     }
    71.     void addEnemiesToListSurface(){
    72.  
    73.         enemiesSurface.Add(Ghost);
    74.         enemiesSurface.Add(Golem);
    75.         enemiesSurface.Add(Imp);
    76.         enemiesSurface.Add(Slime);
    77.         enemiesSurface.Add(Slime2);
    78.         enemiesSurface.Add(Slime3);
    79.         enemiesSurface.Add(Zombie);
    80.         enemiesSurface.Add(ZombieMiner);
    81.  
    82.     }
    83. }
    84.  
    Scriptableobject storage:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ItemStorage : MonoBehaviour
    6. {
    7.     public static ItemStorage instance;
    8.     // public List<Equipment> items;
    9.     public Weapon[] weapons;
    10.     public Equipment[] items;
    11.     public Ability[] abilities;
    12.     public GameObject[] weaponPrefabs;
    13.     public Attack[] attacks;
    14.  
    15.     void Awake(){
    16.         instance = this;
    17.         items = (Equipment[]) Resources.FindObjectsOfTypeAll(typeof(Equipment));
    18.         weapons = (Weapon[]) Resources.FindObjectsOfTypeAll(typeof(Weapon));
    19.         abilities = (Ability[]) Resources.FindObjectsOfTypeAll(typeof(Ability));
    20.         attacks = (Attack[]) Resources.FindObjectsOfTypeAll(typeof(Attack));
    21.         weaponPrefabs = Resources.LoadAll<GameObject>("PickupItems/Weapons");
    22.  
    23.         foreach (var ability in abilities)
    24.         {
    25.             ability.offCD = true;
    26.         }
    27.     }
    28.  
    29.     public Equipment[] getItems(){
    30.         return items;
    31.     }
    32. }
    33.  

    Every time my game runs all the storage items are added in. Is this bad practice? Should i be storing prefabs, scriptableobjects etc somehow else?
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Well, to me the name is slightly misleading, as (again, to me) storage implies persistence. You are simply providing a kind of directory service or centralized accessor to scene components. I don't think that there's much to worry about this approach. Make sure that all the objects you reference need to be loaded at the start of the scene if you are targeting restricted devices (mobiles), but on the other hand, your scripts can be used to make sure all assets are in-memory at the start of the Scene as an additional benefit.
     
    Emolk likes this.