# help with logic for wire / cable connections

hello to all, i wanted to do some kind of wire or connections that the user can layout and build.

but i have no idea on what or how the logic on this is.

basically, the player has this box actor, let’s say this is a generator that generates electricity.
then another box actor that requires electricity.

the player can layout the wires to connect them both.
note: the game is in topdown grid style

so to connect both items, the player must place a few conduits that connect to each other.

now the question is, how can i let the game or have a blueprint logic that lets it know if it has an ‘working’ electric connection

my current one is just a blueprint actor that checks above, below , left and right of the conduit of there’s another conduit.
then stores it in a variable, then when an object checks for electric supply, it asks the connected conduit to ask the next conduit if it is connected to the main power supply… looping all conduits.

but this works on a direct connection… having a hard time to make it work on a split connection…

any other idea how to proceed? or better ideas?

another idea i have got is, a conduit has a special nav modifier, that a special invisible unit can only traverse, this way, for a electricity connection check, all i have to do is spawn the invisible actor, with high speed, then navcheck if it can reach any actor that produces electricity… using pathfinding…

still, im looking for better and faster ways… just like any other building games…
like prison architect, or rimworld, or simcity or cities skylines…

really need help on this…

got a bit of it working, all was well until i crash for infinite loop due to blueprint max iteration…

all i did was a trigger that triggers each conduit that triggers the conduits next to it and so on so forth…
it seems to be working alright for a linear connection, but the crash happen when there are a lot of multi linked conduits…

had 3 different type of experiments, all leads to infinite loop, now im out of ideas…

attached image shows the result…
every thing is fine, until there are about 170+ multi link conduits (left side)
then it causes the infinite loop

but with linear connection, without too much multi linked conduits, i could reach 500+ conduits…

how did other games make it?.. the wire connections…
having the wire know it is connected… or being supplied with power…

I’d also like to know more about the methodology for creating these kinds of propagation systems.
I’m also trying to do this electricity thing, but also something like oxygen inside a room, which would deplete from actors and be refilled by some system. Like FTL has.

Heck, even just knowing a good term for this kind of thing to google for would help heaps.

This would be a type of flood fill algorithm. The problem OP is having is that a branch off or split is causing too many “cells” to try to run the calculation at the same time. What I would do is create a global array that acts as a queue. Any cell that needs to be updated would be added to this queue, and you would loop through this queue, updating and adding as you go.

I’ve managed to get a basic propagation system going, but the question is how to do it as effectively as possible.

When It comes to electrical wires, they should follow some rules -

1. if a wire receive power, it should propagate that to adjacent cells.
2. if a wire lost power, it should propagate that to adjacent cells.
Of course, if a wire receives a state that it already is in, it should drop it so that powered cells does not tell powered cells to propagate.

So having just a on /off state does not seem to work. At least not when you introduce a generator which has the following rules:

1. on begin play, propagate power.
2. if it receives a “lost power” state, drop it and propagate power.

Meaning that all cables are powered up. But then if one is told to loose power, what happens is that it propagates lost power down to the generator, which then propagates power back until they are all powered…
of course, the wires are not “finished”, they have only sent a message to the first member of a loop. So now they will continue their individual executions - “power propagation” will be dropped since all are powered, lost power will be propagated again - to the generator which propagates power again… etc
It may not be an infinite loop but it’s a big enough loop to not work.
Interestingly, using this method and cutting power to a “line”, works, but in a closed circle causes this issue.

I had to introduce a third state, protected, which means that a cable got power from the generator and can not lose power. Problem here is that, how would it lose the protected state? I ended up setting it to “power state” using a short timer.

While I have it working, I’m not sure if it’s a “simple and effective” setup.

Ive never looked into this really before, but wouldnt it be better to set it up so that each time the connection network is changed in anyway (i.e. the player adds a block or removes one or soemething) Turn off all blocks immediately, then resend the power information down the conduit line again, from the generator (or each generator). Like switching on each conduit as power goes down the network?

EDIT: I mean doing it the way you guys are trying surely means every cell has to update every frame, for every direction? or everytime the change event is fired

You shouldn’t have to power off all blocks immediately, but your idea is the best one I’ve seen on this thread: recheck everything connected to the network anytime that network is changed. If for example, you had two separate groups of generators+wires, and you cut one wire, you would want to recheck everything that wire was connected to, but the separate network should not be checked.

That’s a good point, turning everything off and just propagate from the source would work well for a electricity-like system.
If there is only ever one source, you could propagate that source as data, storing a reference inside the source to every network member as well as every member storing the source. When a change happens in a member, inform the source, source tells members to turn off and starts the propagation.

Building on that idea of the source holding references to each cell Ste1nar, if they are added to an array by distance from the source if one is turned off you could simply check each array member after the one is changed. Although I doubt the with the complexity of this sstem you would need to do too much optimization.

(For bonus points you could turn everything off and re propogate from source, but keep the visual representation of the cell being switched on until the propogation is complete. So to the player it does not look like the whole network looses power for a second when they change it)

And samnater the seperate network would not be checked, as there is no connection to it from the main network so its state change event would never be fired