playMaker

Author Topic: Nested For Loop is drawing strangely[SOLVED]  (Read 2098 times)

Ljacobs

  • Playmaker Newbie
  • *
  • Posts: 10
Nested For Loop is drawing strangely[SOLVED]
« on: August 12, 2013, 04:54:08 AM »
TL;DR,

I wasn't resetting my X correctly and incrementing my Z in the right order.


Hey all,

Using Map and Nav, I'm created a 30x30 grid of nodes, each with a prefab child, the properties and material of which I can change at runtime to make a Procedurally generated map, albeit a small one.

I've got a for loop for current rooms < max rooms.

In each iteration, the basic loops is as follows.

Z<=Zi (Z being the starting Z,and Zi being the height of the room.)
Under that another loop

X<=X1 (same relationship but for width.

I randomize the values a little, but each room starts with X and Z at the same value.

With Z and X at 0, I run them through another for loop which iterates through an array of nodes(yeah array maker and find all by tag), and checking to see if both the Z and X values from my loops, match the Z and X position of the node at the current index.

If so, I tell the node to get it's child and change it's material from wall to floor.

I then increment X and return to the step in the for loop where X<=X1(Width) and continue to compare my Z(so far unchanged) and X values. When I find a tile that matches those values, I repeat the process.

Once X has reach X!, I know I've drawn the entire width of the room, so I increment Z, which if it is still <= Z1(height) I start to iterate through the X values again, comparing the Z and X values to each index of an array of nodes and changing tiles appropriately.

The problem I am runnig into, is that for each room, the Z value is drawn once, all the X values are iterated through and drawn, but when I increment Z, It jumps directly to the increment number of rooms step and I end up with 5 X wide lines at 5 different Z heights.

However, with the four variables I have Z, Z height, X and X Width, I should be able to draw a rectangle, no?

For Z<= Z1
     For X<= X1

Run Z and X values through a for loop looking for a match.
If match, alter prefab tile child of node.
Increment X
Loop Back to For X<=X1
When X=X1
Increment Z
Z For Loop
X for Loop
Continue to loop through all nodes looking for a match and changing that tile at those coords.

When Z > Z1
Increment Number of Rooms
For Number of rooms <= Max Rooms
Randomize room dimensions
back to Z for Loop.

I'm not clear why this doesn't work.

Any thoughts?

Thanks,
-Luke


« Last Edit: August 12, 2013, 10:17:42 PM by Ljacobs »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15620
  • Official Playmaker Support
Re: Nested For Loop is drawing strangely[SOLVED]
« Reply #1 on: August 14, 2013, 05:15:07 AM »
Hi,

 to debug this kind of issues. start with a 4 by 4 grid and have very clear and round values. debug it slowly ( like implementing a delay between each loop iteration), and study the values and placement you get and their values. If numbers are simple, you will see what's wrong in your math and find the source of the problem quicker.

bye,

Jean