[0.16.51] Surface.set_tiles Glitch During on_init - Sometimes doesn't set specified tiles

Bugs that are actually features.
Post Reply
timbowit
Burner Inserter
Burner Inserter
Posts: 5
Joined: Wed May 17, 2017 2:37 am

[0.16.51] Surface.set_tiles Glitch During on_init - Sometimes doesn't set specified tiles

Post by timbowit » Fri Feb 08, 2019 12:14 am

What I did
I'm creating a mod which calls LuaSurface.set_tiles in the script.on_init event handler. Some of the tiles I've instructed it to set to concrete are not set to concrete, and vice versa. I've only noticed the glitches at the outermost initially generated chunks, i.e. slightly further out than the initially charted chunks.

In the real mod I use surface.get_chunks, but for the purposes of a minimal reproduction of the problem, hard coding 9 chunks radius produces the same result. The simplified mod (see included control.lua script) to confirm and demonstrate this behavior sets the entire surface to concrete, in the on_init handler.

control.lua:

Code: Select all

script.on_init(function()
	for x = -9*32, 9*32 - 1 do
		for y = -9*32, 9*32 - 1 do
			game.surfaces[1].set_tiles({{name = 'concrete', position = {x, y}}}, false)
		end
	end
end
)
What did I expect
The edges of the concreted area should be smooth, with every tile in the initially generated chunks set to concrete, and every tile beyond that not concreted.
What happened
The edges of the concreted area are "jagged". (See screenshot for map seed "1234". Other seeds also result in jagged edges at different positions around the concreted area.) Some of the tiles that should be concrete are not, and some of the tiles that should not be concrete are. The exact pattern of set and unset tiles is reproducible for a given map seed, using either Freeplay or Sandbox mode.

I didn't see any error messages.

(Attachment: Screenshot of jagged edge of generated concrete, for map seed "1234". Tile grid shown for ease of reference.)
Set_Tiles-Glitch.png
Set_Tiles-Glitch.png (3.78 MiB) Viewed 69 times
Variations tried
Passing an array of tiles rather than a single tile at a time to the set_tiles function had the same unexpected jagged result.

Omitting the second parameter (correct_tiles) rather than setting it to false had the same unexpected jagged result.

Setting the tiles in a console script instead of in a mod's on_init handler did not produce any of the unexpected glitches.

Setting +/- 8 chunks in the mod produced some glitches, but much fewer.
Steps to reproduce
Create a folder in the Factorio mods directory with the specified control.lua and a standard info.json file. Have no other mods enabled. Start Factorio and play a new game or new Sandbox scenario. The attached screenshot is from a default world configuration, with a seed of "1234". Other seeds also have glitches, but not in the exact same tiles. Move to the edge of the concreted area and examine the perimeter.
Attachments
factorio-current.log
(3.9 KiB) Not downloaded yet

Rseding91
Factorio Staff
Factorio Staff
Posts: 8195
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [0.16.51] Surface.set_tiles Glitch During on_init - Sometimes doesn't set specified tiles

Post by Rseding91 » Fri Feb 08, 2019 12:23 am

Thanks for the report however this is working correctly. If you set-tiles on a chunk that hasn't had the bordering chunks > status 'generated tiles" then when that border chunk generates it can modify the edge tiles as it finalizes it's tile generation.

That's why you're supposed to listen to the chunk-generated event and only mutate tiles inside that chunk. If you mutate tiles outside generated chunks you get this result as the chunk generation overwrites portions of the neighbor chunks.
If you want to get ahold of me I'm almost always on Discord.

timbowit
Burner Inserter
Burner Inserter
Posts: 5
Joined: Wed May 17, 2017 2:37 am

Re: [0.16.51] Surface.set_tiles Glitch During on_init - Sometimes doesn't set specified tiles

Post by timbowit » Fri Feb 08, 2019 1:08 am

Thanks, I'm glad there's a reasonable explanation. For newly created maps, using on_chunk_generated is a good solution. (I checked, and the event is generated even for the initial chunks.)

When the mod is added to an existing map, the mod will not get chunk_generated notifications for existing chunks. So using an init handler is still desirable for this scenario. I can't see a way for a mod to check a chunk's "generated tiles" status. So I would like to differentiate between an on_init event for a new game versus for an existing game. It occurs to me that I could check the game.tick value during the on_init event, as I see that it always appears to be zero (0) during the init event handler in a new game, and non-zero otherwise. I'll proceed with that approach.

Thanks again for the quick response.

Rseding91
Factorio Staff
Factorio Staff
Posts: 8195
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [0.16.51] Surface.set_tiles Glitch During on_init - Sometimes doesn't set specified tiles

Post by Rseding91 » Fri Feb 08, 2019 1:19 am

If you want to get ahold of me I'm almost always on Discord.

timbowit
Burner Inserter
Burner Inserter
Posts: 5
Joined: Wed May 17, 2017 2:37 am

Re: [0.16.51] Surface.set_tiles Glitch During on_init - Sometimes doesn't set specified tiles

Post by timbowit » Fri Feb 08, 2019 1:55 am

That's much cleaner. My original confusion arose from not understanding that surface.get_chunks can return some chunks where is_chunk_generated is false.

Post Reply

Return to “Not a bug”

Who is online

Users browsing this forum: No registered users and 2 guests