Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Critcher · Dec 11, 2014 at 10:22 PM · randomgeneratormazedungeon

Starting a roguelike, maze generator problem.

Hi all,

I'm working on a level generator and, it works exactly how I want it to but it is so very slow it could take up to 10 seconds to generate the map. Is there anywhere that I can speed this script up? Thanks for looking!

 var myX : int;
 var myZ : int;
 var i : int;
 var maxTiles : int;
 var tileSpin : int;
 var tilePrefab : Transform;
 var lastTile : Transform;
 var lookDirection : int;
 var locationVector : Vector3;
 static var tiles : Array;
 
 function Awake()
 {
 tiles = new Array();
 locationVector=(Vector3.zero);
 tilePrefab = Instantiate(tilePrefab,locationVector, Quaternion.identity);
 tiles.push(locationVector);
 }
 function Update()
 {    
     if (i == maxTiles){Destroy(gameObject,0);}
     tileSpin = Random.Range (0,4);
     lookDirection = Random.Range(0,5);
     Builder();
 }    
 function Builder ()
 {
     switch (tileSpin) //rotate the cubes at the right angles
     {
         case (1): tileSpin =  90; break;
         case (2): tileSpin = 180; break;
         case (3): tileSpin = 270; break;
     }
     
     switch (lookDirection) // 4 primaries, I used to have all 8 but it generated ugly and got stuck a lot
     {
         case (1): locationVector = Vector3(myX,  0,myZ+1);break;
         case (2): locationVector = Vector3(myX+1,0,myZ)  ;break;
         case (3): locationVector = Vector3(myX,  0,myZ-1);break;
         case (4): locationVector = Vector3(myX-1,0,myZ)  ;break;
     }    
     for (var a = 0; a < tiles.length; a++)  //  I bet it's in this loop here cause of cycling the array everytime
     {
         if (locationVector == tiles[a])        //  but then... how to accomplish the check?
         {
         myX = locationVector.x;
         myZ = locationVector.z;
         return;
         }
     }
     if (i==maxTiles-1)
     {
         lastTile = Instantiate (lastTile,locationVector, Quaternion.identity);
         tiles.push(locationVector);
         i++;
         return;
     }
     
     tilePrefab = Instantiate (tilePrefab,locationVector, Quaternion.Euler(0, tileSpin, 0));
     myX = locationVector.x;
     myZ = locationVector.z;
     tiles.push(locationVector);
     i++;
 }    
 
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VesuvianPrime · Dec 11, 2014 at 10:22 PM 0
Share

How many tiles are you instantiating?

avatar image Critcher · Dec 11, 2014 at 10:36 PM 0
Share

Only about 200, even at 100 though it seems quite laggy. I realized as well that I must have as many or more bad directions as good ones, maybe that as well?

avatar image Critcher · Dec 11, 2014 at 10:50 PM 0
Share

Ad$$anonymous$$s this has been answered by VesuvianPrime. Thank you

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by VesuvianPrime · Dec 11, 2014 at 10:42 PM

It's inefficient sure, but I wouldn't expect it to take 10 seconds to complete.

Try swapping out the tile prefab for a default cube. This should tell you if the problem is the algorithm or the complexity of the assets.

I think your approach to the algorithm can be improved significantly:

  1. Create a 2D array that is going to hold references to tiles in your game world.

  2. Every time you test to see if a tile exists already, look it up in the 2D array INSTEAD of looping over all tiles and comparing position.

  3. When you instantiate a tile add it to the 2D array in the relevant location.

This way you will only ever check 1 value every loop instead of 200.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Critcher · Dec 11, 2014 at 10:46 PM 0
Share

Hah! That is the exact right thing to do. Thank you.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Randomly generated number 0 Answers

Random.Range(..) not working 1 Answer

Weighted Item Generator returns same Item 2 Answers

ArgumentException: RandomRangeInt can only be called from the main thread. 1 Answer

Create passages in round maze 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges