Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 jobothehobo · May 29, 2020 at 08:39 AM · loadingsaveloadnoiseterrain generationsave game

What's the best way to save and load a world with easy save 3?

Im working on a game where I have the terrain procedurally generated, and I downloaded easy save 3 off of the asset store but I don't know the best way to save my trees, animals, and enemies. The game is 2d and I use perlin noise to generate the tilemap for the terrain, and use the noise to say if a random number is a certain amount and the tile is grass, then spawn a tree. This breaks when I quit the game and load the world up again, because all the trees are randomly generated again. So the terrain is the same, but everything else isn't. I've tried to store all the existing trees into a game object list, but when I loaded the world everything was duplicated and it took forever to load when I had a lot of trees in the world. I also thought of the idea of using a different noise pattern on top of the terrain to spawn the trees but then the problem would be if the tree is chopped down by the player, than it would be fully grown again. Hopefully that all made sense and im hoping there's an easy solution to this. This is the code for generating the world:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Tilemaps;
 
 public class MapGenerator : MonoBehaviour
 {
     public Tilemap ground;
     public Tilemap ground2;
     public Tilemap groundTop;
     public Tilemap waterTiles;
     public RuleTile baseGrass;
     public RuleTile water;
     public Tile[] flower;
     public Tile[] sand;
     public Tile grass1;
     public Tile grass2;
     public Tile grass3;
 
     public int width;
     public int height;
 
     public GameObject player;
     public GameObject saveMan;
 
     public List<GameObject> spawners;
     public GameObject spawner;
     public List<GameObject> treeSpawners;
     public GameObject treeSpawner;
     public List<GameObject> enemySpawners;
     public GameObject enemySpawner;
     public List<GameObject> animalSpawners;
     public GameObject animalSpawner;
 
     public float scale;
     public float offsetX;
     public float offsetY;
 
     public GameObject bossShrine;
     public bool shrineSpawned;
     public int randX;
     public int randY;
 
     private int tileNumb;
 
     public int distanceX;
     public int distanceY;
 
     public int islandSize;
     public int villageSize;
 
     public int villageOffsetX;
     public int villageOffsetY;
 
     public int villageRadius;
 
     public GameObject village;
 
     public int chunkSize;
     public int offLoad;
 
     public GameObject invent;
     private InventoryNum inventory;
 
     private bool cooldown;
     private int randTile;
     public int sam;
 
     public bool loadingWorld;
 
     public Texture2D treeNoise;
 
     // Start is called before the first frame update
     void Start()
     {
         GameObject playerCopy = Instantiate(player, transform.position, transform.rotation, transform.parent);
         player = playerCopy;
 
         villageOffsetX = Random.Range(-villageRadius, villageRadius);
         villageOffsetY = Random.Range(-villageRadius, villageRadius);
 
         Renderer renderer = GetComponent<Renderer>();
 
         //renderer.material.mainTexture = GenerateTexture();
 
         int[,] map = new int[width, height];
 
         invent = GameObject.FindWithTag("inventoryGrid");
 
         SetWorld();
         
 
         SpawnPlayer();
     }
 
     public void SetWorld()
     {
 
         if (loadingWorld)
         {
             if (ES3.KeyExists("offsetX"))
             {
                 offsetX = ES3.Load<float>("offsetX");
             }
             if (ES3.KeyExists("offsetY"))
             {
                 offsetY = ES3.Load<float>("offsetY");
             }
             if (ES3.KeyExists("playerPos"))
             {
                 player.transform.position = ES3.Load<Vector3>("playerPos");
             }
             if (ES3.KeyExists("playerWeapon"))
             {
                 player.GetComponent<PlayerController>().LoadWeapon();
             }
             if (ES3.KeyExists("playerHealth"))
             {
                 player.GetComponent<PlayerController>().health = ES3.Load<float>("playerHealth");
                 player.GetComponent<PlayerController>().UpdateHealthbar();
             }
 
             for (int i = 0; i < spawners.Count; i++)
             {
                 Destroy(spawners[i]);
             }
             for (int i = 0; i < treeSpawners.Count; i++)
             {
                 Destroy(treeSpawners[i]);
             }
             for (int i = 0; i < enemySpawners.Count; i++)
             {
                 Destroy(enemySpawners[i]);
             }
             for (int i = 0; i < animalSpawners.Count; i++)
             {
                 Destroy(animalSpawners[i]);
             }
 
             ground.ClearAllTiles();
             ground2.ClearAllTiles();
             groundTop.ClearAllTiles();
             waterTiles.ClearAllTiles();
 
             spawners = new List<GameObject>();
             treeSpawners = new List<GameObject>();
             enemySpawners = new List<GameObject>();
             animalSpawners = new List<GameObject>();
 
             
 
         }
         else
         {
             offsetX = Random.Range(0, 99999f);
             offsetY = Random.Range(0, 99999f);
         }
 
         
     }
 
 
     public void NewWorld()
     {
         ground.ClearAllTiles();
         ground2.ClearAllTiles();
         groundTop.ClearAllTiles();
         waterTiles.ClearAllTiles();
 
         for (int i = 0; i < spawners.Count; i++)
         {
             Destroy(spawners[i]);
         }
         for (int i = 0; i < treeSpawners.Count; i++)
         {
             Destroy(treeSpawners[i]);
         }
         for (int i = 0; i < enemySpawners.Count; i++)
         {
             Destroy(enemySpawners[i]);
         }
         for (int i = 0; i < animalSpawners.Count; i++)
         {
             Destroy(animalSpawners[i]);
         }
 
         spawners = new List<GameObject>();
         treeSpawners = new List<GameObject>();
         enemySpawners = new List<GameObject>();
         animalSpawners = new List<GameObject>();
 
         offsetX = Random.Range(0, 99999f);
         offsetY = Random.Range(0, 99999f);
 
         inventory = invent.GetComponent<InventoryNum>();
         for (int i = 0; i < inventory.slots.Length; i++)
         {
             inventory.slots[i].GetComponent<InventorySlot>().SetDefaults();
         }
 
         SpawnPlayer();
 
         loadingWorld = false;
     }
 
     // Update is called once per frame
     void Update()
     {
 
         if (player.GetComponent<PlayerController>().health <= 0)
         {
             player.GetComponent<SpriteRenderer>().color = new Color(255, 255, 255);
             inventory = invent.GetComponent<InventoryNum>();
 
             for (int i = 0; i < inventory.slots.Length; i++)
             {
                 if (inventory.slots[i].GetComponent<InventorySlot>().id > 0)
                 {
                     for (int x = 0; x < inventory.slots[i].GetComponent<InventorySlot>().value; x++)
                     {
                         Instantiate(inventory.items[inventory.slots[i].GetComponent<InventorySlot>().id - 1], player.transform.position, player.transform.rotation);
                     }
 
                     inventory.slots[i].GetComponent<InventorySlot>().id = 0;
                     inventory.slots[i].GetComponent<InventorySlot>().value = 0;
                     inventory.slots[i].GetComponent<InventorySlot>().full = false;
                     Destroy(inventory.slots[i].GetComponent<InventorySlot>().child);
                     Destroy(player.GetComponent<PlayerController>().currentWeapon);
                 }
             }
 
             if (!cooldown)
             {
                 StartCoroutine(SpawnCooldown());
                 cooldown = true;
             }
         }
 
         /*
         for (int x = -offLoad; x < offLoad; x++)
         {
             for (int y = -offLoad; y < offLoad; y++)
             {
                 if (x > chunkSize || x < -chunkSize)
                 {
                     if (y > chunkSize || y < -chunkSize)
                     {
                         Vector3 playerPos = new Vector3((int)player.transform.position.x + x, (int)player.transform.position.y + y, 0);
 
                         Vector3Int tilePos = groundTop.WorldToCell(playerPos);
 
                         if (ground.GetTile(tilePos) != null)
                         {
                             ground.SetTile(tilePos, null);
                         }
                         if (ground2.GetTile(tilePos) != null)
                         {
                             ground2.SetTile(tilePos, null);
                         }
                         if (groundTop.GetTile(tilePos) != null)
                         {
                             groundTop.SetTile(tilePos, null);
                         }
                         if (waterTiles.GetTile(tilePos) != null)
                         {
                             waterTiles.SetTile(tilePos, null);
                         }
                     }
                 }
             }
         }
         */
 
 
         for (int x = -chunkSize; x < chunkSize; x++)
         {
             for (int y = -chunkSize; y < chunkSize; y++)
             {
                 
 
                 Vector3 playerPos = new Vector3((int)player.transform.position.x + x, (int)player.transform.position.y + y, 0);
 
                 Vector3Int tilePos = groundTop.WorldToCell(playerPos);
 
                 if (groundTop.GetTile(tilePos) == null)
                 {
                     
                     CalculateColor((int)player.transform.position.x + x, (int)player.transform.position.y + y);
                 }
                 
 
 
                 if (ground2.GetTile(tilePos) == null)
                 {
                     int randNumb = Random.Range(0, 100);
                     int numb = 0;
 
                     if (randNumb < 90)
                     {
                         numb = 0;
                     }
                     else
                     {
                         if (randNumb > 92 && randNumb < 95)
                         {
                             numb = 0;
                         }
                         else
                         {
                             if (randNumb > 95 && randNumb < 98)
                             {
                                 numb = 1;
                             }
                             else
                             {
                                 if (randNumb > 98)
                                 {
                                     numb = 2;
                                 }
                                 else
                                 {
                                     numb = 0;
                                 }
                             }
                         }
                     }
                     ground2.SetTile(tilePos, sand[numb]);
                 }
 
                 
             }
         }
 
     }
 
     public void SpawnPlayer()
     {
         Vector3 playerPos2 = new Vector3((int)player.transform.position.x, (int)player.transform.position.y, 0);
 
         Vector3Int tilePos = groundTop.WorldToCell(playerPos2);
 
         if (waterTiles.GetTile(tilePos) != null)
         {
             player.transform.position = new Vector3(player.transform.position.x + 1, player.transform.position.y, 0);
             SpawnPlayer();
         }
         else
         {
             if (player.GetComponent<PlayerController>().health <= 0)
             {
                 player.GetComponent<PlayerController>().health = player.GetComponent<PlayerController>().maxHealth;
                 player.GetComponent<PlayerController>().SetSize(player.GetComponent<PlayerController>().health / player.GetComponent<PlayerController>().maxHealth);
             }
             player.SetActive(true);
             if (!loadingWorld)
             {
                 player.transform.position = new Vector2(0, 0);
                 player.GetComponent<PlayerController>().health = player.GetComponent<PlayerController>().maxHealth;
                 player.GetComponent<PlayerController>().SetSize(player.GetComponent<PlayerController>().health / player.GetComponent<PlayerController>().maxHealth);
             }
             
             cooldown = false;
         }
     }
 
     IEnumerator SpawnCooldown()
     {
         player.SetActive(false);
         yield return new WaitForSeconds(5.1f);
         SpawnPlayer();
     }
 
     Texture2D GenerateTexture()
     {
         Texture2D texture = new Texture2D(width, height);
 
         randX = Random.Range(0, width);
         randY = Random.Range(0, height);
 
         for (int x = -(width / 2); x < width / 2; x++)
         {
             for (int y = -(height / 2); y < height / 2; y++)
             {
                 
                 //Color color = CalculateColor(x, y);
                 //texture.SetPixel(x, y, color);
                
             }
         }
 
         texture.Apply();
         return texture;
     }
 
     Color CalculateColor(int x, int y)
     {
         float xCoord = (float)x / width * scale + offsetX;
         float yCoord = (float)y / height * scale + offsetY;
 
         float sample = Mathf.PerlinNoise(xCoord, yCoord);
         sample = sample;
 
 
         if (Mathf.Pow(x - villageOffsetX, 2) + Mathf.Pow(y - villageOffsetY, 2) < Mathf.Pow(villageSize, 2))
         {
             
             if (x == villageOffsetX && y == villageOffsetY)
             {
                 
             }
         }
         
 
         
 
         if (sample >= 0.4f)
         {
         
 
             ground.SetTile(new Vector3Int((int)x, (int)y, 0), baseGrass);
             
 
             if (sample > 0.5f)
             {
 
                 randTile = Random.Range(0, 100);
                 if (randTile < 90)
                 {
                     groundTop.SetTile(new Vector3Int((int)x, (int)y, 0), grass3);
                 }
                 else
                 {
                     if (randTile > 92 && randTile < 95)
                     {
                         groundTop.SetTile(new Vector3Int((int)x, (int)y, 0), grass1);
                     }
                     else
                     {
                         if (randTile > 95 && randTile < 98)
                         {
                             groundTop.SetTile(new Vector3Int((int)x, (int)y, 0), flower[Random.Range(0, flower.Length)]);
                         }
                         else
                         {
                             if (randTile > 98)
                             {
                                 groundTop.SetTile(new Vector3Int((int)x, (int)y, 0), grass2);
                             }
                             else
                             {
                                 ground.SetTile(new Vector3Int((int)x, (int)y, 0), baseGrass);
                             }
                         }
                     }
 
                     
                     
                 }
 
                 int randSpawn = Random.Range(0, 1000);
 
                 if (randSpawn > 995)
                 {
                     spawners.Add(Instantiate(spawner, new Vector3Int((int)x, (int)y, 0), transform.rotation, transform.parent));
                 }
 
                 if (randSpawn < 11)
                 {
                     treeSpawners.Add(Instantiate(treeSpawner, new Vector3Int((int)x, (int)y, 0), transform.rotation, transform.parent));
                 }
 
                 if (randSpawn > 10 && randSpawn < 12)
                 {
                     enemySpawners.Add(Instantiate(enemySpawner, new Vector3Int((int)x, (int)y, 0), transform.rotation, transform.parent));
                 }
 
                 if (randSpawn > 12 && randSpawn < 17)
                 {
                     animalSpawners.Add(Instantiate(animalSpawner, new Vector3Int((int)x, (int)y, 0), transform.rotation, transform.parent));
                 }
                 
 
             }
 
 
         }
         else
         {
             if(sample < 0.4f && sample > 0.35f)
             {
 
             }
             else
             {
                 waterTiles.SetTile(new Vector3Int((int)x, (int)y, 0), water);
             }
         }
 
         return new Color(sample, sample, sample);
     }
 }
 

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

Answer by javierzagal · Oct 06, 2020 at 02:55 AM

@jobothehobo Did you solve it? I'm having a similar issue. My game is 3d, but for a 2d context as yours something like a dictionary with the tile-type could work, having different letters/strings for each state of the tree. I hope you've solved it! Good luck

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

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

128 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 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 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 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 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 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 avatar image avatar image

Related Questions

my json saves but doesnt load after i exit my game 2 Answers

Generating Chunks of Ore Using Perlin Noise 3 Answers

How to save and load inventory? 1 Answer

Saving & Loading the scene (or at least one array) via Javascript 1 Answer

Advice on 2D Surface Terrain Generation Using Noise 1 Answer


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