entity.destroy not working.

Place to get help with not working mods / modding interface.
memcallen
Inserter
Inserter
Posts: 34
Joined: Sat Mar 28, 2015 1:22 am
Contact:

entity.destroy not working.

Post by memcallen »

I'm trying to make a custom world gen, and I'm over-writing the trees and decorations in my surface, but entity.destroy isn't working.

Code: Select all

script.on_event(defines.events.on_chunk_generated, function(event)
  if(event.surface == game.surfaces.Caves) then
    
    local caves = game.surfaces.Caves
    local area = event.area
    
    local entitiesSpawned = caves.find_entities(area)
    
    for _,v in pairs(entitiesSpawned) do
      
      local rngNum = math.random(0.1, 1) -- not used yet
      
      if(v.type == "resource" or v.type == "decorative") then
        v.destroy()
        return
      end
      
      if(v.type == "tree") then
        caves.create_entity{name="stone-rock", position = v.position}
        v.destroy()
      end
      
      game.player.print(v.type) -- properly prints out the types, although doesn't print tree, resource or deco, but it doesn't destroy them or spawn the rocks
    end
    
  end
end)
It properly detects the entities because I was debugging using '' and it was printing the types out.

EDIT: me destroying things manually using the command prompt properly destroys things

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5246
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: entity.destroy not working.

Post by Klonan »

I looked over it, and couldn't really see much reason why it wouldnt work, but i edited it a little, and hope this might work:

Code: Select all

script.on_event(defines.events.on_chunk_generated, function(event)
  if event.surface == game.surfaces.Caves then
   
    local caves = game.surfaces.Caves
    local a = event.area
     
    for k, v in pairs(caves.find_entities({area = a})) do
     
      local rngNum = math.random(0.1, 1) -- not used yet
     
      if v.type == "resource" or v.type == "decorative" then
        v.destroy()
      end
     
      if v.type == "tree" then
      	v.destroy()
        caves.create_entity{name="stone-rock", position = v.position}        
      end
     
      game.player.print(v.type) -- properly prints out the types, although doesn't print tree, resource or deco, but it doesn't destroy them or spawn the rocks
    end
   
  end
end)

jorgenRe
Filter Inserter
Filter Inserter
Posts: 535
Joined: Wed Apr 09, 2014 3:32 pm
Contact:

Re: entity.destroy not working.

Post by jorgenRe »

Okay i see you are tryieing to do basically the same thing as i did with the trees in my mod. Good job :)!
-Jorg Co Industries

Okay so heres the code that does the job in the Underground mod:

Code: Select all

  --Remove the trees and change rocks to 
  results = game.surfaces["theUnderground"].find_entities_filtered{area = {{game.players[1].position.x -searchDist, game.players[1].position.y -searchDist}, {game.players[1].position.x +searchDist, game.players[1].position.y +searchDist}}, type = "tree"}
  for d,ent in pairs(results) do
    ent.destroy()
  end
  results = game.surfaces["theUnderground"].find_entities_filtered{area = {{game.players[1].position.x -searchDist, game.players[1].position.y -searchDist}, {game.players[1].position.x +searchDist, game.players[1].position.y +searchDist}}, name = "stone-rock"}
    for d,ent in pairs(results) do
      game.surfaces["theUnderground"].create_entity({name = "lava", position = ent.position})
      game.surfaces["theUnderground"].create_entity({name = "fake-pole", position = ent.position})
      game.surfaces["theUnderground"].create_entity({name = "fake-battery", position = ent.position})
	    if game.surfaces["theUnderground"].count_entities_filtered{area = {{ent.position.x-25, ent.position.y-25}, {ent.position.x+25, ent.position.y+25}}, name= "opening"} == 0 then
          if game.surfaces["theUnderground"].get_tile(ent.position.x+10, ent.position.y+10).name == "lavaStone" then
		    --game.surfaces["theUnderground"].create_entity({name = "shadow-spawner", position = {ent.position.x+10, ent.position.y+10}})
		  end
		  if game.surfaces["theUnderground"].get_tile(ent.position.x+10, ent.position.y).name == "lavaStone" then
			--game.surfaces["theUnderground"].create_entity({name = "shadow-spawner", position = {ent.position.x+10, ent.position.y}})
		  end
		end
	 ent.destroy()
  end
Now the only thing i could see would be the reason to the problem would be the way you get the area. Now i did kinda fake it by on start of world create loads...Just loads of markers. But that should probably work. So i advice you to just use a fair bit of print and print out the area to see if it meets the required info which it probably does, but just for the sake of it ;)!
Last edited by jorgenRe on Wed Jan 06, 2016 5:01 pm, edited 1 time in total.
Logo
Noticed the told change in FFF #111 so il continue to use my signature ^_^
Thanks for listening to our suggestions, devs :D!
I would jump of joy if we could specify which tiles spawned in a surfaces

jorgenRe
Filter Inserter
Filter Inserter
Posts: 535
Joined: Wed Apr 09, 2014 3:32 pm
Contact:

Re: entity.destroy not working.

Post by jorgenRe »

Okay nevermind i see on wiki here is the example:

Code: Select all

game.surfaces["name-of-surface"].find_entities{{-10, -10}, {10, 10}}
Notice the use of "{" instead of "("

jorgenRe
Filter Inserter
Filter Inserter
Posts: 535
Joined: Wed Apr 09, 2014 3:32 pm
Contact:

Re: entity.destroy not working.

Post by jorgenRe »

It now works :D!
And il steal your idea for the next version of the underground :lol:!

Code: Select all

script.on_event(defines.events.on_chunk_generated, function(event)
    
    --local caves = game.surfaces.Caves
    local area = event.area
    local entitiesSpawned = game.surfaces["nauvis"].find_entities{{area.left_top.x, area.left_top.y}, {area.right_bottom.x, area.right_bottom.y}}
    for _,v in pairs(entitiesSpawned) do
      message("Found a: "..v.type)
	  local destroyed = false
      if(v.type == "resource" or v.type == "decorative") then
        v.destroy()
		--message("Removed it :P")
		destroyed = true
      end
	  if (destroyed == false) then
      if(v.type == "tree") then
        v.destroy()
		--message("Removed it :P")
      end
	  end
    end
end)
Basically it had something to do with the return you had. So that it would basically so oh i found something, well il just end this stuff.
So instead im just using the local variable destroyed and ask is it true, because if its true then v is gone and so will be invalid when destroyed again.
Also i found that the structure of the table area was as following: area, the bounds of the chunk generated, a table in the format {left_top={x=x1, y=y1}, right_bottom={x=x2,y=y2}}
So remember to use the wiki as its super useful ;)!
PS it is a nice thing to use this message function im using so much easier to write and its one less thing to fix for multiplayer compatibility ;)!

Code: Select all

function message(mes)
  for i, player in ipairs(game.players) do
    if (player.connected) then 
    player.print(mes)
	end
  end
end
Now how did one comment out a bigger section of code in lua hmmmmmm (been doing too much PHP, jscript, html sql etc etc :ugeek:?)
Logo
Noticed the told change in FFF #111 so il continue to use my signature ^_^
Thanks for listening to our suggestions, devs :D!
I would jump of joy if we could specify which tiles spawned in a surfaces

memcallen
Inserter
Inserter
Posts: 34
Joined: Sat Mar 28, 2015 1:22 am
Contact:

Re: entity.destroy not working.

Post by memcallen »

I have a temp file for code I might need in the future, but never use. Also you do --[[ and --]] (start/end, respectively) to comment out large sections of code (here).

Also, instead of using the destroyed boolean you could use v.valid

Code: Select all

script.on_event(defines.events.on_chunk_generated, function(event)
    
    --local caves = game.surfaces.Caves
    local area = event.area
    local entitiesSpawned = game.surfaces["nauvis"].find_entities{{area.left_top.x, area.left_top.y}, {area.right_bottom.x, area.right_bottom.y}}
    for _,v in pairs(entitiesSpawned) do
      message("Found a: "..v.type)
      if(v.type == "resource" or v.type == "decorative") then
        v.destroy()
      --message("Removed it :P")
      end
     if (v.valid) then
      if(v.type == "tree") then
        v.destroy()
      --message("Removed it :P")
      end
     end
    end
end)

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

Re: entity.destroy not working.

Post by Rseding91 »

memcallen wrote:I have a temp file for code I might need in the future, but never use. Also you do --[[ and --]] (start/end, respectively) to comment out large sections of code (here).

Also, instead of using the destroyed boolean you could use v.valid

Code: Select all

script.on_event(defines.events.on_chunk_generated, function(event)
    
    --local caves = game.surfaces.Caves
    local area = event.area
    local entitiesSpawned = game.surfaces["nauvis"].find_entities{{area.left_top.x, area.left_top.y}, {area.right_bottom.x, area.right_bottom.y}}
    for _,v in pairs(entitiesSpawned) do
      message("Found a: "..v.type)
      if(v.type == "resource" or v.type == "decorative") then
        v.destroy()
      --message("Removed it :P")
      end
     if (v.valid) then
      if(v.type == "tree") then
        v.destroy()
      --message("Removed it :P")
      end
     end
    end
end)
on_chunk_generated passes the surface that the chunk was generated on - you should be using that and not the hard coded surface.

Code: Select all

script.on_event(defines.events.on_chunk_generated, function(event)
    local area = event.area
    local entitiesSpawned = event.surface.find_entities{{area.left_top.x, area.left_top.y}, {area.right_bottom.x, area.right_bottom.y}}
    for _,v in pairs(entitiesSpawned) do
      if(v.type == "resource" or v.type == "decorative" or v.type == "tree") then
        v.destroy()
      end
    end
  end
end)
If you want to get ahold of me I'm almost always on Discord.

jorgenRe
Filter Inserter
Filter Inserter
Posts: 535
Joined: Wed Apr 09, 2014 3:32 pm
Contact:

Re: entity.destroy not working.

Post by jorgenRe »

Rseding91 wrote:
on_chunk_generated passes the surface that the chunk was generated on - you should be using that and not the hard coded surface.
Luckily yea :D!
Logo
Noticed the told change in FFF #111 so il continue to use my signature ^_^
Thanks for listening to our suggestions, devs :D!
I would jump of joy if we could specify which tiles spawned in a surfaces

memcallen
Inserter
Inserter
Posts: 34
Joined: Sat Mar 28, 2015 1:22 am
Contact:

Re: entity.destroy not working.

Post by memcallen »

Ok I didn't know there was a different, I thought it was just a convenience variable

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

Re: entity.destroy not working.

Post by Rseding91 »

memcallen wrote:Ok I didn't know there was a different, I thought it was just a convenience variable
It's both :) The event.surface is not guaranteed to be the default base surface - if any other surface is created it will be any one of the surfaces (which ever one generated the chunk).
If you want to get ahold of me I'm almost always on Discord.

Post Reply

Return to “Modding help”