I am trying to make a platform game that generates semi-random levels. I want it to read in the segments of a level and then choose a random one from among these segments. I am stuck. I have absolutely no clue how to set up a system like this and I can't find any good tutorials.

  • 2
    \$\begingroup\$ Normally you should give more information about what you're trying to accomplish, how you're currently working with your data, and any issues you've run into in trying to implement this yourself. \$\endgroup\$ – user1118321 Nov 23 '16 at 21:00
  • \$\begingroup\$ It sounds like you already have a good handle. You need 1) A collection of authored level segments 2) A data structure to store & look up those level segments 3) A piece of logic to detect when a new segment is needed (do you generate it all on level load, or stream it in as the player progresses through the level?) 4) A piece of logic to choose a segment 5) A loading system to spawn the chosen segment into the current level 6) (Maybe) a system to unload a level segment you don't need anymore. Which of these parts do you need help with? Bear in mind we don't answer "how do I get started" here. \$\endgroup\$ – DMGregory Nov 23 '16 at 21:34

The answer to this is highly dependent on what you want out of your semi-random levels, but I'll assume for now that you are moving horizontally and want chunks of level such as a pit with a platform over it or a constrained space with enemies in it that you set up beforehand.

To take this approach you need to have the ability to load one of these level chunks on the fly. This means that you need to be able to have everything that describes one (tile layout if tile based, types of objects and locations/orientations for everything else) in an object that you can reference such as a class. This could be as simple as an object containing an array of what to spawn and where, but you can also add other things as necessary.

Once you have the ability to have these chunks in memory, you then need to set up your game to be able to take one of these chunks and a location so you can spawn all the objects relative to that location. This location could be the top left corner, the place where the edge of the ground touches the new edge of the ground, whatever. So long as the level chunk object locations are also described as positions relative to this anchor.

From here you have a couple options. Assuming you create a number of level chunks, you can then choose from them randomly to set up your random level.

If you know you want the level to be a fixed set and/or finite, at this point you can simply load x random segments at the appropriate positions (perhaps with one or two fixed to things you want at certain locations like a level end or a checkpoint).

If you want to generate your levels on the fly and/or have an infinite level, load enough level chunks to cover the camera plus at least one off each edge the player can move in. These are your buffer so the player never sees empty space (also so enemies off screen can do things if that's how you like it). When one of those buffer chunks would come into the camera, load a new random chunk out of camera as the new buffer. Depending on your gameplay you can also remove the level chunk you moved away from to avoid having too much in memory or if you know you're not going back that way.

There are of course a lot of modifications that can be made to this general formula as well as refinements for various behaviour; but hopefully this will give you a good jumping off point for exploring the world of level generation.


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.