0 votes

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


in Engine by (12 points)
edited by

1 Answer

0 votes

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
# }

    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

# the event callback
    var initial_node = power_source

    # first turn on all connected nodes

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

by (72 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.