[SOLVED] Nested For loop not iterating properly

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
kcilds
Prole
Posts: 4
Joined: Tue Feb 10, 2015 6:40 pm

[SOLVED] Nested For loop not iterating properly

Post by kcilds »

SORRYYYYY haha
just found out that the rooms were getting sliced in a way that made the top-left and bottom-right points (that i use to draw the square) go crazy. I was getting a room with leftX 35 and rightX 30 so it was reversed and the loop couldnt iterate.

I was tired so my fault here! (Will leave this here for anyone learning that stumbles on the same problem)

Original Post:
Hey yall
I'm working on a little rogue-like project and today i was creating a bsp tree to make a templeesque zone in the dungeon layout.
The problem is that when I used a nested for loop to dig the area of each room, leaving only the surrounding walls, I got into a problem.

I get only the First room digged and the rest are ignored.

The loop is going like this:

Code: Select all

-- stuff slicing the zone into rooms and storing them in a table

for i = 1, #rooms do
    print("Im going through "..i)
    print("Width", math.abs(rooms[i].lx - rooms[i].rx))
    print("Height", math.abs(rooms[i].ly - rooms[i].ry))
    
    for roomY = rooms[i].ly, rooms[i].ry - 1 do
      print("in Y for loop", roomY)
      
      for roomX = rooms[i].lx, rooms[i].rx - 1 do
        Map[roomX][roomY].tileType = "floor"
        print("in X for loop", roomX)
        
      end
    end
end

So I ran into lots of trouble on other dugeon gen codes on my lil journey, but this one is making me mad. I use print() to help debugging and what I got this time is that the loop iterate for the first room properly and then its goes flop. After the first sequence loop It gets only to the Y loop and gets nothing on X one.
The table is Full of rooms, the variables inside the table are acessible in loop (i can print the rooms.lx, etc) and there is no 0 or null value.
This is what I get from the print():

Code: Select all

Zone Points:    1,14    26,29
Zone Type:      temple
Rooms Quant     7
Im going through 1
Width   4
Height  4
in Y for loop   14
in X for loop   1
in X for loop   2
in X for loop   3
in X for loop   4
in Y for loop   15
in X for loop   1
in X for loop   2
in X for loop   3
in X for loop   4
in Y for loop   16
in X for loop   1
in X for loop   2
in X for loop   3
in X for loop   4
in Y for loop   17
in X for loop   1
in X for loop   2
in X for loop   3
in X for loop   4
Im going through 2
Width   5
Height  1
Im going through 3
Width   5
Height  1
Im going through 4
Width   1
Height  5
in Y for loop   24
in Y for loop   25
in Y for loop   26
in Y for loop   27
in Y for loop   28
Im going through 5
Width   4
Height  1
Im going through 6
Width   1
Height  4
in Y for loop   19
in Y for loop   20
in Y for loop   21
in Y for loop   22
Im going through 7
Width   3
Height  1
So why for the science sake this **** is not working?
(I'm more or less a noob so i feel It can be lack of important knowledge or im too tired to see something thats obvious in this code...)

Please help
Thanks in advance
Last edited by kcilds on Sat May 02, 2020 2:39 pm, edited 2 times in total.
tobiasvl
Prole
Posts: 29
Joined: Mon Oct 01, 2018 4:58 pm
Location: Norway
Contact:

Re: Nested For loop not iterating properly

Post by tobiasvl »

The code looks good. What are the values of each room's ly, ry, lx and rx?
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Nested For loop not iterating properly

Post by pgimeno »

The symptoms sound like you've run into the gotcha of references vs copies:

Code: Select all

local a = {x=1}
local b = a
b.x = 2
print(a.x) -- prints 2, not 1!
Without more code, however, it's not possible to know for sure.
User avatar
kcilds
Prole
Posts: 4
Joined: Tue Feb 10, 2015 6:40 pm

Re: Nested For loop not iterating properly

Post by kcilds »

pgimeno wrote: Sat May 02, 2020 11:20 am The symptoms sound like you've run into the gotcha of references vs copies:
Don't think so cause i'm not using references. I'm directly getting from the rooms table the X/Y values and assigning them to the map coordinate (and the first iteration works I get only the first room digged).

I first tought that it could be other piece of code overwriting the actions of this but when you look at the returning info from the print function you see that it don't go all the way inside the loop after the first iteration.

probably there is something about for loops that I don't know...

thanks a lot for the feedback ;)

(Updated the post with the current code and what the print function returns)
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests