Page 1 of 1

Hidden objects not always getting placed/removed

Posted: Wed Nov 02, 2016 3:03 pm
by lettherebelight
I've got a mod up and running that adds lit versions of the four power poles (small, med, large, substation) to the game. It works great once the poles are placed, however during playtesting I discovered two intermittent issues:

1. The hidden light doesn't always get added when FARL places the pole[/list]

2. The hidden light doesn't always get removed when a bot deconstructs the pole

I'm registering for bot construction/deconstruction events:

Code: Select all

script.on_event(defines.events.on_robot_built_entity, BuiltEntity)
script.on_event(defines.events.on_robot_pre_mined  , MinedEntity)
When constructed I do the following:

Code: Select all

function BuiltEntity(event)
	if (event.created_entity.name == "lighted-small-electric-pole") or
	   (event.created_entity.name == "lighted-medium-electric-pole") or
	   (event.created_entity.name == "lighted-big-electric-pole") or
	   (event.created_entity.name == "lighted-substation")	   
	   then	
		local e = event.created_entity
		local s = e.surface
		local X = e.position.x
		local Y = e.position.y
    local l = s.create_entity{name = "hidden-small-lamp", position = {X,Y}, force= game.forces.neutral}
		l.destructible = false
	end
end
When deconstructed I do the following:

Code: Select all

function MinedEntity(event)
	if (event.entity.name == "lighted-small-electric-pole") or
	   (event.entity.name == "lighted-medium-electric-pole") or
	   (event.entity.name == "lighted-big-electric-pole") or
	   (event.entity.name == "lighted-substation")
	   then
	local b = event.entity
	local X = b.position.x 
	local Y = b.position.y		
	lamp = b.surface.find_entity("hidden-small-lamp",{X, Y})
	if lamp ~= nil then
		lamp.destroy()	end		
	end
end
Anyone have ideas as to why this sometimes works for FARL construction and robot deconstruction, but sometimes doesn't work? In both cases the pole is created/removed, but the hidden light isn't.

Thanks!

Re: Hidden objects not always getting placed/removed

Posted: Wed Nov 02, 2016 5:25 pm
by aubergine18
If you build two entities on exact same coordinate, sometimes one will replace the other or not get built (depends on entity type as far as I can tell).

When destroying entities, it's better to do an area search than a position search.

Also, your code would be a bit cleaner by replacing this:

Code: Select all

 if (event.entity.name == "lighted-small-electric-pole") or
      (event.entity.name == "lighted-medium-electric-pole") or
      (event.entity.name == "lighted-big-electric-pole") or
      (event.entity.name == "lighted-substation")
      then
With something like this:

Code: Select all

local applicable_entity = {
  ["lighted-small-electric-pole"] = true,
  ["lighted-medium-electric-pole"] = true,
  ["lighted-big-electric-pole"] = true,
  ["lighted-substation"] = true,
}

function BuiltEntity(event)
  local entity = event.created_entity
  if applicable_entity[ entity.name ] then
    -- ...
  end
end

function MinedEntity(event)
  local entity = event.entity
  if applicable_entity[ entity.name ] then
    -- ...
  end
end

Re: Hidden objects not always getting placed/removed

Posted: Thu Nov 03, 2016 5:45 pm
by lettherebelight
Thanks for the reply. I spent some time this morning attempting to fix the deconstruction problem by swapping over to an area search. My new code, using Factorio stdlib, is as follows:

Code: Select all

		
		local b = event.entity
		local X = b.position.x 
		local Y = b.position.y		
		
		local hidden_lights = Surface.find_all_entities({
			type = 'hidden-small-lamp',
			surface = b.surface,
			area = Position.expand_to_area(b.position, 5),
			force = game.forces.neutral})
				
		if hidden_lights ~= nil then
			for i, light in ipairs(hidden_lights) do
				light.destroy()
			end
		end		
This doesn't find lights at all, regardless of whether the robots do it or whether I manually deconstruct the pole via right-click.

Any thoughts as to why this approach isn't working?

Re: Hidden objects not always getting placed/removed

Posted: Thu Nov 03, 2016 6:13 pm
by Klonan
What flags do you have set in the lamp definition?

In my mod i have these flags, and the code works just fine:

Code: Select all

flags = {"placeable-off-grid", "not-on-map"},

Re: Hidden objects not always getting placed/removed

Posted: Thu Nov 03, 2016 6:26 pm
by lettherebelight
I have the same flags in the lamp definition:

flags = {"placeable-off-grid", "not-on-map"},

What mod are you doing something similar in? Maybe if I compare the code I can see where I have some flag or something set differently.

Re: Hidden objects not always getting placed/removed

Posted: Thu Nov 03, 2016 9:39 pm
by Klonan
lettherebelight wrote:I have the same flags in the lamp definition:

flags = {"placeable-off-grid", "not-on-map"},

What mod are you doing something similar in? Maybe if I compare the code I can see where I have some flag or something set differently.

In the concrete lamppost mod: https://mods.factorio.com/mods/Klonan/Concrete_Lamppost

Re: Hidden objects not always getting placed/removed

Posted: Thu Nov 03, 2016 9:45 pm
by lettherebelight
Ah, that's yours! It's great, I used your approach as a base for what I'm working on. I've re-done my removal code again and it appears that robots are correctly cleaning things up now.

Did you ever have trouble with FARL not placing your hidden light? FARL will place my poles, but not the light :(

Neil

Re: Hidden objects not always getting placed/removed

Posted: Fri Nov 04, 2016 5:38 pm
by aubergine18
Your error in that area search was this:

Code: Select all

type = 'hidden-small-lamp'
'hidden-small-lamp' is the *name* of your prototype, not it's *type*.

Re: Hidden objects not always getting placed/removed

Posted: Fri Nov 04, 2016 7:36 pm
by Klonan
lettherebelight wrote:Ah, that's yours! It's great, I used your approach as a base for what I'm working on. I've re-done my removal code again and it appears that robots are correctly cleaning things up now.

Did you ever have trouble with FARL not placing your hidden light? FARL will place my poles, but not the light :(

Neil

It was a problem that had to be solved on FARL's end,

He simply raised a 'created_entity' event whenever his mod would place a concrete lamppost

Re: Hidden objects not always getting placed/removed

Posted: Fri Nov 04, 2016 8:54 pm
by lettherebelight
Ah yes, I see right at the top he's got a trigger_event list for supported poles that trigger the event getting fired. I'll hand-hack that to update it with my poles, make sure that works, then will reach out to the author and request an update.

Thanks for everyone's help!

Re: Hidden objects not always getting placed/removed

Posted: Sat Nov 05, 2016 3:06 pm
by aubergine18
This might be of use for FARL or any other mod that creates/destroys entities that are part of separate mods: https://github.com/aubergine10/lifecycle-events