Electricity via Cables and Relays from Generator to Machine?

From the Asset Store
Forget about default textbox restrictions, you can create sprites atop of the textbox
  • C3 File: https://easyupload.io/vtbbr6

    What I'm trying to do:

    I'm trying to send electricity from generator via cables that can be connected to relays to extend the range and then be connected to machines that consume power.

    How I approached it:

    I need to recalculate_connections between generator, cables, relays and machines when a cable is created or a cable is destroyed and a generator is turned on/off.

    Each cable gets assigned the ID of the generator (gen_id) when created, when connected to a relay or machine it gets assigned the gen_id via cable reference. At creation of the cable on the generator (the first cable for that generator), it gets assigned cable_num of 0, when connected to a relay the relay then has cable_num of Cable.cable_num + 1, and when creating cable from relay cable gets cable_num of Relay.cable_num + 1.

    When I turn on the generator, function recalculate_connections runs to calculate which cables and relays are connected to the generator and are receiving power. It goes through the cables and relays checking for overlays and passing active = true. Then for each machine that overlaps a cable that has active = true, machine gets consuming_power = true.

    *Relays are the Component object with type instance variable = "relay" in the file.

    Problem:

    The ID assigning works, as well as passing the power until the cable of the second relay, then the cable remains active = false.

    I don't know how to solve this. If you know of a fix or a completely new system for this, I would appreciate it greatly since I'm stuck on this for months now and had tried around 15 different systems, none of which worked.

    Tagged:

  • What does active=true/false mean specifically? The recalculate connections function has me confused, pretty sure there is no need for that one. If a cable is touching a machine then it is receiving power from generator cable.genID I guess that's enough no?

  • Active true/false means that the cable/relay is connected to a generator that is turned on/off. The active doesn't change on cables/relays/machines without the recalculate_connections function, I tried that by checking overlaps every tick between cables, relays and machines. I might be making this more difficult than it actually is, I don't know, there might be an insanely simple system which could work and I'm not seeing. But, all the simple stuff I tried didn't work for me either...

  • I would set variable/boolean "hasPower" for each Cable, Relay and Generator.

    Generator would have power set 1 by default.

    If Cable overlaps Generator & Generator hasPower==1, set Cable hasPower 1.

    If Cable overlaps Relay & Cable hasPower==1 & Relay hasPower==0, set Relay hasPower 1.

    If Cable overlaps Relay & Cable hasPower==0 & Relay hasPower==1, set Cable hasPower 1.

    Things get problematic when you want to add shorts:

    If Cable overlaps Relay & Cable hasPower==1 & Relay hasPower==1, run function "ElectricShort".

    This would be set immeadetly when unpowered Cable is overlapping with powered Relay. To tackle this, power cable could be split in two + & - side and check for those instead.

    If you do not want to run this all the time, put it into function and set it for each Cable.

    But in this case the function has to run from cable starting from generator first!

    Haven't tested it, but this is how I would start making it.

  • Yes the simple solution you kind of began then went for overlap checks. It's all about data and the gen ID is all you need. If the gen is down, you find all cables that have the gen ID and they are inactive. They are linked with IDs for this purpose, no need for checking overlap of objects.

  • Yes the simple solution you kind of began then went for overlap checks. It's all about data and the gen ID is all you need. If the gen is down, you find all cables that have the gen ID and they are inactive. They are linked with IDs for this purpose, no need for checking overlap of objects.

    Brilliant.

    But what if one relay is getting power from multiple generators?

  • Yes the simple solution you kind of began then went for overlap checks. It's all about data and the gen ID is all you need. If the gen is down, you find all cables that have the gen ID and they are inactive. They are linked with IDs for this purpose, no need for checking overlap of objects.

    Why I went for overlap checks is because just powering all cables/relays depending on if generator with the id is running or not, is because if I only have data and disconnect a cable from a relay, it won't turn off all other cables but they will be active because generator is on. That's why I'm using overlaps to check for physical connections

  • Again you use the data that you made yourself, you have a route of numbered nodes. If you disconnect a cable then you set to inactive all cables that are in the same route (same gen ID) and have a higher number in the path.

  • I would set variable/boolean "hasPower" for each Cable, Relay and Generator.

    Generator would have power set 1 by default.

    If Cable overlaps Generator & Generator hasPower==1, set Cable hasPower 1.

    If Cable overlaps Relay & Cable hasPower==1 & Relay hasPower==0, set Relay hasPower 1.

    If Cable overlaps Relay & Cable hasPower==0 & Relay hasPower==1, set Cable hasPower 1.

    Things get problematic when you want to add shorts:

    If Cable overlaps Relay & Cable hasPower==1 & Relay hasPower==1, run function "ElectricShort".

    This would be set immeadetly when unpowered Cable is overlapping with powered Relay. To tackle this, power cable could be split in two + & - side and check for those instead.

    If you do not want to run this all the time, put it into function and set it for each Cable.

    But in this case the function has to run from cable starting from generator first!

    Haven't tested it, but this is how I would start making it.

    Yeah, that's pretty much the problem, if I knew how to make a cable have +/- I would've had it working with my system 2 months ago, how I have it each cable has 2 relays and each relay has 2 cables, i tried tackling the +/- with cable_num being incremented like you can see in the diagram, each in the line has a greater cable_num by one... But it doesn't work, couldn't find a different way for detecting that yet

  • Again you use the data that you made yourself, you have a route of numbered nodes. If you disconnect a cable then you set to inactive all cables that are in the same route (same gen ID) and have a higher number in the path.

    That's so simple that it's ingenious. I finally got it all working with your method.. Can't thank you @lions_ and @Radiowaves enough for all your elaborate contributions =] When I release my game on Steam I'll find a way to send you guys a key, the least I could do. This "electricity question with cables" gets posted quite a lot on the net without any apply-able answers, will finish my file, clean it up and upload it in my post as a solution for others to use. Thanks again (:

  • Great, glad it's working!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Actually, after trying to implement the second part (removing cables) this morning, it just can't work. If your global loop of setting all cables hasPower=true when connected to running Generator, then setting hasPower=false to all cables after it to hasPower=false too, does nothing as you can expect (it can work with 1 cable being disconnected, but with 2 it doesn't work, and when reconnecting when more than 2 have been disconnected causes trouble and even when turning on machines since it would give power again to all cables).

    I again had to try and do it with functions but it's just way too complicated, at least for me, if you know how to actually make a system that works, then id appreciate you doing it in a file, otherwise I'll be giving up on this since I can't take spending an hour again and have already spent too much time trying to make something work that I know I can't, it seems impossible to do...

  • When the cables and relays have the data required, the order in a sequence and linked to a generator I don't see the issue. You remove a cable, you gather its order and linked generator, then every cable with the same generator ID after it is switched off. You don't need a function, you run these actions on cable removed by whatever method.

    You don't need to use functions here imo or any overlap checks. When you create a cable, you take its start point gen ID, this could be from a generator or relay, and on the destination point, you then create the cable and assign that gen ID to the destination whether it is a relay or machine. Also taking into account the number in the sequence since that matters as you have a mechanic where you can remove cables.

  • What you are describing sounds swell, but trying to implement it gives other problems that I cannot find a way to solve;

    • Removing a cable works
    • Connecting the objects numerically initially works

    If two cables are disconnected from the line and you connect one back or turn on the generator -> it will set active all that have a bigger ID than the one reconnected, even though there's one cable missing in the line. If e.g. cable of ID 2, 5, 9, 11 are disconnected and I connect 9 back, I can't just give power to all cables with ID > 9 and frankly I haven't found a way around it, am probably too stupid.

    If one relay has 2 cables each going in a different direction, they both have the id of let's say 4 (because they are being created from relay number 3), if I disconnect the cable line left, the cable line on the right will also get disconnected even though it's on a different line and no cables are missing.

    As said, your system may work, I don't know if it does, I tried my best implementing what you said, but without any success, because while doing so a lot of other issues turn up that I can't manage to solve. Thanks for your time.

  • I thought i'd have a go at this. Can be done with a flood fill.

    dropbox.com/s/7yyocrr06epdufe/power.capx

    It starts at the source, marks it as powered, and recursively follows the connected wires to power them.

    dropbox.com/s/z2ulq22r0qup7um/power_with_id.capx

    But if you want the distance from the source of every component then the above still works for one source.

    dropbox.com/s/vto9mwobq9uoqsx/power_with_id_multi_source.capx

    For multiple sources we have to spread one layer at a time.

    sources are all set to zero. loop over the 0s and set unpowered connected to 1. Once done do the same for two and so on.

    A nice reference. If we are just powering things it's basically a flood fill. Otherwise it's a breadth first search, which isn't too different.

    redblobgames.com/pathfinding/tower-defense

    -cheers

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)