1
\$\begingroup\$

I'm making a 2D Tower Defense and currently looking into making a save feature. The problem is that my game is procedural generated (based on the rogue-like tutorial posted by Unity): 2d generation

At runtime I generate a random amount (approx 7k) tiles of treasures, obstacles and different enemies at random locations. Some tiles will also be destructible and not exist when destroyed.

What should I do to save the position of each tile and values such as "Level" and "Gold"?

This is a list of what I need to be saved:

  • Player value (position, gold, level, health, inventory)
  • Building values (Tower/Building positions, health value etc)
  • Wave values (wave_level)
  • Board values (all tile positions such as obstacles, treasures etc)

NOTE: Every tile is a GameObject.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Do you need help with how to create a savegame in general or are you only interested in the aspects which are relevant to procedural generated content? \$\endgroup\$
    – Philipp
    Commented Feb 22, 2016 at 22:15
  • \$\begingroup\$ Well both really, but mostly how to save the position of each tile and building gameObjects that are being instansiated at runtime and then apply it to the scene if the player is loading a save. \$\endgroup\$ Commented Feb 22, 2016 at 22:28
  • \$\begingroup\$ You can just save all your objects and stuff. If you're worried about the file size, you can always compress the save or something. \$\endgroup\$
    – ashes999
    Commented Feb 22, 2016 at 22:53
  • \$\begingroup\$ I think position of everything is the main concern here, everything else you can save on objects itself. What I did for this is simply save a long string of numbers representing everything. For example you have 3 types of tiles, a player and a building, total 5 elements. Make a rule 0=player,1=building, so on. If you have a grid of 10x10 then it would be total 100 characters string containing only numbers. You can easily use it in nested way \$\endgroup\$ Commented Feb 23, 2016 at 5:47
  • \$\begingroup\$ Either serialize your objects and save to file -- then deserialize them when you load them up.... Or write your own serialization/deserialization methods, and do it manually. As it stands now, this question is a duplicate. \$\endgroup\$
    – jgallant
    Commented Feb 23, 2016 at 11:50

1 Answer 1

-1
\$\begingroup\$

Random numbers in computing, in most cases, aren't actually that random, they are technically pseudo-random. What this means is that the random number generator starts with what is called a "seed". The seed determines the random numbers that are generated with each call to random, and given the same seed, you'll get the same random numbers for each call to random(), assuming you call it the same number of times after the seed.

If there are no other things that would make your application run differently (other than the random numbers), then by saving the seed used to initialize the random number generator, you'll get the same random numbers.

Normally, the random number generator will be seeded by something that is different for each user by default, something like the number of milliseconds since the device was booted. Instead, you can make sure that before you start generating random numbers you set the seed to something (and it can be something like the number of milliseconds since the device booted).

You can save that seed, and then re-generate all the same random numbers, which means you don't need to save them.

Another alternative would be to wrap the random call with something of your own that calls to random, and in that wrapped function, you can save the random number that was generated and play them back.

To see a bit more about how this works, search for srand(), which is the C function to set the seed for the random number generator. That documentation may help put you down the right path.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ If you save the seed, change your algorithms to get another random number somewhere in the middle, and then load that save game and re-generate, you'll get a totally different set than what you saved. \$\endgroup\$
    – ashes999
    Commented Feb 22, 2016 at 22:52
  • 1
    \$\begingroup\$ I see how this could be done with the initial world creation but what about when an obstacle is destroyed or when a player places a tower? How can I save that information together with the seed? \$\endgroup\$ Commented Feb 22, 2016 at 23:05
  • 1
    \$\begingroup\$ @FransBergström Besides with seed, you actually need to find an efficient way to save every change to your world too - which minecraft does with so named "chunks" system. Then after creating world again from seed, you need to reapply every change to world again. \$\endgroup\$ Commented Feb 22, 2016 at 23:13
  • \$\begingroup\$ @SamedTarıkÇETİN Do you know any good resources/guides to look for regarding this? \$\endgroup\$ Commented Feb 22, 2016 at 23:29
  • \$\begingroup\$ @FransBergström Unfortunately no, this logic is a general approach. But i can give you a tip, tag every single GameObject that can change in-game with a unique identity (preferably an int). Then create a Dictionary<uniqueIdentity, GameObject> (or whatever). So you can simply save and load everthing just by referencing that unique identity value, which saves you from weird logic of serializing GameObjects. \$\endgroup\$ Commented Feb 23, 2016 at 0:08

Not the answer you're looking for? Browse other questions tagged .