Trying to make a generator(s) send power to computers via same ID

  • By ID I mean the ipAdress instance variable.

    What I am trying to do:

    For each Computer that has the same ipAddress as a Generator to get a count of how many computers have are connected to it to later split the power. After that -> every second from each Generator that has that ipAdress (multiple generators should have the ability to be connected to one Computer and then split the power -> ), remove 0.01 power / the amount of Computers found on that ipAdress.

    I've tried at least 10 different ways through multiple hours and I just don't know what to try anymore, I was mainly trying with loops and nested loops. I am generally quite bad when it comes to loops in the first place.

    Project File (Uses Advanced Text Box addon construct.net/en/forum/extending-construct-2/addons-29/plugin-advanced-textbox-59727):

    https://ufile.io/tp8iryv0

    Tagged:

  • Wulfy

    you were on the right track...

    I added a debug text box in the top left corner of the screen - click any device and its details will be listed. This comes in handy when checking how much power a generator has left...

    I started with the "Every One Second" condition first, because it isn't necessary to calculate the number of devices every tick - only when the devices actually need power.

    if the device is on and needs power, then we go looking for generators it is attached to. If they are on and have power, then we try to take it.

    if there are multiple generators, then we divide what we need between them.

    if any don't have enough to supply their portion, then we try to get it from another generator.

    if the device can't get enough power, then we turn it off.

    (if a device is off, and you click it, turn it back on. if it can't get power it goes off again).

    https://www.rieperts.com/games/forum/script2.capx

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • That is like magic compared to mine and even addresses all the issues that occur and its solutions! This is amazing. I can't thank you enough for this AllanR :D - now I even see what a good loop structure actually is and I see the solutions to the things I struggled with!

  • I am only struggling with one thing in your code. I can't find how to display the amount subtracted when two or more Computers are getting power from a Generator. It can display everything under 0.01 Normally. But if the Generator has for example two Computers connected, each getting 0.01 power it is still display -0.01 power on the Generator (the power subtraction in Debug subtracts 0.02 normally, I also made the debug update itself every tick if a device is selected - for a better overview). Could you maybe take a look? It's on line 38 and I've tried all ways I could think of and I feel like they should work but they don't.

    Project File: ufile.io/0o1h6rns

  • Wulfy

    ok, since there could be any number of generators, and any number of devices, and generators could run out of power at any time, you can't display the total power used until the end of the one second cycle.

    so, I added a new variable to the generators called PowerLoad. At the start of every cycle that is set to zero for every generator.

    then as power is assigned to each device, the amount deducted from each generator is added to its PowerLoad variable. At the end of the cycle it loops through all generators that had some power used and displays the total then...

    https://www.rieperts.com/games/forum/script.capx

  • Okay now I understand why it didn't work. Thank you for updating the project, the PowerLoad way is also very neat AllanR!

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