- Home /
Save a randomly generated terrain to game file
Hello! I am currently working on a 2D Minecraft-esk game as a learning experience. I have gotten rather far on my own but I have gotten stuck on how to save my map (Blocks and player position). I created a random block generator (It's a bit of a mess, I know, but it works) The issue I am having is saving the prefab copies to a save file without the terrain regenerating on me every time. With all the save methods I have tried every single one just regenerated the terrain.
Bonus Info: I have the terrain generation script attached to my player so as soon as the player is rendered the terrain randomly generates.
using UnityEngine; using System.Collections; public class blockSpawn : MonoBehaviour { %|-1446747765_1|% public GameObject Grass; %|903217905_3|% %|136237991_4|% %|-1251951135_5|% %|-1426623343_6|% %|-2065649517_7|% %|-1819742777_8|% %|1906837029_9|% %|-1986051694_10|% %|1651827282_11|% %|-1108236652_12|% %|670030102_13|% %|-2029995424_14|% %|602125092_15|% %|-1586535938_16|% %|-1923784418_17|% %|-1864907346_18|% %|2135106283_19|% %|-1141352543_20|% %|-102703709_21|% %|1590346120_22|% %|467579366_23|% %|-2115044570_24|% %|-397939301_25|% %|2140486734_26|% %|-1756250263_27|% %|-1587411223_28|% %|686911334_29|% %|-1137264129_30|% %|-1738760240_31|% %|1299188956_32|% %|-1179186729_33|% %|-1930294853_34|% %|1411423665_35|% %|366522794_36|% %|-955403629_37|% %|-1381109886_38|% %|209607248_39|% %|46396959_40|% %|-335559266_41|% %|554706497_42|% %|-99270313_43|% %|2053780166_44|% %|435716658_45|% %|-1615331825_46|% %|823788486_47|% %|1405987283_48|% %|-2130050861_49|% { x = 0; %|23122755_52|% %|-1974208865_53|% %|-898427287_54|% %|-730124794_55|% %|-293419199_56|% %|969071082_57|% %|804195557_58|% %|-698426720_59|% //End of Update }
Random Generation usually just involves a couple of variables that are fed into an equation. Ins$$anonymous$$d of sending ALL the terrainData, just send the values used in the equations (eg random seed). Let each client do the calculation. As long as you are not using Random.Range or value, each client will produce the same output.
and I would also like to know what this is you have pasted
Answer by Pangamini · Oct 26, 2016 at 09:25 AM
You don't want to save prefabs directly, neither of level nor your player object. Unity doesn't natively support any savegame mechanics, but you can write your own.
Basically, you need to extract some data from your game, write it into a file, and later be able to load this data and recreate your game. You don't write entire prefabs, just all data needed to recreate it (and link it back to the game).
A robust automatic system (that utilizes reflection) might take some time, but you can put something simple together rather quicky.
If your level is a simple 2D array of blocks, you could just write: dimensions of your level one byte per block of your level (byte defining a type of the block) Write this data using System.IO library When reading, simply follow the same steps but reversed: Read level dimensions; Then read through the block data, recreating blocks in your game
Your answer
Follow this Question
Related Questions
Create an autosave/checkpoint feature for my game? 1 Answer
Lets talk multiplayer: General Questions? 2 Answers
Making a Script that places Objects at random possition but not at the same as a taken position 1 Answer
Save Game Folder 1 Answer
How to save some integers and bools when a scene is switched. 0 Answers