- Home /
Need opinions, or facts, about how to best go about programming this behavior. Basically Redstone.
So, in the most condensed way I can put it, here is what I need:
My player can place wires which each can be connected to eachother (with a linerenderer, visually).
Right now, each wire has its own little script containing its "are we powered" boolean and a little function to tell the other wires what their state should be - when they are placed, a "placer" script handles giving the wire, and its connections, cross-references to eachother at the moment they are placed, filling an array, they have a "refresh" function which loops through the list of connections to set their new state, and then tell them to refresh also (i prevent a loop/crash by only "refreshing" wires which needed changing)
-
-The player can use a "lever" item to give power to the wires, and to any number or connected wires arranged in any number of ways, like Redstone in minecraft
-Doing this with a lot of wires in a frame-based function causes severe framerate drop when there are a lot of wires, obviously
I understand that I need to use Coroutines to arrange the logic in a way that makes all the wires update relatively simulatenously in a way that will not slow the framerate
-
I implemented coroutines in a baseline successful way: I am using WaitForSeconds to wait just a moment before one wire updates the next wire.
-However, I am still pretty sure I am using them the wrong way. What is the best way to handle this situation? I have considered using BitArray to handle all of the wires within one script, just to give an idea of my needs here: I need the wires to update pretty instantaneously, but not so instant that it slows anything else about the game down. How to get one coroutine that handles all the wires in a way respective to their logic (i,e, "one wire can remember a connection to 6 other wires [currently doing this with an array of behaviours on each wire objects' script] and will push its current state to those wires [currently doing this with SendMessage on each individual referenced wire]")*
So far I've gotten the basic logic functional. I can place a lever, connect a wire from it to a lamp (or to another wire to another wire to a lamp) and I've also got a "signal inverter" working (like a redstone torch, it takes a signal and outputs the opposite, rather, stays on as a true state until it receives one) and the lever does successful transfer its state to its connected nodes. Just need a general idea of how to boil it down to one script / coroutine instead of one coroutine instance per wire.
Obviously you are thinking the wrong way and using "coroutine with new" express how inexperience you are with coding.
Here is how I would approach this problem. Of course you are gonna rework the entire thing.
Power Supply -- I got power. I send power to any connect to me. (have Update() function)
Station -- I got Line. I send power to anyone connect to me. (have function SendPower()). And have a List. When receive power in update --> Send power to connected Station. (If Station already powered -- do not send power anymore) (make overpowered station if you want)
Line -- Connect between 2 station.
Hey I am crossing over another wire(line). I must connect to power to anyone connect to that Line too. (Only called CheckCrossOver() when place down)
Adding connected Station to the line crossing over.