Page 1 of 1

Connecting and Disconnecting Wires

Posted: Mon Apr 02, 2018 4:17 pm
by TheSAguy
Morning,

I'm planning to add Power rail to my mod.
I'm going to have a small hidden power pole build when a rail is placed.

I need that power pole ("rail-hidden-power") to only connect to others like it or a special "Power-to-Rail-pole" pole.

I think I have the code correct below, but would lie for someone to review to confirm I'm not messing anything up.

Only connect "rail-hidden-power" to each other and "power-to-rail" poles

Code: Select all

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

Code: Select all

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

				if neighbour.name == "rail-hidden-power" then
					entity.disconnect_neighbour(neighbour)
				end
			
			end
	
		end
		
	end
Thanks.

Re: Connecting and Disconnecting Wires

Posted: Thu Apr 26, 2018 4:29 pm
by TheSAguy
Could someone please help me with a little code below.
When I built a "bi-power-to-rail-pole", I look for all "bi-rail-hidden-power-pole" in the area. If I find any, I want to FORCE a Copper wire connection between the two.
So the pole might not be a "Neighbor", since if I increase the radius, it might be outside the neighbor range, but still want to force a copper connection.

Code: Select all

	if entity.valid and entity.name == "bi-power-to-rail-pole" then
    
		local surface = entity.surface
		local force = entity.force
		local position = entity.position
		local radius = 2		
		local area = {{position.x - radius, position.y - radius}, {position.x + radius, position.y + radius}}
		local power_rail_poles = {}
		power_rail_poles = surface.find_entities_filtered{area = area, name="bi-rail-hidden-power-pole", force = game.forces.player}

		if power_rail_poles ~= nil and  #power_rail_poles >= 1 then 	
			
			for i=1, #power_rail_poles do
				NEED HELP HERE
				connect the power pole built ("bi-power-to-rail-pole") to ALL found "bi-rail-hidden-power-pole" poles
			end
		
		end
Thanks,.

Re: Connecting and Disconnecting Wires

Posted: Thu Apr 26, 2018 7:42 pm
by sticklord
I'm not sure but I think you can't force connect them if they are outside range.

Without knowing any better, I probably would increase the range of all the entity prototypes, and disconnect all when they are built and the connect them with your code there. Unless that ruins something for your mod that is :)