Hey guys, what’s up !?
I’ve a question, but I don’t believe that this should be categorized as a script question. And I can’t find a better place do ask. So…
I’m trying to create a object to my game, which has a need for energy (or water), and to provide this, I need to connect this object to a energy supplier. Ok, I’m already doing that. But I really don’t know the better way to transfer this energy. I mean, how can I do this in a good way, where my object will only works, if any energy is been supplied ?
To be situated. Think about prison architect, or minecraft with the industrial mod. In both games, you need to place some cables to transfer electricity between 2 points. And you can add another cable making a cross, to split this electricity to another point.
My first approach was to have a script attached to any cable, which would hold a certain amount of energy. So everytime I place a new cable, I call a event to all cables checks adjacents cables, and store them in a list.
Those cables has a coroutine to check if they have any energy inside, and if don’t, they do a loop over all other adjacent cables, to “ask” for energy.
The energy supplier has a script to “provide” energy, every time “someone” ask. And the object which will consume this energy has another script, to only ask this energy.
The problems I had with this solution, it’s that I’m “consuming” energy without a object to use that, because all cable “ask” for energy, and this energy stay there without being used. And of course, if I lay down a lot of cables, at some point, the supplier will be exhausted without anyone use this energy. And of course, I’ve a lot of coroutines running in background, which I didn’t checked, but I believe that could be very processing consuming, right ?
I was thinking to have a “parent” gameobject to hold energy lines. So, everytime I lay down a cable, I could check if this cable is adjacent to any other cable which is already a member of one energy line. If true, I make this cable a member of this energy line, if false, I would create a new energy line, for this cable. Then, when one consumer needs energy, I could simple check in the energy line, if is there any provider, and ask for energy.
The problem with this approach, is that each provider can only be a member of one energy line. So, I cannot have multiple energy lines connected to a single provider. And if I remove one cable in the middle, I would need to do a check to all other cables, to see if they are still a member of one energy line, or if they should create a new energy line to be member.
I was looking into the minecraft mod approach, where they have a generator which can be linked to at least 6 cables, one at each side of the block. The cables can admit different voltages, and have a “bandwich limit” per model. And if I don’t have a machine to use this energy, the generator do not consume any energy. So, I really don’t know how they did that.
Can anyone here, please, give me one insight about this situation ? Maybe a better approach.