[SOLVED] Remove anything from list function ignores some items

dtishin

  • Posts: 89
Hi, I encountered a weird issue.

TL,DR: The “remove item” function does not remove some items from the list even though the items meet the condition; at the same time (seemingly) identical items that meet the condition are removed. Does anybody have an idea what’s happening here?

In detail:
-   I have a scene of 16 x 9 tiles (144 tiles in total). The tiles in the tileset have data attached to them that describes their terrain type or a combination (e.g. “grass”, “sea” or “grass;hills”). When I create the scene I programmably build a list (game variable) of all tiles in the scene. Each item in the list is a map. The map has keys “column” (number 0-15), “row” (number 0-8), “occupied” (number 0-1), and “data” (list built by parsing the tile’s “data” field).
-   Every turn I need “quest goal” actors to be randomly placed on the map. But I need them to be only placed on suitable tiles, i.e. 1) unoccupied by other objects, and 2) NOT containing data “sea” and 3) NOT containing data “town”.
-   For this purpose I set a local list variable to a copy of the global list of tiles, then cycle through it and remove all items that meet either of the criteria above (containing “sea”, “town” or “occupied=1”).
-   It generally works fine except some “sea” tiles are not removed from the list :(
I attached a picture of the scene (Map_unremoved_tiles.jpg) showing tiles for which the corresponding map items are not removed from the local list. E.g. file “log2” shows that in row 8 (bottom row of tiles in the scene) only three items are removed; the other two are not.
-   The log also shows that the local list does contain the items that need to be removed. E.g. file “log1” shows that both tiles with coordinates [0;8] and [1;8] are in the list and they have identical value of the data field.

The code goes attached for your convenience. It looks big because of all the “print” debugging functions :(

Thank you!

« Last Edit: May 01, 2019, 01:53:09 pm by dtishin »

Luyren

  • *
  • Posts: 2810
If I'm not mistaken, removing items within the loop isn't a good idea. In the middle of the loop, there are less items than when it started, so stuff will be skipped. My suggestion is to, instead of removing, add the tiles instead to another list (Skipped Tiles List, or something). Then, check if that list contains the tile, in order to skip it.
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.

dtishin

  • Posts: 89
Thanks Luyren, that was great advice!
Instead of checking the LOCAL list for all UNSUITABLE tiles I checked the GLOBAL list for all SUITABLE tiles, then copied them to a local list. Like clockwork :)