Search Unity

How to improve speed of array creation or population at start

Discussion in 'Scripting' started by Velo222, Jan 19, 2014.

  1. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Hello,

    I am attempting to create a fairly large grid of square tiles where each "tile" (or location on the grid) can hold 5-6 variables, such as position information, integer values, and a couple of boolean variables.

    In essence, I need some sort of "container" if you will that can hold some variables at each location. My solution was to create an array of classes, that I can then iterate through and set variables within the class at each tile location. The array itself will not change in size, and the information in the class variables will always be the same at the start of the game (i.e. it's always going to be the same over and over again for the same map, the size and the information the classes contain).

    This actually works great except for one big drawback, it takes about a minute to 2 minutes to create the class instances and put them all into an array at the start of my game (or any map for that matter). And this is actually an eternity when the player is waiting for the map to load.

    So, I was wondering if there was a better way to do it, first of all, and secondly if there was some way to save the array starting state in a file somewhere that I could just load quickly at the start of the game?

    I'm kind of new to this, but I would think there would be some way to save an array and it's data in a way that could quickly be referenced or looked up when the game loads? And again, if there's a faster (more performant) way to create containers to store variables on a grid, I'm open to suggestions.



    Below I've put the function that I'm using to create the classes and populate the array, and I'm using javascript by the way:

    Some of the variables:
    Code (csharp):
    1. public var infArrayTiles = new List.<InfMapData1>();
    2.  
    3. //An object that Unity can add all the new "class" scripts to, to keep from getting 1000's of errors at start
    4. //You cannot add Monobehaviors using the "new" keyword for some reason, you must use "AddComponent()".
    5. private var mapObject : GameObject;
    6.  
    The function itself:
    Code (csharp):
    1. function CreateGridTiles () {
    2.    
    3.     xMinGrid = 185;
    4.     xMaxGrid = 278;
    5.    
    6.     zMinGrid = 176;
    7.     zMaxGrid = 244;
    8.    
    9.     //For every "one" z increment on the game map, create 100 "x's" (just for example)
    10.     //This way, the map is created going horizontally, instead of vertically
    11.     for(var z = zMinGrid; z < zMaxGrid; z++) {
    12.                
    13.         for(var x = xMinGrid; x < xMaxGrid; x++) {
    14.                                    
    15.             //The InfMapData1 script is not attached to any gameobject, but is simply in the assets folder.  
    16.             //It is a class unto itself, simply by being a Javascript script.  Each separate script in Javascript is treated as a class automatically by Unity.
    17.             //var tempClass2 = new InfMapData1();//Create a new instance of the InfMapData1 class
    18.            
    19.             //**Note:  Using the "new" keyword when creating class instances was causing 1000's of errors and slowing down the start of the
    20.             //game by a large amount of time.  To fix this, I must use "AddComponent()" on an actual gameobject.  I don't know why yet.
    21.             var tempClass2 = mapObject.AddComponent(InfMapData1);
    22.            
    23.             tempClass2.position = Vector3(x, 0, z);//Set this unique class instance's position variable to a value
    24.            
    25.             //Populate the map tiles' array
    26.             infArrayTiles.Add(tempClass2);//Add it the the array's generic List;
    27.            
    28.         }
    29.     }
    30.    
    31.     //The map object is only needed to keep Unity from throwing thousands of errors and slowing down the map creation process
    32.     //at start.  After the tiles array is created, the object can be destroyed as it is no longer needed.
    33.     Destroy(mapObject);
    34.  
    35. }
    36.  
     
    Last edited: Jan 19, 2014
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Any class that inherits from MonoBehaviour must be attached to a GameObject as a component because it would make no sense for it to exist without a GameObject. If you're not using MonoBehaviour functionality (Start, Awake, Update, etc.) then don't inherit from MonoBehaviour.

    Creating and populating arrays of a class that has 5-6 variables is trivial and you would normally be able to initialize millions (billions?) of entries per second. I expect that adding zillions of components is the problem. Also, if the array isn't going to change size, you'd be better off using an array instead of a generic List.

    --Eric
     
  3. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Hey Eric,

    You're right about the creation and population of the array (as usual lol). I guess I didn't test the function "alone" properly, and now I see it actually creates the grid fairly quickly -- so that wasn't my problem. Which is good.

    However, I still have the problem of a function I use to generate some information on each tile. I have to do a lot of distance checks and whatnot to calculate the proper value to give each tile. The value generated by this function (which I can give if it matters) is always the same value, as I stated in my first post. So, I know what value I need to assign to each tile.....but how can I set these values without having to redo the distance calculations at the start of the game every time?


    For example, lets say I know tile #5 (which would be myarray[4] lets say) needs an integer value of "6". Which is calculated and populated based off of this function that is taking a long time. But I know that every single time I load my game, that tile will always have a value of "6".

    So can I do anything to populate the array quicker -- the only thing I can think of is to manually tell each and every slot of my array what it's value needs to be -- but if there are 2,000 tiles (and 2,000 array slots subsequently), that seems a little ridiculous.

    Any suggestions?


    P.S. @Eric5h5, I read one of your articles and you stated that javascript arrays were almost never needed, and that generic lists were always better. What happened to that? lol Just curious in this case. I appreciate your help Eric :)