Page 1 of 1
surface.get_tile function returns wrong result.
Posted: Wed May 26, 2021 8:06 am
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.
Re: surface.get_tile function returns wrong result.
Posted: Wed May 26, 2021 8:45 am
by sp55aa
How to report a bug - READ BEFORE POSTING
viewtopic.php?f=7&t=3638
Re: surface.get_tile function returns wrong result.
Posted: Wed May 26, 2021 4:09 pm
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.
Re: surface.get_tile function returns wrong result.
Posted: Thu May 27, 2021 1:17 am
by yagaodirac
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.
Re: surface.get_tile function returns wrong result.
Posted: Thu May 27, 2021 5:54 am
by Pi-C
yagaodirac wrote: ↑Thu May 27, 2021 1:17 am
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.
Re: surface.get_tile function returns wrong result.
Posted: Thu May 27, 2021 10:20 am
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.
Re: surface.get_tile function returns wrong result.
Posted: Thu May 27, 2021 10:23 am
by yagaodirac
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.
Re: surface.get_tile function returns wrong result.
Posted: Thu May 27, 2021 3:13 pm
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.
Re: surface.get_tile function returns wrong result.
Posted: Mon May 31, 2021 7:53 am
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.