- Home /
Replicating MC Redstone Wiring
I'm trying to design a wiring system that allows players to connect power sources to switches via wires. Think redstone in Minecraft. When a wire is placed, if it is not initially connected to the power source, it has no power (behaving as expected). The PROBLEM occurs when the wire is connected to power, detecting that it is powered, and then disconnects from power. After it has touched the power source, it then becomes it's own source if disconnected, and never de-powers.
This is what SHOULD be happening
Here's what is ACTUALLY happening.
And here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Wire_Moveable : MonoBehaviour {
public static Wire_Moveable control;
public bool IsBlue;
public bool IsRed;
public bool powered;
void Start () {
IsBlue = false;
IsRed = false;
}
void Update () {
if (Input.GetMouseButtonUp (0)) {
following = false;
this.gameObject.GetComponent<Wire_Moveable>().SnapWire ();
}
}
void OnTriggerStay2D (Collider2D trigger) {
if (trigger.tag == "wire") {
if (trigger.gameObject.GetComponent<Wire_Moveable>().powered) {
powered = true;
}
if (trigger.gameObject.GetComponent<Wire_Moveable> ().IsBlue) {
IsBlue = true;
IsRed = false;
} else if (trigger.gameObject.GetComponent<Wire_Moveable> ().IsRed){
IsBlue = false;
IsRed = true;
}
}
}
void OnTriggerExit2D (Collider2D trigger) {
if (trigger.tag == "startnode_blue") {
IsBlue = false;
powered = false;
}
if (trigger.tag == "wire") {
if (trigger.gameObject.GetComponent<wire_Moveable>().powered) {
powered = false;
}
}
}
void SnapWire () {
if (this.gameObject.name == "Wire_Basic" || this.gameObject.name == "Wire_Basic(Clone)") {
StartCoroutine ("ActivatePower", 0);
}
}
IEnumerator ActivatePower () {
yield return new WaitForSeconds (0.1f);
if (powered) {
PowerBeam.SetActive (true);
} else if (!powered) {
PowerBeam.SetActive (false);
}
}
Answer by TreyH · Apr 19, 2018 at 07:00 PM
As a simple, brutish solution, can you just have movement of any block force all components to lose their powered status -- then start with the power source and slowly walk back through everything it's touching and reassign power / colors?
This actually proved to be the simplest, most effective method. $$anonymous$$y game is very conservation on runtime resources, so this method does not put any strain on gameplay.
There was initially a flickering issue with the animation resetting, but that was fixed with a condition IEnumerator and a 0.1f delay.
If you post this as an answer ins$$anonymous$$d of a comment, I'll accept it as the correct solution.
Answer by DigiTranceG · Apr 18, 2018 at 03:26 PM
Maybe it has to do with the Singleton you are calling in the script?
public static Wire_Moveable control;
Answer by Bunny83 · Apr 18, 2018 at 04:34 PM
Well, it's not clear when you said "Replicating MC Redstone Wiring" what you actually mean. MC redstone wireing has a loss of energy each block it travels. This naturally let would let the energy "dry out" when any update happens to one block. Further more redstone updates due to minecraft's "block update". So when anything changes (like placing or removing a block) the neighbors get an update and should re-evaluate their state. Unlike most other mechanics in MC redstone does an actual search / floodfill through connected pieces. So when an update happens it would search for an actual energy source up to 15 blocks. If no source is found it will be disabled.
This can get quite expensive quite fast especially if you want to support longer chains than 15. Other mods which provide a wireing system (with no loss) usually build an actual network. Here the actual properation of a signal through the network doesn't matter. If a network has a source somewhere the whole network is triggered. The active attached sources may be registrated in a network so once all sources are gone the network will be off. Each time a block is placed or destroyed that is adjacent to an existing network / part of a network, the whole network will be re-traced again to determine which pieces are connected. Destroying a piece could split a single network into up to 6 new networks (In MC each block has max 6 neighbors).
Using triggers may work but if your game is actually grid based it would be way easier to actually check the grid neighbors. We have almost no idea of how your implementation looks like, how a user is able to place / destroy pieces. What kind of collider an piece has. If one piece can be connected to more than two other pieces. Where the power / energy comes from. ...
And now i'm busy since I'm currently breeding a redstone chicken in SkyFactory3 as well as prepearing real chicken legs for dinner ^^
To clarify things a bit then. I'm really wanting a directional energy flow that can be cut off. There is a defined source, and the energy can travel freely when connected to the source, but if one of the wires is removed, then all wires AFTER the break lose power.
Currently they don't do that. Each wire can successfully be powered after connecting to the source, and there is a check in the code to detect if they have lost connection with the power, which sort of works. The problem is that when two non-powered wires become powered, then one of them becomes a source and cannot be de-powered.
The wires are on a grid. Here is a better illustration of what is happening.
Desired Behavior:
Actual Behavior:
Your answer
Follow this Question
Related Questions
My Algorithm for checking whole tiles didint work 0 Answers
How to stop a calling function 1 Answer
KEEP INSTANTIATE PREFAB ONMOUSEDOWN 1 Answer