Search Unity

adding more variables to an array

Discussion in 'Scripting' started by Yourking77, Aug 23, 2016.

  1. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    I have an array of GameObjects now how would I add a separate spawn chance for each element added to the array? would I need to make a special editor script?
     
  2. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    You may want an array of serializable objects:
    Code (csharp):
    1. [System.Serializable]
    2. public class SpawnItem {
    3.   public GameObject item;
    4.   public float chance;
    5. }
    6.  
    7. public List<SpawnItem> itemsToSpawn;
    That should show up fine in the editor as is, although you'll have to expand each one. A custom editor script could make it easier to work with.
     
  3. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    Thank you, I thought there was a way to do it with just arrays, I will have to mess around with lists.
     
  4. lloydsummers

    lloydsummers

    Joined:
    May 17, 2013
    Posts:
    359
    In most languages you can. In the current implemented version of .NET with Unity, you cannot... That said, you have two choices.

    Cast it to a List, which creates a new structure using your data. It is about twice as slow as option number two, if you plan to cast it back to an Array again.

    The proper C# Unity way is actually to create a new Array of the new size. And iterate through and assign the elements.
     
  5. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    Do what with arrays? You can certain use:
    public SpawnItem[] itemsToSpawn;

    Instead of the List. I just prefer the latter for most uses.
     
  6. lloydsummers

    lloydsummers

    Joined:
    May 17, 2013
    Posts:
    359
    Sorry I misread what was asked. I assumed when he asked for 'adding more variables to an array' he was seeking a method to resize an array. He actually asking how to use a custom class as a type for an array of objects, which beside being demonstrated as a list, is what you shared.
     
  7. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    I just needed an array (a list is fine what it is really as important as what it does) that when you expand it it says amount, you type a number and click enter, and for everything in the field, there is type, chance, spawn here, ect. for each one.

    I Have not had time to look into lists yet but I think that is what I was reading up on when I remember seeing someone do that. Not sure if that clarified what I wanted or not, but lloydsummers misread it so I thought I might as well TRY and clarify what I meant, not sure if I did or not but oh well...
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    collective term if you don't care about the specific type is "collection" :)

    data holding class or struct certainly sounds the way forward.

    You might consider a editor script if you want to change the default layout. "Custom Property Drawer" would let you change the vertical list style into something a little more compact (i.e. type and chance on the same row), and you could also make an editor script which would also allow you to do things like ensure the chances don't go over 100 or fancy things like that :D
     
  9. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    I wanted to avoid editor scripts if at all possible, maybe I will have to come back later because at the moment I have no idea how to do any of that.
     
  10. lloydsummers

    lloydsummers

    Joined:
    May 17, 2013
    Posts:
    359
    Today is as good a day as any to learn :)

    Every week, I go to the Unity help pages. Find a bunch of references I haven't used in a while. And keep learning and seeing what changed.

    After a while you'll memorize most of it and at least have an understanding of the rest. It's good to learn as you go but you should learn the core first IMO so you are less likely to get frustrated.
     
  11. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    Makes sense, however the basics are boring, I tried reading up on things, doesn't work for me I get bored and dive right back in XD
     
  12. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
  13. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    no that is not what I am trying to describe, I think a list will do what I need but I will try and clarify once more I guess.
    Imagine an empty array in the editor.
    then you type in, say 2, and click enter.
    now instead of 2 blank fields show up, asking for a model (GameObject), a spawnChance(float or double), a min and max size(float or double), ect. for each one.
    instead of having to make multiple arrays for every one of each object the spawn chance and sizes are attached so that the values for each one are easier to keep track of, and easy to keep the amount the same. That was just an example, but the idea should be there more clearly (I am usually not able to describe what I want clearly).
     
  14. lloydsummers

    lloydsummers

    Joined:
    May 17, 2013
    Posts:
    359
    While I understand you don't want to learn Custom Property Drawers, I don't think you can create an editor-level serializable list (although I haven't stopped to test it). I don't have time to create one for you, so you might want to put this to the side for now...
     
  15. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    I understand that and will return later when I have the time, I recently decided to restructure and rebuild the whole game, it was originally made as a learning project and I learned enough that a lot of the existing scripts and mesh and everything I could do better on a lot of it, so I decided to just restructure everything. so because of that a lot of the little things got pushed aside like this. I will eventually have or make time for learning how to do this but for now I will just store this stuff in separate arrays and work from there. thanks fro all the help so far, I guess this project will just have to wait for a later day..
     
  16. lloydsummers

    lloydsummers

    Joined:
    May 17, 2013
    Posts:
    359
    Sounds like a good idea, getting core system work down first is an ideal way to go too.