How to make electric current in godot?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By breaker_ground

Hello, iam newly godot user and newly in the game dev.
please somebody help how to make electrical circuit with Godot?
it will change color when sprite connected the source power (red triangle) simply like electricity. the picture below explain that every wire connected with source power will changing. Thank you …

:bust_in_silhouette: Reply From: LemonSqueezy

Hi, as a disclaimer I’m pretty new to godot and game dev too, so I don’t know if my idea is the best way to solve your problem. After that say, this is how I would aproach it:

First to set up some terminology:

  • red triangle = power source
  • green lines = cables ( green when connected, grey when they are disconnected from the power source
  • blue circles = light bulbs (filled circle if connected, circle outline if disconnected)

Your visual representation of the cables (the drawing) needs to have a underlying data structure for you to be able to “check” if a cable/light bulb is connected.

Explanatory Image

Every piece of cable needs to know what it is connected with. In this example A is connected with B and C on the one end. And connected to the power source on the other end. C is connected with a light bulb.

So the algorithm would work like this: Every time the user changes the circuit, you would take your power source node and get the cables it is connected with (A). So A has to be green. Then you repeat that for every cable that is connected with A. A is connected to B and C. So B and C are green because A is green. Then you iterate over B and C. B is not connected to anything so you stop there. C is connected with a light bulb so the light bulb is ON.

I will call the power source, the cables and light bulbs nodes.

You would need a recursive algorithm. Something like this:
(this is pseudo code btw.)

# this is an array that holds
# all the nodes in the circuit.
var circuit_nodes = [nodeA, nodeB, nodeC, lightBulbA, lightBulbB, ...]

# sample data structure
#
# var nodeA = {
#     connected_nodes: [nodeB, nodeC],
#     is_on: false,
#     was_checked: false
# }


turn_on_conneced_nodes(node):
    for connected_node in node.connected_nodes:
        connected_node.is_on = true
        connected_node.was_checked = true

        # this is the recursive part
        # where you call the same function again
        # on the connected node
        turn_on_conneced_nodes(connected_node)


# the event callback
_on_player_changed_circuit():
    var initial_node = power_source

    # first turn on all connected nodes
    turn_on_conneced_nodes(initial_node)

    # then turn off all nodes that
    # are not connected to the power source
    for node in circuit_nodes:
        if ! node.was_checked:
            node.is_on = false

        node.was_checked = false # reset

There is probably a lot more to talk about but maybe this is a start and a hint in the right direction.