1
\$\begingroup\$

I'm having a problem wrapping my head around scenario like this.

Say I've got a scene called Room. Now, my game procedurally generates content of each room. Entering a new room to see new content, finishing it and going to the next one isn't much of a problem. However, I'd like to add ability to go back AND to generate a set of rooms (say, 5) along certain path. Now, how exactly can I achieve this?

The latter one, I can store somewhere information about each room whether it should have exits on left/right or top/bottom and just generate as player moves forward using this data. The tricky part is coming back. What would you guys recommend? Serializing the whole scene (will it even work in this case?), picking up and serializing my own information about the randomed scene? Not sure how to approach this.

\$\endgroup\$
  • \$\begingroup\$ If you generate your rooms, you generally start off with a seed for your random generator. You could try and only save that seed for your room, this way you recreate it, and you don't have to save the room data. \$\endgroup\$ – Vaillancourt Oct 6 '15 at 13:03
  • \$\begingroup\$ You're right, however, problems appear when you leave a room collecting something, opening a chest or interacting with certain things. Then I guess I will have to somehow save the whole scene. \$\endgroup\$ – waxx Oct 6 '15 at 13:11
  • 1
    \$\begingroup\$ You can save the delta, i.e. the difference between what's there originally and what has changed. If no data about something exist in you saved data, it means that the user has not interacted with it. \$\endgroup\$ – Vaillancourt Oct 6 '15 at 13:16
1
\$\begingroup\$

First, you need a way to create rooms in a deterministic way. A well-established method to do this is generating the procedural content using a random number generator with a fixed seed. When you initialize the rng with the same seed, it will output the same sequence of values resulting in the same room.

The C# Random class can be instantiated with a 31 bit seed (it is technically an Int32, but negative values are converted to positive ones, so the actual entropy is just 31 bit). That seed should be part the room number and part a "world seed" value which is randomly generated for each playthrough when the player starts a new game but doesn't change afterwards.

Example: Assuming you have a world with no more than 256x256 rooms, you could have the seed for each room be 15 bit world-seed, 8 bit x-coordinate and 8 bit y-coordinate (giving you 32768 unique worlds but each room being unique across all worlds). You would then generate the world seed with the normal rng world_seed = new Random().Next(32768);.

To initialize the rng for generating a room, you could then use:

Random room_rng = new Random(
         world_seed << 16 +
         x << 8 +
         y 
    );

Second, you need some way to save the changes the player made to the room to a file when they leave it and reapply them when they re-enter the unloaded room. This is highly application-specific to your game, so you will have to implement your own file format for this. When you need help about how to serialize the state of some specific feature of your game, feel free to ask a new question explaining the problem in detail.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.