- Home /
I have chains of connectable objects which reference eachother. Need them to pass a simple boolean to eachother! How?
So, basically I have a system where my character can place wire. A little cube gets instantiated, and a new one starts following the mouse, with a linerenderer running the visual "wire". At this moment the cubes get references to eachother. Then the second cube can get placed down, to either run another length of wire or to end the connection at a powerable object, right now I have a "lamp".
We connect a lever to the first cube which triggers a function, "If the cubes we are connected to are not the same state as I am, make them the same state. and then trigger this same function on that cube's script"
This was working well enough logic-wise but once I got further into the system, I realized doing all of this in frame-based loops was inefficient and killed my FPS.
So I started trying to convert my functions into coroutines, but I can't seem to figure it out! I've gotten debug logs to show they are happening but the resulting behavior isn't reflecting it.
I left in some commented code to show my trial-and-errors here.
public Coroutine Flip(){
//state = !state;
//StartCoroutine(PushState);//PushState ();
print("Flipping");
StartCoroutine ("PushState");
return null;
//yield return null;
}
//public IEnumerator Flip() {
//for(;;) {
// print ("Pushing state");
// PushState();
// yield return new WaitForSeconds(.05f);
// }
//}
WireNode node;
public IEnumerator PushState(){
print ("Pushing");
foreach (Behaviour conn in ports) {
if (conn != null) {
if (conn.name.Contains ("WireNode")) {
node = conn as WireNode;
if (node.state != state) {
node.state = state;
node.PushState ();
}
foreach (LineRenderer cable in node.wirelines) {
if (node.state == true)
cable.materials [0].color = onColor;
if (node.state == false)
cable.materials [0].color = offColor;
}
}
if (conn.name.Contains ("Powerable")) {
Powerable p = conn as Powerable;
p.powered = state;
p.StartRefresh ();
}
} else {
//yield return null;
}
}
//}
yield return null;
}`
TL;DR: I need each wire to tell its own list of other connected wires what to do / what state they are in (boolean).
I plan to allow for a huge number of wires taking place at once, so I am hoping to condense the behaviour into one that is not framerate dependant, will not bog down the framerate
I only need this message / state to be changed once at a time. I.e., all of the wires are simply in one state, and not doing anything, until they are told to refresh, which is one action that pushes their state to other wires.
Like Redstone.
Answer by PanzerPotato · Jun 17, 2018 at 06:21 PM
Take a look at this. You can try using yourOtherObject.SendMessage ()
to tell the next to do what ever it is you need to do.
I actually am using Send$$anonymous$$essage as my "powerable" script's way of making machines do things.
You're saying, have the lever Send$$anonymous$$essage the wire to Send$$anonymous$$essage the next wire, etc etc?
I like the idea, but my question / concern is, Wouldn't every single message still happen in a single frame?
So just to be clear, are you trying to have one segment of wire power on at a time?
No, the player can place these nodes freely. Each node has a script with a list of 6 "slots" (behaviours) which is how each node knows what other nodes, or machines, it is connected to. So when a node is changed (say by a lever), it "pushes" its state to the nodes it is attached to, and tells them to do the same. I prevent a loop by only changing states which are not already the same.
If a node is attached directly to an activated source of power, the nodes state is forced to stay on, and continue its update chain to push its state to its connected wires.
I changed, in all scripts with a "refresh" or "push state" function, the "Invokes" with "Send$$anonymous$$essage". I believe I did get a performance boost, but I still get the feeling I'm misunderstanding the way to apply co routines here - my goal is, I want the computer to handle as many wire updates as it can without affecting the framerate. Framerate is priority primero.
Your answer
Follow this Question
Related Questions
[HELP] How to make camera smooth change position while follow object? 1 Answer
How do couroutines handle If Statements within a for loop? 1 Answer
Rotating cube steadily around world axis not working properly 0 Answers
Can you run a coroutine more often than Update? 1 Answer
1 out of 4 objects with the same script that reference the same transform list returning null? 1 Answer