[Resolved] Why is Entity not removed

Place to get help with not working mods / modding interface.
Post Reply
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

[Resolved] Why is Entity not removed

Post by TheSAguy »

Hi,

I have some hidden entities that I destroy once the primary entity is destroyed.
Currently I have a radar, and was hoping to add a power pole and a lamp.

I need to make it compatible with existing games so if you already had the entity you won't have any issues.

Below is my code. It works as intended with NEW entities built, but when I remove Existing entities that did not have the new items, the old items are not removed.

In the image below, this was an old item, it only had the Radar, and pole, not the lamp, but when I now remove it those don't get removed.
Image

Code When Building:

Code: Select all

	if entity.valid and entity.name == "bi-arboretum-area" then
	writeDebug("Arboretum has been built")
		   
		local arboretum_new = "bi-arboretum"
		local radar_name = "bi-arboretum-radar"  
		local pole_name = "bi-hidden-power-pole"
		local lamp_name = "bi-bio-farm-light"      	
		
		local create_arboretum = surface.create_entity({name = arboretum_new, position = position, direction = entity.direction, force = force})  -- New Arboretum, the first was just used for Radius overlay
		local create_pole = surface.create_entity({name = pole_name, position = position, direction = entity.direction, force = force})  -- Hidden pole
		local create_lamp = surface.create_entity({name = lamp_name, position = position, force = force}) -- Hidden Lamp
		
		local position_c = {position.x - 3.5, position.y + 3.5}
		local create_radar = surface.create_entity({name = radar_name, position = position_c, direction = entity.direction, force = force}) -- Hidden Radar


		create_radar.minable = false
		create_radar.destructible = false
		create_pole.minable = false
		create_pole.destructible = false
		create_lamp.minable = false
		create_lamp.destructible = false
		
		-- Remove the "Overlay" Entity
		event.created_entity.destroy()
		
		-- Group Multiple Entities Together
		global.Arboretum_Table[create_arboretum.unit_number] = {inventory=create_arboretum, radar=create_radar, pole=create_pole, lamp=create_lamp}

	end
Code when removing (or destroyed)

Code: Select all

	--- Arboretum has been removed
   	if entity.valid and entity.name == "bi-arboretum" then
	writeDebug("Arboretum has been removed")	
		
		if global.Arboretum_Table[entity.unit_number] then
			global.Arboretum_Table[entity.unit_number].radar.destroy()
			if global.Arboretum_Table[entity.unit_number].pole.valid then
				global.Arboretum_Table[entity.unit_number].pole.destroy()
			end
			if global.Arboretum_Table[entity.unit_number].lamp.valid then
				global.Arboretum_Table[entity.unit_number].lamp.destroy()
			end				
			global.Arboretum_Table[entity.unit_number] = nil
		end
		
	end
I also tried it without the ".valid" with the same results.
Not sure what to do here.

Again, it works as intended with new Entities, but Existing entities, before the new Items are added is causing an issue.

Thanks.
Last edited by TheSAguy on Wed May 08, 2019 2:16 pm, edited 1 time in total.

eduran
Filter Inserter
Filter Inserter
Posts: 344
Joined: Fri May 09, 2014 2:52 pm
Contact:

Re: Why is Entity not removed

Post by eduran »

I don't understand why your code does not throw an error for "old" entities:

Code: Select all

if global.Arboretum_Table[entity.unit_number].lamp.valid then
If the above runs on an entity without lamp, you should get a trying-to-index-nil-value error, because global.Arboretum_Table[entity.unit_number].lamp == nil. So that if-statement is not executed. Not sure if that helps.

Some general advice on how to debug such an issue:
As a first step, use either game.print or log to figure out how far into the nested if statements your code advances. Adding something like game.print("passed if statement <some identifying name here> after every if should do that.

Code: Select all

[...]
writeDebug("Arboretum has been removed") --  let's assume you see this printed
if global.Arboretum_Table[entity.unit_number] then
  game.print("passed if statement: global.Arboretum_Table[entity.unit_number]")  -- but not this
  global.Arboretum_Table[entity.unit_number].radar.destroy()
[...]
Now you know "global.Arboretum_Table[entity.unit_number]" evaluates to false. Now let's figure out why:

Code: Select all

[...]
writeDebug("Arboretum has been removed") 
log("entity unit_number: " .. entity.unit_number)
log("global.Arboretum_Table: " .. serpent.block(global.Arboretum_Table))
error() -- optional, but I find it convenient to abort right after the relevant info was logged
  if global.Arboretum_Table[entity.unit_number] then
[...]
Have a look at your log file and see what that table of yours actually stores.

TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Why is Entity not removed

Post by TheSAguy »

Okay, I've added the following:
On Built:

Code: Select all

    --- Arboretum has been built
	if entity.valid and entity.name == "bi-arboretum-area" then
	writeDebug("Arboretum has been built")
		   
		local arboretum_new = "bi-arboretum"
		local radar_name = "bi-arboretum-radar"  
		local pole_name = "bi-hidden-power-pole"
		local lamp_name = "bi-bio-farm-light"      	
		
		local create_arboretum = surface.create_entity({name = arboretum_new, position = position, direction = entity.direction, force = force})  -- New Arboretum, the first was just used for Radius overlay
		local position_c = {position.x - 3.5, position.y + 3.5}
		local create_radar = surface.create_entity({name = radar_name, position = position_c, direction = entity.direction, force = force}) -- Radar
		local create_pole = surface.create_entity({name = pole_name, position = position, direction = entity.direction, force = force})  -- Hidden pole
		local create_lamp = surface.create_entity({name = lamp_name, position = position, force = force}) -- Hidden Lamp
		
		create_radar.minable = false
		create_radar.destructible = false
		create_pole.minable = false
		create_pole.destructible = false
		create_lamp.minable = false
		create_lamp.destructible = false
		
		-- Remove the "Overlay" Entity
		event.created_entity.destroy()
		
		-- Group Multiple Entities Together
		global.Arboretum_Table[create_arboretum.unit_number] = {inventory=create_arboretum, radar=create_radar, pole=create_pole, lamp=create_lamp}
		log("built: entity unit_number: " .. create_arboretum.unit_number)
		log("built: global.Arboretum_Table: " .. serpent.block(global.Arboretum_Table[create_arboretum.unit_number]))
		
	end
On Remove:

Code: Select all

	--- Arboretum has been removed
   	if entity.valid and entity.name == "bi-arboretum" then
		
		writeDebug("Arboretum has been removed")	
		log("entity unit_number: " .. entity.unit_number)
		log("global.Arboretum_Table: " .. serpent.block(global.Arboretum_Table[entity.unit_number]))
		
		if global.Arboretum_Table[entity.unit_number] then 
		game.print("passed if statement: global.Arboretum_Table[entity.unit_number]")  -- it does not get here now!
		
			global.Arboretum_Table[entity.unit_number].radar.destroy()
			if global.Arboretum_Table[entity.unit_number].pole.valid then
				global.Arboretum_Table[entity.unit_number].pole.destroy()
			end
			if global.Arboretum_Table[entity.unit_number].lamp.valid then
				global.Arboretum_Table[entity.unit_number].lamp.destroy()
			end				
			global.Arboretum_Table[entity.unit_number] = nil
		end
		
	end


I get this log:

Code: Select all

  43.438 Script @__Bio_Industries__/control.lua:371: built: entity unit_number: 502
  43.438 Script @__Bio_Industries__/control.lua:372: built: global.Arboretum_Table: {
  inventory = {
    __self = "userdata: 0x000001fcdbe4cb80"
  },
  lamp = {
    __self = "userdata: 0x000001fcdbe4efe0"
  },
  pole = {
    __self = "userdata: 0x000001fcdbe4f7c0"
  },
  radar = {
    __self = "userdata: 0x000001fcdbe4e320"
  }
}
  49.466 Script @__Bio_Industries__/control.lua:510: entity unit_number: 502
  49.466 Script @__Bio_Industries__/control.lua:511: global.Arboretum_Table: nil
  73.238 Quitting: user-quit.
Not exactly sure what to do with this...
I don't understand how adding two entities to the table is corrupting it.

Going from:

Code: Select all

		
global.Arboretum_Table[create_arboretum.unit_number] = {inventory=create_arboretum, radar=create_radar}
to

Code: Select all

global.Arboretum_Table[create_arboretum.unit_number] = {inventory=create_arboretum, radar=create_radar, pole=create_pole, lamp=create_lamp}
Then the table goes blank...

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Why is Entity not removed

Post by DaveMcW »

TheSAguy wrote:
Tue May 07, 2019 11:16 pm

Code: Select all

-- Remove the "Overlay" Entity
event.created_entity.destroy()
Don't destroy stuff until everything else is finished.

eduran
Filter Inserter
Filter Inserter
Posts: 344
Joined: Fri May 09, 2014 2:52 pm
Contact:

Re: Why is Entity not removed

Post by eduran »

There has to be another part of your code that interacts with Arboretum_Table and deletes the entry before the remove function runs.

TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Why is Entity not removed

Post by TheSAguy »

DaveMcW wrote:
Wed May 08, 2019 4:47 am
TheSAguy wrote:
Tue May 07, 2019 11:16 pm

Code: Select all

-- Remove the "Overlay" Entity
event.created_entity.destroy()
Don't destroy stuff until everything else is finished.
It seems that cleared it up...
Thanks DaveMcW and Eduran!

Post Reply

Return to “Modding help”