For Each problem

0 favourites
  • 3 posts
  • 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).

    <img src="" border="0" />

    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.

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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 :(

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)