- Home /
How to save a scene that you randomly generated as a new level in your build settings?
I am randomly generating the overworld of my game. When the player enters a city they are loaded into a level of that city. Then when they exit the level I would like them to return to the same previously randomly generated overworld, however currently when they return it generates a new overworld.
So I suppose my question is, how would I save the generated world to return to later?
Here is how i'm generating the world.
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 tree;
public GameObject wolf;
public GameObject bandit;
public int randNum = 0;
public int xCubes;
public int zCubes;
public bool fighting = false;
public bool enterWolf = false;
void Start () {
GenerateLandscapeAndObjects(xCubes,zCubes);
GenerateBorder(xCubes,zCubes);
}
private void GenerateLandscapeAndObjects(int xMax, int zMax) {
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.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
{
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,z), Quaternion.identity,landscape.transform);
}
}
}
}
}
private void GenerateBorder(int xMax, int zMax) {
GameObject borderGameObject = new GameObject("Landscape and Objects");
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);
}
}
private void DoInstantiate(GameObject prefab, Vector3 position, Quaternion rotation, Transform parent) {
Transform temp = ((GameObject)Instantiate(prefab,position,rotation)).transform;
temp.parent = parent;
}
Heres how I get inside buildings
if(guiOn && enterCity == true)
{
if(GUI.Button(new Rect(10,200,1200,100), "Visit the inn"))
{
Application.LoadLevel("InsideTheInn");
}
And heres how I return to the overworld.
public class InnCameraScript : MonoBehaviour {
void OnGUI(){
if(GUI.Button(new Rect(10,400,1200,100), "Leave the tavern.")){
}
Application.LoadLevel("Map Creator");
}
}
However obviously that's where it creates a new map because its loading the first script again.
So my question is how would I make that first script save the world it generated as a new level in the build settings so that I could refer to it later in scripts similar to the third script I posted.
Answer by herpanda · Mar 13, 2014 at 02:17 AM
You can have the Map Creator save the values generated to a text file (or however you like storing data) while checking to see if the name of the file is unique. Then create another class called MapLoader that takes in the name of the file, parses the file, and generates your level like how your Map Creator does, but instead reading from a file instead of randomly generating it.
Before you load your level, you can have a check if that map has already been randomly generated. For example,
//pseudocode
if(city1Generated){
Application.LoadLevel("MapLoader");
}else{
Application.LoadLevel("MapCreator");
}
That code probably doesn't do what I think it does, but the basic idea is there:
Save the values when generating the level to a file
Mark the level as "generated"
When loading a level, check if it's been flagged as "generated"
If so, load the level from the file you saved in step 1
Else, generate the level, repeat.
Interesting, now to just figure out how to actually implement it.