Page 1 of 1

entity.destroy not working.

Posted: Mon Jan 04, 2016 9:49 pm
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

Re: entity.destroy not working.

Posted: Wed Jan 06, 2016 12:46 pm
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)

Re: entity.destroy not working.

Posted: Wed Jan 06, 2016 4:54 pm
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 ;)!

Re: entity.destroy not working.

Posted: Wed Jan 06, 2016 5:00 pm
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 "("

Re: entity.destroy not working.

Posted: Wed Jan 06, 2016 5:25 pm
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:?)

Re: entity.destroy not working.

Posted: Wed Jan 06, 2016 6:29 pm
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)

Re: entity.destroy not working.

Posted: Wed Jan 06, 2016 7:10 pm
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)

Re: entity.destroy not working.

Posted: Wed Jan 06, 2016 7:40 pm
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!

Re: entity.destroy not working.

Posted: Wed Jan 06, 2016 8:11 pm
by memcallen
Ok I didn't know there was a different, I thought it was just a convenience variable

Re: entity.destroy not working.

Posted: Wed Jan 06, 2016 8:23 pm
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).