- Home /
Go back and forth between procedurally generated levels?
I'm creating a procedurally generated maze with multiple levels. I already have the code for generating the maze; that's not the issue.
The issue is I need the player to be able to backtrack to the entrance of the first level of the maze from any level, so all of the levels need to be saved (as scenes, I assume) but I don't want a fixed number of levels, so making a list of scenes in the build settings doesn't seem like the way to go. It seems like this should be simple but, being a noob, I just can't figure out how.
Maybe I'm just searching with the wrong keywords, but so far I'm only finding tutorials for procedural map generation or going back and forth between preexisting scenes listed in the build settings.
Answer by robertbu · Nov 05, 2014 at 03:08 AM
Not much information here about your procedural levels. Typically levels like these are generated from random values. Generate and save a seed for the Random class. Using the same seed should generate the same level. You can use PlayerPrefs to save the seed for each level, or if it is only during a single run of the app, you can use DonDestroyOnLoad:
http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
I used this tutorial for the basis of the maze http://catlikecoding.com/unity/tutorials/maze/ so I'm not sure how that would work, since a seed wasn't involved.
While DontDestroyOnLoad could probably be a key part of the solution, I would still first need to be able to create new scenes via code, and I can't seem to find information on how to do that.
So just before you generate your maze, add this line of code:
Random.seed = 1234;
http://docs.unity3d.com/ScriptReference/Random-seed.html
If you set the seed for Unity's Random class to the same value before calling your maze generation code, you should get the same maze every time. Note I personally find it easier when I do things like this to use the .NET Random class for areas I want to manipulate the seed. It works a bit differently, and since it has the same name, it can be a bit tricky to use both, but you get an instance of a class with .NET Random, so you can isolate the manipulation of the seed value.
I guess I could randomly generate the seed for Random.seed the first time the player enters the level and then use that same seed every other time the player enters the level; since I don't want the player to get the same maze every time they start a new game.
It seems a little more complicated than just saving the scene as-is; I will also have collectibles and a few monsters, and I don't want them all resetting when the player backtracks, but I already have a few ideas for how to handle that.
Thanks.
Answer by Cherno · Nov 05, 2014 at 12:37 PM
"Just" save all the information bout a level in a file (size, block information, objects, player start positions etc.) and when you need to change a level, you first clear the scene and then generate the new level by looking up the informtion from your file. In effect, you have only one Unity scene which gets used for building any level when needed.
And is there a tutorial for that somewhere? It's all well and good to say "you need to do this kind of thing" but I already understood that the two most blatantly obvious possibilities were either creating a new scene every time the player goes to the next level without destroying the previous level, or saving the level data and clearing the scene each time, then loading from that data.
What I need to know is "how," and so far I haven't come across a usable resource for either case. If you know of one, please point me to it.
You should not rely on tutorials for anything other than getting familiar with the topic you are dealing with. Work on understanding what is happening in the tutorials and then implement it yourself for your custom(hopefully unique) situation
Which still requires a tutorial on the relevant topic, duh. I'm looking for a tutorial on one aspect of a larger project I'm working on; just like I used part of a tutorial for the maze, ignored anything I didn't want, and then added my own features to it. I'm also a total newbie to both program$$anonymous$$g and unity, so naturally I'm going to tend to be more reliant on tutorials.
I'm doing my own project from the get-go ins$$anonymous$$d of just copying a tutorial wholesale, because if I'm not working on something I'm interested in I won't have the drive to keep with it. In other words, I'm doing exactly what you just passive-agressively chastised me for not doing; which should really have been totally obvious given that I was only looking for a tutorial relevant to a specific aspect of the game.
Seriously, if you don't have anything useful to add, why even comment? Does it feed your ego or something?