Hidden objects not always getting placed/removed

Place to get help with not working mods / modding interface.
Post Reply
lettherebelight
Burner Inserter
Burner Inserter
Posts: 11
Joined: Wed Nov 02, 2016 2:57 pm
Contact:

Hidden objects not always getting placed/removed

Post 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!

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Hidden objects not always getting placed/removed

Post 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
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

lettherebelight
Burner Inserter
Burner Inserter
Posts: 11
Joined: Wed Nov 02, 2016 2:57 pm
Contact:

Re: Hidden objects not always getting placed/removed

Post 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?

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5150
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Hidden objects not always getting placed/removed

Post 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"},

lettherebelight
Burner Inserter
Burner Inserter
Posts: 11
Joined: Wed Nov 02, 2016 2:57 pm
Contact:

Re: Hidden objects not always getting placed/removed

Post 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.

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5150
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Hidden objects not always getting placed/removed

Post 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

lettherebelight
Burner Inserter
Burner Inserter
Posts: 11
Joined: Wed Nov 02, 2016 2:57 pm
Contact:

Re: Hidden objects not always getting placed/removed

Post 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

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Hidden objects not always getting placed/removed

Post 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*.
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5150
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Hidden objects not always getting placed/removed

Post 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

lettherebelight
Burner Inserter
Burner Inserter
Posts: 11
Joined: Wed Nov 02, 2016 2:57 pm
Contact:

Re: Hidden objects not always getting placed/removed

Post 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!

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Hidden objects not always getting placed/removed

Post 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
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

Post Reply

Return to “Modding help”