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

Question Serialization of Arrays of Arrays

Discussion in 'Scripting' started by Xerun1, Nov 8, 2022.

  1. Xerun1

    Xerun1

    Joined:
    Sep 9, 2022
    Posts:
    15
    I feel like this is probably not the best way to go about doing this, but Im fairly new so happy for someone to steer me in the right direction.

    I currently have a script on an enemy object that determines the kind of attack it can do. I have a float array like so {.25f,.5f,.75f,1f} that uses Random.Range(0f,1f) to determine which attack the enemy does.


    I want to advance that so that instead I have one big array madeup of other arrays and so I can swap in and out which array i'm using to determine attacks.

    The addendum to this is I'm hoping to then use the Serialize Field to create prefabs so that every enemy can use the same script. So that a lv1 enemy may only have 1 array of 2 attacks, but a boss may have 4 arrays of 8 attacks.

    Is there a good and optimal way of doing this?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    Well, for starters, Unity doesn't support multidimensional array serialisation. The world around is to have a wrapper class and have a collection of those instead. Something like this:

    Code (CSharp):
    1. [System.Serializable]
    2. public class AttackCollection
    3. {
    4.     public float[] AttackValues;
    5. }
    6.  
    7. //usage
    8. public AttackCollection[] AttackCollection;
    But further to that, I would look at scriptable objects in this situation. For these kind of values it'd make sense to author them in pure data assets and then reference said assets in your enemy prefabs.
     
  3. Xerun1

    Xerun1

    Joined:
    Sep 9, 2022
    Posts:
    15
    Yeah this was sort of where I was looking at. It's been a while since I looked at them though so will need to refresh myself on it.

    Was just hoping I could do it the array way since my code was 95% done in that department and would have needed only a slight tweak

    Thanks for your help! I appreciate it
     
  4. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    You can have a list of array. Or you can navigate your array using 1 dimension using any constant or known mathematics defining that array. Try a new List <GameObject[]>(), or else take it into 1 dimension. I had a few array issues I had to solve with the 1 dimension handling of that list. One notable was Euler angles not accepting a list of list to define the setting of an int in those xyz vector fields. Instead euler angles wanted me to grab the value of the three ints using the mathematics of a 1 dimension. Go figure. I don’t know how I did. Skeptical I guess. But it worked none the less where a list of list had failed for me.
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    The question was about serialising arrays of arrays (multidimensional collections), which Unity can't do. Try reading the posts please.
     
  6. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    Yeah list of array will let him serialise it
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    Um... no it won't.

    Code (CSharp):
    1.     public class SerialisationTestObject : ScriptableObject
    2.     {
    3.         [SerializeField]
    4.         private List<float[]> listArray = new List<float[]>();
    5.     }
    upload_2022-11-8_20-2-49.png

    Unity does not support multidimensional collections in any way shape or form. Please stop dispensing incorrect advice.
     
  8. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    You’re quite right you can’t serialise that array my mistake. I could have sworn I managed to serialise a list of arrays before :S can’t remember what I did now
     
  9. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,183
    There is a way to get the same effect though.
    Code (csharp):
    1. [System.Serializable]
    2. public class Cell
    3. {
    4.    //some stuff
    5. }
    6.  
    7. [System.Serializable]
    8. public class Array
    9. {
    10.    public List<Cell> cells = new List<Cell>();
    11.    public Cell this[int index] => cells[index];
    12. }
    13.  
    14. [System.Serializable]
    15. public class Matrix
    16. {
    17.    public List<Array> arrays = new List<Array>();
    18.    public Cell this[int x, int y] => arrays[x][y];
    19. }
    https://stackoverflow.com/questions...dimensional-arrays-in-unity/69019548#69019548
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    Yes, which was in my original post. Bottom line is you'll have to use wrapper classes to achieve the structure.
     
  11. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,183
    Whoops. I may have skimmed the thread a bit too much this time. :p

    There is a paid alternative in the form of Odin Inspector.

    upload_2022-11-8_18-15-49.png

    Code (csharp):
    1. using Sirenix.OdinInspector;
    2.  
    3. public class MyMB : SerializedMonoBehaviour
    4. {
    5.     [TableMatrix]
    6.     public int[,] IntArray;
    7. }
     
    spiney199 likes this.