surface.get_tile function returns wrong result.

Bugs that are actually features.
Post Reply
yagaodirac
Fast Inserter
Fast Inserter
Posts: 152
Joined: Sun Jun 16, 2019 4:04 pm
Contact:

surface.get_tile function returns wrong result.

Post by yagaodirac »

In on_chunk_generated, I turned all the tiles out side (-200,-200),(200,200) into "water".

Code: Select all

script.on_event(defines.events.on_chunk_generated, function(event)
  --Some trivial code removed.
  
   local tiles = {}
   local index = 1
   for x=event.area.left_top.x,event.area.right_bottom.x-1 
   do
      for y=event.area.left_top.y,event.area.right_bottom.y-1
      do
         if math.abs(x)>global.distance_control.land_half_size or math.abs(y)>global.distance_control.land_half_size
         then
            tiles[index] = {name = "water", position = {x, y}}
            index = index + 1
         end
      end
   end
   event.surface.set_tiles(tiles)

     --Some trivial code removed.

end)
Then, I tried to test if a tile is land or water.

Code: Select all

script.on_event(defines.events.on_tick, function(event)
     --Some trivial code removed.

	local test_tile = game.surfaces[1].get_tile(some coord out side the 200 square)
	game.print(tostring(test_tile.valid))
	game.print(test_tile.name)
	-- Sometimes the result is some land.
	
     --Some trivial code removed.
end)
Any idea? Idk if this is a bug. By the way, the get_tile function is very slow. Would you plz add a get_tile_is_water or get_tile_is_land.

sp55aa
Long Handed Inserter
Long Handed Inserter
Posts: 55
Joined: Mon Oct 07, 2019 9:27 am
Contact:

Re: surface.get_tile function returns wrong result.

Post by sp55aa »

How to report a bug - READ BEFORE POSTING viewtopic.php?f=7&t=3638

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

Re: surface.get_tile function returns wrong result.

Post by Rseding91 »

Most likely: the chunk you set_tiles on then generated normally and overwrote what you did. You'd need to also set the chunk generated status to prevent the game from overwriting them.
If you want to get ahold of me I'm almost always on Discord.

yagaodirac
Fast Inserter
Fast Inserter
Posts: 152
Joined: Sun Jun 16, 2019 4:04 pm
Contact:

Re: surface.get_tile function returns wrong result.

Post by yagaodirac »

Rseding91 wrote:
Wed May 26, 2021 4:09 pm
Fitst of all, Thank you for helping.
But I really need some more help.
I added

Code: Select all

global.surface.set_chunk_generated_status(event.position, defines.chunk_generated_status.custom_tiles)
global.surface.set_chunk_generated_status(event.position, defines.chunk_generated_status.basic_tiles)
global.surface.set_chunk_generated_status(event.position, defines.chunk_generated_status.corrected_tiles)
global.surface.set_chunk_generated_status(event.position, defines.chunk_generated_status.tiles)
global.surface.set_chunk_generated_status(event.position, defines.chunk_generated_status.entities)
after the set_tiles function. But it doesn't work. The get_tile function doesn't guarantee the result even the game tick is > 1000.
Do I have to teleport players[1] around to force the game engine to create these chunks?
Or does it work if I create radar when a chunk is generated, set energy for it to let it work for 1 tick and then remove it?
I tried

Code: Select all

global.force.chart(global.surface, {{x = -800, y = -800}, {x = 800, y = 800}})
It doesn't work.

Finally it turns out to be a disaster. You should add a field to let programmers know if this chunk which contains this tile has raise the on_chunk_generated event.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1703
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: surface.get_tile function returns wrong result.

Post by Pi-C »

yagaodirac wrote:
Thu May 27, 2021 1:17 am
Rseding91 wrote:
Wed May 26, 2021 4:09 pm

Code: Select all

global.surface.set_chunk_generated_status(event.position, defines.chunk_generated_status.custom_tiles)
You must convert event.position from tiles to chunks. Just divide both x and y by 32.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

yagaodirac
Fast Inserter
Fast Inserter
Posts: 152
Joined: Sun Jun 16, 2019 4:04 pm
Contact:

Re: surface.get_tile function returns wrong result.

Post by yagaodirac »

Pi-C wrote:
Thu May 27, 2021 5:54 am
I'm not 100% sure, but this event.position is already divided by 32. In other words, it's chunk position, it's not tile position.
On chunk generated passes 2 position to the call back function. The other one is an area, with coords not divided by 32.

yagaodirac
Fast Inserter
Fast Inserter
Posts: 152
Joined: Sun Jun 16, 2019 4:04 pm
Contact:

Re: surface.get_tile function returns wrong result.

Post by yagaodirac »

Rseding91 wrote:
Wed May 26, 2021 4:09 pm
I'm very sorry to tell you that your suggestion doesn't help. If it's supposed to work and you are very sure, plz provide more detail or experience, or code directly.
My solution is a container to store the generated chunks. It works properly. But I guess you should provide the api in your cpp code.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1703
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: surface.get_tile function returns wrong result.

Post by Pi-C »

yagaodirac wrote:
Thu May 27, 2021 10:20 am
Pi-C wrote:
Thu May 27, 2021 5:54 am
I'm not 100% sure, but this event.position is already divided by 32. In other words, it's chunk position, it's not tile position.
On chunk generated passes 2 position to the call back function. The other one is an area, with coords not divided by 32.
Seems you're right. :-)
yagaodirac wrote:
Thu May 27, 2021 1:17 am
I added

Code: Select all

global.surface.set_chunk_generated_status(event.position, defines.chunk_generated_status.custom_tiles)
global.surface.set_chunk_generated_status(event.position, defines.chunk_generated_status.basic_tiles)
global.surface.set_chunk_generated_status(event.position, defines.chunk_generated_status.corrected_tiles)
global.surface.set_chunk_generated_status(event.position, defines.chunk_generated_status.tiles)
global.surface.set_chunk_generated_status(event.position, defines.chunk_generated_status.entities)
Does that really make sense?

Code: Select all

set_chunk_generated_status(position, status)

Set generated status of a chunk. Useful when copying chunks.

Parameters
position :: ChunkPosition: The chunk's position.
status :: defines.chunk_generated_status: The chunk's new status.
In my opinion, this means that any chunk can have only one value from defines.chunk_generated_status, so you'd overwrite everything again and again, and only defines.chunk_generated_status.entities would persist. Of course, it could be that this is inclusive (so basic_tiles would be custom_tiles + basic_tiles, and entities would include all of the above) … That's just guesswork, though, I didn't play around with it.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

yagaodirac
Fast Inserter
Fast Inserter
Posts: 152
Joined: Sun Jun 16, 2019 4:04 pm
Contact:

Re: surface.get_tile function returns wrong result.

Post by yagaodirac »

Pi-C wrote:
Thu May 27, 2021 3:13 pm
I have no idea how the set chunk ... status works. No idea if the code works. No idea, and I don't want to guess any more.
The trouble is already solved. I made a container in lua, and looked up from it. You could see it in my scenario.

Post Reply

Return to “Not a bug”