- Home /
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++;
}
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?
Ad$$anonymous$$s this has been answered by VesuvianPrime. Thank you
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:
Create a 2D array that is going to hold references to tiles in your game world.
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.
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.
Your answer
Follow this Question
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