- 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.
That seems like what I would need to use, but i'm not sure how it gets implemented into the code. Does it go in at the end of each time I generate something? private void GenerateBorder(int x$$anonymous$$ax, int z$$anonymous$$ax) {
`
GameObject borderGameObject = new GameObject("Landscape and Objects");
for ( int x = 0; x < x$$anonymous$$ax; x++ )
{
DoInstantiate(border, new Vector3(x,1,0), Quaternion.identity,borderGameObject.transform);
DoInstantiate(border, new Vector3(x,1,z$$anonymous$$ax), Quaternion.identity,borderGameObject.transform);
}
for ( int z = 0; z < z$$anonymous$$ax; z++ )
{
DoInstantiate(border, new Vector3(0,1,z), Quaternion.identity,borderGameObject.transform);
DoInstantiate(border, new Vector3(x$$anonymous$$ax,1,z), Quaternion.identity,borderGameObject.transform);
}
DontDestroyOnLoad(transform.gameObject);
}`
Ahh okay its working, now I just have to add a variable so it doesnt create a new world, and save the old one each time. Right now its for$$anonymous$$g a second random world on top of the old one. But I should be able to fix that easy. Thanks for the help, that was pretty simple.
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 level that was randomly generated, so it can be returned to later. 1 Answer
How can i make an infinite level? 4 Answers
Post-Build Custom Level Loading 2 Answers
Randomly Generated 2D Level 1 Answer