Page 1 of 1

[Resolved] Table Question

Posted: Fri Jul 21, 2017 4:04 am
by TheSAguy
Okay, I can't get this figured out.
I am creating some hidden entities that I need to delete once I mine the main one, but also need to store all the entities in a table, since I need to check the inventory levels.

Here is my code for creating and storing the entities in the table:
On Build
Now when I mine the Station, I'm deleting all stations that exist....
On Remove

So "TerraformingStation_i" is the Main Entity. When it gets removed, I need to destroy the corresponding "TerraformingStation_c" and "TerraformingStation_r" entities.

I was thinking of checking the positions of each. If they match delete them, but try as I may, or I delete ALL "TerraformingStation_i" entities are removed, or I'm not deleting the other "c" and "r" entities...

Thanks for any help.

Re: Table Question

Posted: Fri Jul 21, 2017 6:32 am
by Optera
For me best practice is to store the main entity with its sub entities in one global list indexed by entity.unit_number and set script generated sub entities minable = false and destructible = false.

Code: Select all

local entity = event.created_entity
if entity.valid and entity.name == "TerraformingStation" then
  local T_Station_Inv = surface.create_entity({name = "TerraformingStation_i", position = position, direction = event.created_entity.direction, force = force})
  local T_Station_Container = surface.create_entity({name = "TerraformingStation_c", position = position, direction = event.created_entity.direction, force = force})
  local T_Station_Radar = surface.create_entity({name = "TerraformingStation_r", position = position, direction = event.created_entity.direction, force = force})
 
  global.T_Stations[entity.unit_number] = {entity=entity, inventory=T_Station_Inv, container=T_Station_Container, radar=T_Station_Radar}
end
Now if the main entity is destroyed or removed you can directly destroy the sub entities from the stored global entity list.

Code: Select all

local entity = event.entity
if entity.valid and entity.name == "TerraformingStation" then
  global.T_Stations[entity.unit_number].radar.destroy()
  global.T_Stations[entity.unit_number].container.destroy()
  global.T_Stations[entity.unit_number].inventory.destroy()
  global.T_Stations[entity.unit_number] = nil
end
Storing all entities belonging to one object makes accessing them very easy for other updates as well.

Re: Table Question

Posted: Fri Jul 21, 2017 7:52 pm
by TheSAguy
Thanks Optera, worked like a charm!