# Endless Runner - segment generation

**URL:** <https://forum.defold.com/t/endless-runner-segment-generation/801>\
**Category:** Questions\
**Created:** [January 19, 2016, 9:43am UTC](https://forum.defold.com/t/endless-runner-segment-generation/801 "2016-01-19T09:43:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vigridzki](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/vigridzki/32/352_2.png) [@vigridzki](https://forum.defold.com/u/vigridzki)\
**Post date:** [January 19, 2016, 9:43am UTC](https://forum.defold.com/t/endless-runner-segment-generation/801/1 "2016-01-19T09:43:14Z")

</div>

Hi,

I’m making an endless runner game and I’m wondering about approach to generating levels. I would like to create segments and stitch them together. A segment would be a finite section of a level with its own obstacles and spawners for enemies and collectibles. Segments would need to be added at runtime at random from a pre-defined selection. I would like to offload segment creation work to another person, who is going to focus on level design, so ideally no programming should be necessary to create another segment.

What is the correct approach to take in Defold? In my understanding, the segments need to be Defold collections, and these collections need to be instantiated by a collection factory at runtime. Since one factory can spawn only one type of collection, there would need to be multiple factories, one for each segment type, and I could parent them in a single “factory controller” game object, that would tell an appropriate factory to generate a segment.

Is this a correct approach or is there a better one?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [January 19, 2016, 1:15pm UTC](https://forum.defold.com/t/endless-runner-segment-generation/801/2 "2016-01-19T13:15:55Z")

</div>

Yes, that sounds about right I think. How many different segments would you have?

---

<div class="post-metadata">

**Author:** ![vigridzki](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/vigridzki/32/352_2.png) [@vigridzki](https://forum.defold.com/u/vigridzki)\
**Post date:** [January 19, 2016, 1:19pm UTC](https://forum.defold.com/t/endless-runner-segment-generation/801/3 "2016-01-19T13:19:09Z")

</div>

I think we’ll end up using about 15-20 different segments, for variety. They will reuse graphic elements for the most part, but will have different layouts.

I’ve noticed there is no way to get a list child objects from an object, so I figure that when I ask the designer to extend the factory controller object with new factories, I will have to register these factories in the scripts. Is there any built-in mechanism in Defold that would allow my designer to extend the selection of objects I’d create, without him having to touch any of the code?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [January 19, 2016, 3:30pm UTC](https://forum.defold.com/t/endless-runner-segment-generation/801/4 "2016-01-19T15:30:37Z")

</div>

15-20 collections shouldn’t be a problem.

You are correct that there is no way to get the child objects. You could in your main collection let the segment designer create one game object per collection and in each game object attach the collection factory plus a script. The purpose of the script would be in the init function to register the contained factory with the main collection by doing a msg.post():

```
msg.post("game:/game", "register_segment", { url = msg.url("#collectionfactory") })

```

Maybe this picture better illustrates what I mean:

 ![](https://forum-defold.b-cdn.net/uploads/default/original/1X/98a24b0151671ad01f1229a281bcf24e844b3d54.png)

---

<div class="post-metadata">

**Author:** ![vigridzki](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/vigridzki/32/352_2.png) [@vigridzki](https://forum.defold.com/u/vigridzki)\
**Post date:** [January 19, 2016, 3:35pm UTC](https://forum.defold.com/t/endless-runner-segment-generation/801/5 "2016-01-19T15:35:11Z")

</div>

@britzl Thank you very much, I think this will work exactly the way I’d like. I will try this and let you know! 🙂

---

<div class="post-metadata">

**Author:** ![Johan](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/johan/32/127_2.png) [@Johan](https://forum.defold.com/u/Johan)\
**Post date:** [January 19, 2016, 3:41pm UTC](https://forum.defold.com/t/endless-runner-segment-generation/801/6 "2016-01-19T15:41:57Z")

</div>

Just want to chime in to say this is exactly how we handled level creation in our game “Hammerwatch Coliseum”. The levels are made up of random “pieces” that was created by level designers without coding skills. They then added these to a collection with a unified naming with incremental numbers (“room\_1”, “room\_2” etc) and we have a script with two properties, min\_room and max\_room which they changed as they added new rooms (and it also allowed us to set min\_room and max\_room to the same number to force a certain map-segment to be created during debugging and development).
