- Home /
How to save a level that was randomly generated, so it can be returned to later.
I have kind of an idea of what I want to do here, but I don't know how to implement it. Basically the over world of my game is created using this CubeGrid script.
public class CubeGrid : MonoBehaviour {
//these are now GameObjects so you can use Prefabs to define what they are
public GameObject border;
public GameObject grassLand;
public GameObject house;
public GameObject city;
public GameObject graveyard;
public GameObject desert;
public GameObject tree;
public GameObject wolf;
public GameObject bandit;
public int randNum = 0;
public int xCubes;
public int zCubes;
public int desertCreated = 0;
public bool fighting = false;
public bool enterWolf = false;
static bool spawned = false;
void Start ()
{
if(spawned == false)
{
GenerateLandscapeAndObjects(xCubes,zCubes);
GenerateBorder(xCubes,zCubes);
}
else
{
DestroyImmediate(gameObject);
}
}
void Awake()
{
}
private void GenerateLandscapeAndObjects(int xMax, int zMax) {
//create a GameObject to contain the landscape and objects
//to keep things neat in the Hierarchy tab if you decide
//to start generating huge landscapes later on
GameObject landscape = new GameObject("Landscape and Objects");
for ( int z = 0; z < zMax; z++ )
{
for ( int x = 0; x < xMax; x++ )
{
float rnd = Random.value;
if ( rnd < 0.01 )
{
DoInstantiate(city, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
}
// else if (rnd < 0.3 && desertCreated < 19 && z > 0)
// {
// DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
// x++;
// DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
// x++;
// DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
// x++;
// DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
// x++;
// DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
// x++;
//DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
// x++;
// DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
//x++;
// DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
// x++;
// DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
// x++;
// DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
//x++;
// DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
// x++;
// DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
// x++;
// DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
// desertCreated++;
//}
else if (rnd < 0.07)
{
DoInstantiate(tree, new Vector3(x,0,z), Quaternion.identity,landscape.transform);
}
else if ( rnd < 0.15 )
{
DoInstantiate(house, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
}
else if (rnd < 0.18)
{
DoInstantiate(graveyard, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
}
else
{
DoInstantiate(grassLand, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
randNum = Random.Range(0,50);
if(randNum <= 3){
DoInstantiate(wolf,new Vector3(x,0,z), Quaternion.identity,landscape.transform);
}
else if(randNum ==4){
DoInstantiate(bandit, new Vector3(x,0.1f,z), Quaternion.identity,landscape.transform);
}
}
}
}
}
private void GenerateBorder(int xMax, int zMax) {
//create a GameObject to contain the border
//to keep things neat in the Hierarchy tab if you decide
//to start generating huge landscapes later on
GameObject borderGameObject = new GameObject("Landscape and Objects");
//create border blocks along x axes
for ( int x = 0; x < xMax; x++ )
{
DoInstantiate(border, new Vector3(x,1,0), Quaternion.identity,borderGameObject.transform);
DoInstantiate(border, new Vector3(x,1,zMax), Quaternion.identity,borderGameObject.transform);
}
//create border blocks along z axes
for ( int z = 0; z < zMax; z++ )
{
DoInstantiate(border, new Vector3(0,1,z), Quaternion.identity,borderGameObject.transform);
DoInstantiate(border, new Vector3(xMax,1,z), Quaternion.identity,borderGameObject.transform);
}
}
//A helper function to let you organise GameObjects easily in
//the hierarchy by setting the transform.parent of the instantiated prefab.
private void DoInstantiate(GameObject prefab, Vector3 position, Quaternion rotation, Transform parent) {
Transform temp = ((GameObject)Instantiate(prefab,position,rotation)).transform;
temp.parent = parent;
}
}
I would like to save the results I get from this as their own level in the build settings. So the first time the game starts it would go down something like this
MapCreated = false; Loadlevel MapCreator MapCreated = true; Then save that map in the build settings(Which Idk how to do) as like OverWorld so that whenever I have to return to the overworld I could load the OverWorld level that was created. Currently I always return to the MapCreator level (Which means everytime I go back to the overworld its recreated randomly and is a brand new world).
Hopefully i've explained what i'm trying to do in a good enough way for you to understand.
Answer by robertbu · May 19, 2014 at 04:15 PM
What I would do is generate and save the 'seed' used to generate the level. Then when you go back, just use the seed for the random generator to reproduce the same level using your code above. To make this happen, I'd use .NET Random class rather than Unity's Random class. For .Net you have to create an instance of the class. Note if you add 'using System; you will get conflicts between the two classes, but you can get access doing:
System.Random rand1 = new System.Random(seed);
The seed itself you can generate using Unity's Random.Range or by using DateTime. Once you have a level you want to save, use PlayerPrefs to save the seed and then reuse it the next time.
You'll need to rewrite you use of Random in the above code. Instead of Random.Range() for example, you would do:
int r = rand1.Next(10, 60);
I've haven't used seeds before so I'll probably have a hard time figuring this out. But i'm going to give it a try because that does seem like a good idea, thank you!
Actually... that was rather easy... O.O I just got it working in like a second haha thank you so much!
Your answer
Follow this Question
Related Questions
How to save a scene that you randomly generated as a new level in your build settings? 1 Answer
How to save a scene that you randomly generated as a new level in your build settings? 1 Answer
Post-Build Custom Level Loading 2 Answers
Distribute terrain in zones 3 Answers
Dialog Box in Build Settings 1 Answer