Page 1 of 1

[Resolved] Connecting and Disconnecting Copper Wires

Posted: Thu May 10, 2018 7:08 pm
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.

Re: Connecting and Disconnecting Copper Wires - HELP Please

Posted: Fri May 11, 2018 2:14 am
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.

Re: Connecting and Disconnecting Copper Wires - HELP Please

Posted: Fri May 11, 2018 11:37 am
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.

Re: Connecting and Disconnecting Copper Wires - HELP Please

Posted: Fri May 11, 2018 8:09 pm
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

Re: Connecting and Disconnecting Copper Wires - HELP Please

Posted: Sat May 12, 2018 3:06 pm
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.

Re: Connecting and Disconnecting Copper Wires - HELP Please

Posted: Sun May 13, 2018 1:23 am
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.

Re: Connecting and Disconnecting Copper Wires - HELP Please

Posted: Sun May 13, 2018 1:44 am
by TheSAguy
Yip, I think I got it figured out, thanks!

This is my code:
Code