For Each problem

Get help using Construct 2

Post » Fri Jan 18, 2013 6:12 pm

I thought I understood how For Each worked, but apparently not and I've been stuck on this one all day long.

Hard to explain so I'm attaching an image. This is the exact code as it runs in my program (not the original code, but in my frustration I've had to hack it down to this, to try to work out what's going on).



The text boxes show two different values. There is nothing else in the code, this is exactly how it's set in my program right now. The top text box (outside the for/each loop) shows the correct value that's stored in the tile variable. The one inside the loop shows a different value.

I don't understand what's happening here at all. My understanding of how this SHOULD be working is, it runs through the loop and when the tile with those X/Y/Z coordinates is picked, it will then show that tile's value in the text box, which should of course be exactly the same as the one above.

Presumably I'm doing something wrong, and that 2nd pick isn't picking the same tile?

To add to this, this isn't the original code. In the original code, it doesn't do a 2nd pick of 'ShipTiles' at all, it's attempting to push all of the values in the tiles into an array. So the code inside the loop in the original (which also doesn't work) just says ('Put ShipTiles.Condition into Array.At(X,Y,Z)). It puts the wrong values in. So in that case, it's not doing a 2nd pick of the Shiptiles object, which may be partly what's wrong above.farflamex2013-01-18 18:15:30
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Fri Jan 18, 2013 6:55 pm

It looks like it may be a bug. Logically it should be working. However it would be more efficient to have the variable conditions first then the for each condition.

For more info on for each, check out the for each tutorial.
Moderator
B
87
S
32
G
33
Posts: 3,005
Reputation: 27,397

Post » Fri Jan 18, 2013 7:00 pm

Thanks Arima. I did read your tutorial through several times to no avail. I also understand the efficiency of putting the conditions first to cut down the size of the loop.

After 6 hours, I've worked out what's happening. There are two tiles at that location (an oversight elsewhere so some tiles had the same location), so it's actually picking different tiles. Sigh. At least it's fixed now, and it wasn't a C2 problem or my understanding of the logic.... as with all bugs, it's obvious now that there were 2 tiles there :(
B
25
S
5
G
3
Posts: 446
Reputation: 5,633


Return to How do I....?

Who is online

Users browsing this forum: 99Instances2Go, Braus, supreme676 and 7 guests