[Resolved] Connecting and Disconnecting Copper Wires

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

[Resolved] Connecting and Disconnecting Copper Wires

Post by TheSAguy »

I could really use some help with connecting and disconnecting copper wires on my Power Rail System.

There are 3 types on entities:

"bi-rail-hidden-power-pole" :
This is the hidden power pole that gets created when you place the power rail.
It should only connect to other "bi-rail-hidden-power-pole" or the "bi-power-to-rail-pole" entity.

"bi-power-to-rail-pole":
Can connect to both regular power poles or the "bi-rail-hidden-power-pole"

All other poles:
(type == "electric-pole")
Can't correct to the "bi-rail-hidden-power-pole" entity.


So this is what I'm doing:
Check 1: Was a "bi-rail-hidden-power-pole" built:

Code: Select all

	if entity.valid and entity.name == "bi-rail-hidden-power-pole" then
	
		for _,neighbour in pairs(entity.neighbours.copper) do
		
			if neighbour.name ~= "bi-rail-hidden-power-pole" and neighbour.name ~= "bi-power-to-rail-pole" then
				entity.disconnect_neighbour(neighbour)
			end
			
			if neighbour.name == "bi-rail-hidden-power-pole" or neighbour.name == "bi-power-to-rail-pole" then
				entity.connect_neighbour(neighbour)
			end
		
		end
	
	end
Check 2:
If any other poles are built, disconnect it from the "bi-rail-hidden-power-pole"

Code: Select all

	--- Disconnect any other power lines from the rail-hidden-power pole
	if entity.valid and entity.type == "electric-pole" then
		
		if entity.name ~= "bi-rail-hidden-power-pole" and entity.name ~= "bi-power-to-rail-pole" then
			
			for _,neighbour in pairs(entity.neighbours.copper) do

				if neighbour.name == "bi-rail-hidden-power-pole" then
					entity.disconnect_neighbour(neighbour)
				end
			
			end
	
		end
		
	end
If I place the Power rail first, (that creates the "bi-rail-hidden-power-pole" entity), then other power poles. It does not connect to them, unless it's the "bi-power-to-rail-pole". So that works!

Now if I place other poles first, then lay the power rail (that creates the "bi-rail-hidden-power-pole" entity), they do connect!
That should not happen.
It's okay to connect to the "bi-power-to-rail-pole" or other "bi-rail-hidden-power-pole", but not other power poles

I can't seem to get this to work if the power rail is built after other poles in the area...

Thanks.
Last edited by TheSAguy on Sun May 13, 2018 6:46 am, edited 2 times in total.
betrok
Fast Inserter
Fast Inserter
Posts: 101
Joined: Wed Feb 28, 2018 12:08 pm
Contact:

Re: Connecting and Disconnecting Copper Wires - HELP Please

Post by betrok »

It seems you are doing all this in on build handlers. But do you raise any events when creating hidden poles?
Btw neighbours do not include all possible variants, only already connected ones.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Connecting and Disconnecting Copper Wires - HELP Please

Post by eradicator »

betrok wrote:It seems you are doing all this in on build handlers. But do you raise any events when creating hidden poles?
As hidden power poles are never "built" but rather always spawned in OP should just call the relevant function directly when spawning instead of trying to filter through on_built.
betrok wrote:Btw neighbours do not include all possible variants, only already connected ones.
And also new power poles only connect to a limited number of adjacent poles, even if range would allow to connect to more. This avoids cable chaos.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Connecting and Disconnecting Copper Wires - HELP Please

Post by TheSAguy »

eradicator wrote:
betrok wrote:It seems you are doing all this in on build handlers. But do you raise any events when creating hidden poles?
As hidden power poles are never "built" but rather always spawned in OP should just call the relevant function directly when spawning instead of trying to filter through on_built.
Well, that's defnitly my problem.
I only use :

Code: Select all

defines.events.on_built_entity, defines.events.on_robot_built_entity
What should I use for Spawned entities?

Current built code:
Code
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Connecting and Disconnecting Copper Wires - HELP Please

Post by TheSAguy »

Anyone know what event to use when spawning a hidden entity?

I went through all the Defines table, and don't see anything that will work.
I even tried adding "trigger_created_entity = true," to the pole, and then checking on "script.on_event(defines.events.on_trigger_created_entity, function(event)" but that did not work. (It's not a created entity, but spawned?!...)

Thanks.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Connecting and Disconnecting Copper Wires - HELP Please

Post by eradicator »

surface.create_entity does not trigger any events. And like i said before why would you even want to? Just call your cable correction function directly after create_entity. Because at that point you already have all nessecary information.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Connecting and Disconnecting Copper Wires - HELP Please

Post by TheSAguy »

Yip, I think I got it figured out, thanks!

This is my code:
Code
Post Reply

Return to “Modding help”