Connecting and Disconnecting Wires

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

Connecting and Disconnecting Wires

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

TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Connecting and Disconnecting Wires

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

sticklord
Burner Inserter
Burner Inserter
Posts: 12
Joined: Sat Apr 14, 2018 4:13 pm
Contact:

Re: Connecting and Disconnecting Wires

Post 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 :)

Post Reply

Return to “Modding help”