Page 1 of 1

[Solved]Can not delete unused chunks

Posted: Sat Aug 11, 2018 10:15 am
by WIZ4
Hi, i wrote this code, which should delete all the chunks except those on which there are player builds.But instead, he removes the chunks on the contrary with the players buildings. Why?

Code: Select all

function delete_chunks()
local surface = game.surfaces.nauvis
for chunk in surface.get_chunks() do
    local entities = surface.find_entities_filtered{area={{chunk.x*32, chunk.y*32}, {(chunk.x)*32+32, (chunk.y)*32+32}},force = "player"}
    for _, entity in pairs(entities) do
        if  #entity == 0 then 
		surface.delete_chunk({chunk.x,chunk.y})
		end	
    end
end
end

Re: Can not delete unused chunks

Posted: Sat Aug 11, 2018 10:25 am
by Bilka
You are checking for the length of an entity, not how many entities there are on the chunk. Here is a simpler and working version:

Code: Select all

function delete_chunks()
  local surface = game.surfaces.nauvis
  for chunk in surface.get_chunks() do
    local entity_count = surface.count_entities_filtered{area={{chunk.x*32, chunk.y*32}, {(chunk.x)*32+32, (chunk.y)*32+32}},force = "player"}
    if entity_count == 0 then
      surface.delete_chunk({chunk.x,chunk.y})
    end
  end
end

Re: Can not delete unused chunks

Posted: Sat Aug 11, 2018 10:33 am
by WIZ4
Thanks for the help, Bilka!

Re: [Solved]Can not delete unused chunks

Posted: Sat Aug 11, 2018 11:04 am
by darkfrei
You can also use this mods:
DestroyEmptyChunks
DeleteEmptyChunks