[Solved]Can not delete unused chunks

Place to get help with not working mods / modding interface.
Post Reply
User avatar
WIZ4
Fast Inserter
Fast Inserter
Posts: 209
Joined: Thu Apr 07, 2016 1:36 pm
Contact:

[Solved]Can not delete unused chunks

Post 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
Last edited by WIZ4 on Sat Aug 11, 2018 10:37 am, edited 1 time in total.
My native language is russian. Sorry if my messages are difficult to read.

Bilka
Factorio Staff
Factorio Staff
Posts: 3129
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Can not delete unused chunks

Post 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
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

User avatar
WIZ4
Fast Inserter
Fast Inserter
Posts: 209
Joined: Thu Apr 07, 2016 1:36 pm
Contact:

Re: Can not delete unused chunks

Post by WIZ4 »

Thanks for the help, Bilka!
My native language is russian. Sorry if my messages are difficult to read.


Post Reply

Return to “Modding help”