Create Specific Object X Times

In this topic, we handle scenarios in which you want some objects to be instantiated a specific number of times. This is useful when…

  • You want to define more precisely how often loot chests, enemies, or points of interest should appear
  • You want the Player to look for a key or switch before he can escape through the exit
  1. Let’s add some loot chests on the Decoration Map and assign them to, e.g., Region Id 11. I drew 4 loot chests, whereas 3 are small ones, and 1 is a more epic one. This way, we get a 3:1 ratio.

  2. Place some Spawn Locations on the snippets (again with Region Id 11).
  3. Open the Script, which calls the Generator.
  4. We will modify the line drawDecorations(). Add a list of all the region ids of all ordinary decorations as a function parameter. In other words: All decorations except for the loot chests. This could be the regions 1, 2, and 3; then we use this call:

    .drawDecorations([1, 2, 3])
  5. Just below this line, we add this function call:

    .drawDecorationXTimes(11, 1, 4)

    The numbers 1 and 4 define the range of how often an item will be drawn. At least 1 and not more than 4 loot chests will appear – if possible. If there’s no free location to spawn available anymore, the generator skips this command, and nothing happens.

The parameters 2 and 3, which stand for the minimum and maximum number, are optional. If you leave out the maximum number, then the Generator will draw exactly X times, and if you leave out both, the Generator will draw exactly once.

Now you will have something in between one and four loot chests on every map (unless you put too less locations to spawn). If you have any troubles, look into the Demon Castle of the Demo Project.