Search Unity

How to spread a hefty 250 000 - 1 000 000 loop using a coroutine

Discussion in 'Scripting' started by FisherM, Oct 29, 2014.

  1. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    I need to execute ruffly 250 000 - 1 000 000 raycasts, currently I just have it in a Start() function

    However if I could use a co routine to A) not overload and crash the game and B) take a minute to set up the grid. How would I do it
    Code (CSharp):
    1.  
    2.         void Start () {
    3.             startWorld ();
    4.         }
    5.  
    6.         void startWorld(){
    7.             for(int xpos = 0; xpos < radius; xpos++){
    8.                 for(int ypos = 0; ypos < radius; ypos++){
    9.                     Debug.Log ( ypos + " < y ? x > " + xpos + " radius = " + radius);
    10.                     Vector3 dwn = transform.TransformDirection(Vector3.down);
    11.                     Vector3 zero = Vector3.zero + Vector3.up * 100 + Vector3.right * xpos + Vector3.forward * ypos;
    12.                     RaycastHit hit;
    13.                     if (Physics.Raycast (zero, dwn, out hit, 500)){
    14.                         putCube (hit.point);
    15.                     }
    16.                 }
    17.             }
    18.             Debug.Log ("done");
    19.          
    20.         }
    21.  
    22. void putCube(Vector3 spawnpoint){
    23.             Vector3 _Location = new Vector3(Mathf.Round(spawnpoint.x), Mathf.Round(spawnpoint.y) - 1, Mathf.Round(spawnpoint.z));
    24.  
    25.             //Debug.Log ((int) _Location.x + "" + (int) _Location.y+ "" +(int) _Location.z);
    26.             world.MarkPosition(new Point3D((int) _Location.x,(int) _Location.y,(int) _Location.z), true);
    27.         }
     
  2. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    I am also considering precomputing this but I'm not sure how I would do that. Would anyone recommend I do so or have any suggestions about how to do it?
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    So, what are you hitting with your raycasts?

    If it's some kind of pre-made terrain, precomputing would be easy. Use the same script, and make it available in edit mode.

    You'll really want to make a custom editor script, but to prototype it, just remove startWorld from your Start, and add [[ContextMenu("startWorld")] on top of startWorld:

    Code (csharp):
    1. [[ContextMenu("startWorld")]
    2. void startWorld() { ... }
    That'll add the command 'startWorld' to the script's tool bar (cogwheel button). Check if it works!


    Also, what the heck are you doing? Making a 500*500 grid of blocks? Is this another Minecraft clone in the works?
     
  4. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    no it is not anything like minecraft haha. The pathfinding is just similar
    Problem with that is that it is building up a set of false coords on line 26 above.

    world.MarkPosition(new Point3D((int) _Location.x,(int) _Location.y,(int) _Location.z), true);

    so below is the world class

    as you can see the class is initialized at start and then the pathfinding using points and a* to get a path.

    I'm not sure how I would go about precomputing when this class isn't

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5.  
    6. namespace Tests
    7. {
    8.     /// <summary>
    9.     /// Author: Roy Triesscheijn (http://www.roy-t.nl)
    10.     /// Sample World class that only provides 'is free or not' information on a node
    11.     /// </summary>
    12.     public class World
    13.     {
    14.         private int sx;
    15.         private int sy;
    16.         private int sz;
    17.         private int offsetIdx;
    18.         private bool[] worldBlocked; //extremely simple world where each node can be free or blocked: true=blocked      
    19.  
    20.         //Note: we use Y as height and Z as depth here!
    21.         public int Left { get { return 0; } }
    22.         public int Right { get { return sx - 2; } }
    23.         public int Bottom { get { return 0; } }
    24.         public int Top { get { return sy - 2; } }
    25.         public int Front { get { return 0; } }
    26.         public int Back { get { return sz - 2; } }
    27.  
    28.         /// <summary>
    29.         /// Creates a 2D world
    30.         /// </summary>      
    31.         public World(int width, int height)
    32.             : this(width, height, 1)
    33.         { }
    34.  
    35.         /// <summary>
    36.         /// Creates a 3D world
    37.         /// </summary>      
    38.         public World(int width, int height, int depth)
    39.         {
    40.             // add 2 to compensate for the solid border around the world
    41.             sx = width + 2;
    42.             sy = height + 2;
    43.             sz = depth + 2;
    44.             offsetIdx = (0 + 1) + ((0 + 1) + (0 + 1) * sy) * sx;
    45.             //offsetIdx = -250;
    46.             worldBlocked = new Boolean[sx * sy * sz];
    47.  
    48.             // add solid border
    49.             for (int x = 0; x < sx; ++x)
    50.                 for (int y = 0; y < sy; ++y)
    51.                 {
    52.                     MarkPositionEx(new Point3D(x, y, 0), true);
    53.                     MarkPositionEx(new Point3D(x, y, sz - 1), true);
    54.                 }
    55.  
    56.             for (int y = 0; y < sy; ++y)
    57.                 for (int z = 0; z < sz; ++z)
    58.                 {
    59.                     MarkPositionEx(new Point3D(0, y, z), true);
    60.                     MarkPositionEx(new Point3D(sx - 1, y, z), true);
    61.                 }
    62.  
    63.             for (int z = 0; z < sz; ++z)
    64.                 for (int x = 0; x < sx; ++x)
    65.                 {
    66.                     MarkPositionEx(new Point3D(x, 0, z), true);
    67.                     MarkPositionEx(new Point3D(x, sy - 1, z), true);
    68.                 }
    69.         }
    70.  
    71.         /// <summary>
    72.         /// Mark positions in the world als blocked (true) or unblocked (false)
    73.         /// </summary>
    74.         /// <param name="value">use true if you wan't to block the value</param>
    75.         public void MarkPosition(Point3D position, bool value)
    76.         {
    77.             worldBlocked[offsetIdx + position.X + (position.Y + position.Z * sy) * sx] = value;
    78.         }
    79.  
    80.         private void MarkPositionEx(Point3D position, bool value)
    81.         {
    82.             worldBlocked[position.X + (position.Y + position.Z * sy) * sx] = value;
    83.         }
    84.  
    85.         /// <summary>
    86.         /// Checks if a position is free or marked (and legal)
    87.         /// </summary>      
    88.         /// <returns>true if the position is free</returns>
    89.         public bool PositionIsFree(Point3D position)
    90.         {
    91.             return
    92.                 !worldBlocked[offsetIdx + position.X + (position.Y + position.Z * sy) * sx];
    93.         }
    94.  
    95.         public bool PositionIsGrounded(Point3D position){
    96.             return
    97.                 worldBlocked[offsetIdx + position.X + (position.Y - 1 + position.Z * sy) * sx];
    98.         }
    99.     }
    100. }
    101.