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 Razputin · Mar 13, 2014 at 01:47 AM · buildrandomsavelevelsettings

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.

Comment
Add comment
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

1 Reply

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

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:

  1. Save the values when generating the level to a file

  2. Mark the level as "generated"

  3. When loading a level, check if it's been flagged as "generated"

  4. If so, load the level from the file you saved in step 1

  5. Else, generate the level, repeat.

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 Razputin · Mar 13, 2014 at 03:26 AM 0
Share

Interesting, now to just figure out how to actually implement it.

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Distribute terrain in zones 3 Answers

Post-Build Custom Level Loading 2 Answers

Settings/skills not carrying over to build 0 Answers


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