Help with removing hidden Entities

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

Help with removing hidden Entities

Post by TheSAguy »

Hi,

I have a greenhouse mod that adds a few hidden entities.
When you build the green-house, I also place a electric pole, lamp and solar panel.

Some people have said that when they remove the greenhouse, some of these hidden entities remain. I've not been able to re-produce this, so was hoping someone coujld please look at the code and tell me if the're a better way of writing it.

Code: Select all

---------------------------------------------
function On_Built(event)
     
    --- Bio Farm has been built
	if event.created_entity.name == "bf_bio_farm" then
		local surface = event.created_entity.surface
		local force = event.created_entity.force
		surface.create_entity({name = "bf_medium-electric-pole_for_Bio_Farm", position = event.created_entity.position, force = force})
		surface.create_entity({name = "bf_light_for_Bio_Farm", position = event.created_entity.position, force = force})
		surface.create_entity({name = "bf_solar-panel_for_Bio_Farm", position = event.created_entity.position, force = force})
	end
end
 
---------------------------------------------
function On_Remove(event)
	
	--- Bio Farm has been removed
   	if event.entity.name == "bf_bio_farm" then
		
		res2 = game.get_surface(1).find_entities_filtered{name="bf_medium-electric-pole_for_Bio_Farm", area=GetArea(event.entity.position, 0.5)}
		res = game.get_surface(1).find_entities_filtered{name="bf_light_for_Bio_Farm", area=GetArea(event.entity.position, 0.5)}
		res3 = game.get_surface(1).find_entities_filtered{name="bf_solar-panel_for_Bio_Farm", area=GetArea(event.entity.position, 0.5)}

		if #res then
         -- If we've found it, destroy it.
         res[1].destroy()
		end
		if #res2 then
         -- If we've found it, destroy it.
         res2[1].destroy()
		end
		if #res3 then
         -- If we've found it, destroy it.
         res3[1].destroy()
		end
	end

end
Thanks.
Attachments
Bio_Farm_0.6.2.zip
Farm Mod
(2.03 MiB) Downloaded 89 times
User avatar
bloc97
Inserter
Inserter
Posts: 47
Joined: Sat Apr 16, 2016 4:57 am
Contact:

Re: Help with removing hidden Entities

Post by bloc97 »

I don't have much time but I can give you a hint.

Its better to save the hidden entity in a separate table, like for example global.greenhouseentities={}
Then when you remove the greenhouse entity, you can lookup that table (using in pairs) and remove the associated hidden entities.

Edit: Your method works too!, but it is looking on the map instead of in a table, which *might* slightly be slower depending on how the game is coded...
Image-Vehicles, Defense, Energy & More!
Image-Keep inventory on death!
AenAllAin
Long Handed Inserter
Long Handed Inserter
Posts: 61
Joined: Sat Apr 02, 2016 3:10 am
Contact:

Re: Help with removing hidden Entities

Post by AenAllAin »

TheSAguy wrote:Hi,

I have a greenhouse mod that adds a few hidden entities.
When you build the green-house, I also place a electric pole, lamp and solar panel.

Some people have said that when they remove the greenhouse, some of these hidden entities remain. I've not been able to re-produce this, so was hoping someone coujld please look at the code and tell me if the're a better way of writing it.

...

Thanks.
Has anyone who complained been specific? Would they happen to mean that this occurs when they use robots to deconstruct? Are you setting your helper entities 'minable' property to false upon creation in your 'control.lua'?

Code: Select all

        l_wall.minable = false
For example, I had the issue you are referring to with my helper entities for my powered walls until I did this:

Code: Select all

function build_powered_wall(event)
    local entity = event.created_entity
    if entity and entity.name == "powered-wall-wall" then
        local l_name = "powered-wall-pole"
        local l_surface = entity.surface
        local l_force = entity.force
        local l_position = entity.position
        local l_wall = entity
        local l_pole = l_surface.create_entity{name = l_name, position = l_position, force = l_force}
        l_wall.minable = false
        l_wall.destructible = false

        group_entities(cantor(l_position.x,l_position.y), { l_wall, l_pole })
    end
end
...just one possibility; hope it helps.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1455
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Help with removing hidden Entities

Post by TheSAguy »

bloc97 wrote:I don't have much time but I can give you a hint.

Its better to save the hidden entity in a separate table, like for example global.greenhouseentities={}
Then when you remove the greenhouse entity, you can lookup that table (using in pairs) and remove the associated hidden entities.

Edit: Your method works too!, but it is looking on the map instead of in a table, which *might* slightly be slower depending on how the game is coded...
Blov97, This is ideally what I'd like to do. I have tried it in the past woth my other mod, but was unable to figure it out, so I hope when you have some time you could assist me with it. Thanks!!
AenAllAin wrote:
Has anyone who complained been specific? Would they happen to mean that this occurs when they use robots to deconstruct? Are you setting your helper entities 'minable' property to false upon creation in your 'control.lua'?

Code: Select all

        l_wall.minable = false
For example, I had the issue you are referring to with my helper entities for my powered walls until I did this:

Code: Select all

function build_powered_wall(event)
    local entity = event.created_entity
    if entity and entity.name == "powered-wall-wall" then
        local l_name = "powered-wall-pole"
        local l_surface = entity.surface
        local l_force = entity.force
        local l_position = entity.position
        local l_wall = entity
        local l_pole = l_surface.create_entity{name = l_name, position = l_position, force = l_force}
        l_wall.minable = false
        l_wall.destructible = false

        group_entities(cantor(l_position.x,l_position.y), { l_wall, l_pole })
    end
end
...just one possibility; hope it helps.
AenAllAin, I've not done anything with the minable flag, so I'll check it out.
I do think the problem comes with bots removing entities, though I do have:
script.on_event({defines.events.on_entity_died,defines.events.on_robot_pre_mined_item,defines.events.on_preplayer_mined_item,},function(event) On_Remove(event) end)
AenAllAin
Long Handed Inserter
Long Handed Inserter
Posts: 61
Joined: Sat Apr 02, 2016 3:10 am
Contact:

Re: Help with removing hidden Entities

Post by AenAllAin »

TheSAguy wrote:...
AenAllAin wrote:...
...just one possibility; hope it helps.
AenAllAin, I've not done anything with the minable flag, so I'll check it out.
I do think the problem comes with bots removing entities, though I do have:
script.on_event({defines.events.on_entity_died,defines.events.on_robot_pre_mined_item,defines.events.on_preplayer_mined_item,},function(event) On_Remove(event) end)
SAguy, I was having kind of the inverse of your problem, I think for similar reasons. My mod was crashing at first from additional attempts to remove entities that were already invalid because of the robots "seeing" the invisible entities and removing them first. (This is still causing issues with blueprints and I have not figured out how to fix it as there are no blueprint events to trigger and correct on.)

I think the source of your issue is somehow the same but opposite; so the minable flag might prevent the robots from doing things out-of-order in a way. You may still have issues with blueprints though, unless you figured out a way to solve that already.

I used the method bloc97 was referring to of grouping entities with my mod. If it would help at all, I wrote some reasonably modular grouping and hashing source for one of my mods that you are welcome to use. I would be elated if it could be re-used by someone and help in any way. It's still an alpha mod, but here is the code link: https://github.com/AenAllAin/factorio-walls. The file with the modular code is "util_ext.lua".

On a broader note, I haven't really seen the dev's weigh in on whether they approve of us doing this sort of thing. Based on the mod API structuring, I would guess that they do not like it or want it. (1) they do not expose or allow multiple prototype inheritance, which is what forces us to use a grouping mechanism to begin with, (2) they have not ID'd the entities or enabled stable hash-map comparisons which are necessary for efficient/straight-forward keying, (3) they have not provided or sanctioned the use of supplemental methods to overcome these issues ...so basically, there isn't any straight-forward support for this type of modding.

It is honestly starting to look like they don't intend for us to really 'mod' or change the game; it actually looks more like they built a basic game engine, exposed a limited part of it with a mod interface. and released it in alpha state to the community so that we could then run around implementing all the content for them and they can sit back and cherry-pick the front runners for inclusion in the final game. ...Save money, save development time, make bank.

I am really hoping this isn't the case, because it may make sense as a business strategy, but morally and ethically it is dishonest and disingenuous to the whole community; in a word that is a "dick-move" ...but only time will tell.
User avatar
bloc97
Inserter
Inserter
Posts: 47
Joined: Sat Apr 16, 2016 4:57 am
Contact:

Re: Help with removing hidden Entities

Post by bloc97 »

AenAllAin wrote: On a broader note, I haven't really seen the dev's weigh in on whether they approve of us doing this sort of thing. Based on the mod API structuring, I would guess that they do not like it or want it. (1) they do not expose or allow multiple prototype inheritance, which is what forces us to use a grouping mechanism to begin with, (2) they have not ID'd the entities or enabled stable hash-map comparisons which are necessary for efficient/straight-forward keying, (3) they have not provided or sanctioned the use of supplemental methods to overcome these issues ...so basically, there isn't any straight-forward support for this type of modding.
Yes you are definitively right, it is strange that the game does not use entity IDs, which would make these types of things much much easier, as you can lookup specific IDs and make a list instead of brute-forcing through the entire table and finding the names.
And the entities' properties are not open to control.lua, which means you can't have dynamic entities (entities that change sprites, damage resistance, etc). It would be a bliss if they could give some way for us modders to access the entity table...

And to the OP, AenAllAin's mod uses one type of table lookup that should work most of the time, especially when you don't have a ton of different entities.
Image-Vehicles, Defense, Energy & More!
Image-Keep inventory on death!
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1455
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Help with removing hidden Entities

Post by TheSAguy »

Thanks guys!! I'll review this and update my mod.
Post Reply

Return to “Modding help”