Random Groups of Objects

Get help using Construct 2

Post » Mon Oct 01, 2012 6:28 pm

I am making a sidescrolling game where obstacles will be randomly generated. However, I want the series of obstacles to be clean and not just jumbled or impossible to navigate.

My solution to this problem was to create groups of obstacles to randomly generate instead. I imagine I will want to define these groups in an external file and load them in via the ajax object which I am comfortable with. Maybe something like this:   obj1:0,obj7:50,obj3:100,obj3:140

That string would represent a list of objects to spawn, along with a corresponding x offset that would place it behind the spawn x location.

Here is where I'm not sure how to proceed:

I want to be able to create these objects based on the string without needing to use a huge amount of events or having to repeat events for each scenario. What would be the best way to approach this, keeping in mind that I want it to be clean and dynamic.

Thanks :)
B
14
S
5
G
2
Posts: 20
Reputation: 2,378

Post » Mon Oct 01, 2012 8:50 pm

Do a loop over the tokens in the string and do a compare for each object type to pick which to create.

Something like this:
[code]global string objs = "obj1:0,obj7:50,obj3:100,obj3:140"
global string current_obj = ""

repeat tokencount(objs, ",") times
     + set current_obj to tokenat(objs, loopindex, ",")
    system compare tokenat(current_obj, 0, ":") = "obj1")
        +create obj1 at (int(tokenat(current_obj, 1, ":")), 0)
    system compare tokenat(current_obj, 0, ":") = "obj7")
        +create obj7 at (int(tokenat(current_obj, 1, ":")), 0)
    system compare tokenat(current_obj, 0, ":") = "obj3")
        +create obj3 at (int(tokenat(current_obj, 1, ":")), 0)[/code]
B
79
S
24
G
54
Posts: 4,738
Reputation: 40,739

Post » Tue Oct 02, 2012 4:39 pm

very straightforward, I appreciate the tip :)
B
14
S
5
G
2
Posts: 20
Reputation: 2,378


Return to How do I....?

Who is online

Users browsing this forum: Mirlas, TELLES0808 and 22 guests