Search Unity

Making a List of Lists inside of a Scriptable Object

Discussion in 'Scripting' started by literacy, Jul 11, 2022.

  1. literacy

    literacy

    Joined:
    Nov 14, 2021
    Posts:
    56
    With some help, I was able to create a Scriptable Object with a List holding a class:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.VisualScripting;
    5.  
    6. [CreateAssetMenu(fileName = "New LevelData", menuName = "Level Data", order = 51)]
    7.  
    8. public class LevelData : ScriptableObject
    9. {
    10.     [SerializeField]
    11.     public List<AuxNeem> list;
    12.  
    13.     [System.Serializable]
    14.    
    15.     [IncludeInSettings(true)]
    16.     public class AuxNeem{
    17.         [SerializeField]
    18.         public NeemData superNeem;
    19.        
    20.         [SerializeField]
    21.         public FeemData Feem;
    22.    
    23.     }
    24. }
    It works amazingly now, but now I am getting ambitious.

    * My Scriptable Object allows me to easily create a List of AuxNeems.
    * I would like to be able to create *multiple* Lists of AuxNeems within the same Scriptable Object.

    Any advice on how to make such a List of Lists inside of this Scriptable Objects?
     
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    just use type List<List<AuxNeem>>
     
  3. literacy

    literacy

    Joined:
    Nov 14, 2021
    Posts:
    56
    Like this?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.VisualScripting;
    5. [CreateAssetMenu(fileName = "New WordData", menuName = "Word Data", order = 51)]
    6. public class WordData : ScriptableObject
    7. {
    8.     [SerializeField]
    9.     public List<List<AuxNeem2>> list;
    10.     [System.Serializable]
    11.  
    12.     [IncludeInSettings(true)]
    13.     public class AuxNeem2{
    14.         [SerializeField]
    15.         public NeemData superNeem;
    16.      
    17.         [SerializeField]
    18.         public FeemData Feem;
    19.  
    20.     }
    21. }
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    List<List<>> won't be serializable. You'll need to create a container object:

    Code (CSharp):
    1. [Serializable]
    2. public class AuxNeemContainer {
    3.   public List<AuxNeem2> auxNeems;
    4. }
    5.  
    6. [Serializable]
    7. public class AuxNeem2 {
    8.   // SerializeField is unnecessary for public fields
    9.   public NeemData superNeem;
    10.   public FeemData Feem;
    11. }
    12.  
    13. public List<AuxNeemContainer> list;
     
  5. literacy

    literacy

    Joined:
    Nov 14, 2021
    Posts:
    56
    Thank you, that worked perfectly!

    I am slowly learning how to write Scriptable Objects that include Lists and Lists of Lists, etc. Is there a resource/tutorial on this stuff? Or even just a library of Scriptable Objects that I can read carefully and learn from.
     
    McPeppergames likes this.
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    ScriptableObjects just use Unity's serialization system, which is documented fairly well in the manual. If you read up on that it covers all of the fundamentals, and from there it's just applying them, and occasionally experimenting to make sure things behave as you expect.
     
  7. McPeppergames

    McPeppergames

    Joined:
    Feb 15, 2019
    Posts:
    103
    Thank you for posting this.
    I can not figure out how to use this correctly. It seems logical 100% and is implemented fast BUT how can you check if a Container or subcontainer is already existing?
    How can you add a new subcontainer then and how can you delete a list entry? Etc. ?
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    I'm pretty sure all of these questions can be answered by familiarizing yourself with the C# List API. All of the operations you asked about are handled here:

    https://learn.microsoft.com/en-us/d...ctions.generic.list-1?view=net-8.0#properties
     
  9. McPeppergames

    McPeppergames

    Joined:
    Feb 15, 2019
    Posts:
    103
    I have now changed the code to this. Same as above, but using Waves of Creeps instead:

    Code (CSharp):
    1.  
    2. [Serializable]
    3. public class WaveContainer
    4. {
    5.     public List<CreepContainer> creepCont = new List<CreepContainer>();
    6. }
    7.  
    8. [Serializable]
    9. public class CreepContainer
    10. {
    11.     public int HD_VAl;
    12.     public float Speed_VAL;
    13. }
    14.  
    15. public List<WaveContainer> myWaveCreepContainer = new List<WaveContainer>();

    As you can see I have added the parts:
    = new List<CreepContainer>();
    and
    = new List<WaveContainer>();

    This was the part missing to make it work correctly.

    Anything against doing it this way?

    Thank you!