+1 vote

So, I'm developing a game that uses blocks as a power source for weapons attached to them. the blocks can only be placed next to another block or the main power source. This creates a circuit. I've got them able to read the blocks next to it when placed based on the onBodyenter function whenever the block is placed. This will then turn the InCircuit boolean to true and, based off the numbers of the blocks around it, assign itself the proper number in the circuit

However, I need this code to be able to update itself. As in knowing when something next to it has changed.
So if a block down the circuit connecting it to the power source is destroyed, cutting if off from the power source, every block that isn't connected to the source any more needs the InCircuit boolean set to false so the weapons no longer function.
It also needs to change back to true if the block is replaced.
I've heard of basic cell automata programming but haven't really understood how the logic works.
I looked up the code to the minecraft redstone circuit but without commentary or context, it didn't really make much sense to me since I don't know java very much...

Is anyone able to help me out with this or point me in the right direction with how i can accomplish this using Godots resources?!
this is my current code based on collision

in Engine by (13 points)

1 Answer

0 votes

Once the block connected to the power source is destroyed, emit a signal, block_destroyed, which the blocks beside it should be connected to.

After that, the blocks beside it can emit power_disconnected, which can then trigger the function to set InCircuit Boolean to false.

by (202 points)

Well, after posting this, I've been able to get the blocks to assign themselves a number in the circuit by knowing how many blocks away from the power source and update it constantly using a timer and putting getcollidingbodies() I to an array, which is then checked to see if it is a weapon, block, or other. Circuit piece number is done by assigning itself the number of the lowest numbered block above, below, left or right of it and adding 1 to it.
The issue is that with signals, I won't be able to tell which block it's coming from unless I assign that number, but each block with the same number could emit the same signal. And I need to be able to have the blocks know if it really was disconnected from the circuit, because 1 block below it could be destroyed, but the block next to it still connects it to the power source. Or on the other side, the block next to it destroyed and the line of blocks on the other side aren't connected. The block immediately disconnected needs to know how to tell the difference.
So far, I've only been successful about checking if any blocks or power source are next to it at all. If not, InCircuit = false.

The issue is that with signals, I won't be able to tell which block it's coming from unless I assign that number, but each block with the same number could emit the same signal.

Right, you can send using signals, the name of the signal's sending node.

emit("power_disconnected" self.name)

And I need to be able to have the blocks know if it really was disconnected from the circuit, because 1 block below it could be destroyed, but the block next to it still connects it to the power source.

Each side of a block can list whether it's connected to a power source (via powered block)

power_disconnected(block_name):
    remove_side(block_name) #removes the side without power
    var in_power = check_sides() #checks other sides if they have power
    if not in_power: 
        InCircuit = false
        emit_signal("power_disconnected")
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.