Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Connection Script

Discussion in 'Scripting' started by Tayfe, May 29, 2014.

  1. Tayfe

    Tayfe

    Joined:
    Nov 21, 2011
    Posts:
    103
    Hey guys,

    I'm trying to create a Script that checks which Objects (powerpoles) are connected to my main "energy" object. So it's a power supply line.

    Im trying to do it be starting at my energy-object. It checks for any power power in a specific radius and turns them on by adding energy. Every power pole found around the energy object should now check itself again for the next power poles in the specific radius and so on. So at the end every power pole connected to the energy object through the other power poles should be turned on with energy.

    This is my script:

    Code (csharp):
    1. #pragma strict
    2. var energy : int;
    3. var maxRange = 20;
    4.  
    5. var updateConnections = 0;
    6.  
    7. function Start ()
    8.     {
    9.  
    10.     }
    11.  
    12. function Update ()
    13.     {
    14.     if(updateConnections == 0)
    15.         {
    16.         UpdateConnections(gameObject);
    17.         }
    18.     }
    19.    
    20. function UpdateConnections (gameObjectToCheck : GameObject)
    21.     {
    22.     var powerPoles = GameObject.FindGameObjectsWithTag("PowerPole");
    23.  
    24.     for(var specificPowerPole : GameObject in powerPoles)
    25.         {
    26.         //Don't check the gameObject itself again
    27.         if(specificPowerPole.transform.position != gameObjectToCheck.transform.position)
    28.             {
    29.             if(Vector3.Distance(gameObjectToCheck.transform.position, specificPowerPole.transform.position) <= maxRange)
    30.                 {
    31.                 specificPowerPole.GetComponent(powerPole).energy = energy;
    32.                 UpdateConnections(specificPowerPole);
    33.                 }
    34.             }
    35.         }
    36.     updateConnections = 1;
    37.     yield WaitForSeconds(3);
    38.     updateConnections = 0;
    39.     }
    This is the error message I recieve:

    I don't know how to solve this problem. Its a recursion and it has a stop-clause so I dont know why it doesnt work. Have you got any idea to solve this problem?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you're trying to do the whole chain from the powerplant script?


    would it not make more sense to have the powerplant script call a function attached to the powerpoles within range. Those poles then handle their own "find poles within range, call their functions if they're not already on" and so on.

    Component object model = have each bit do it's thing, don't try to centralise the code onto one object...
     
  3. Tayfe

    Tayfe

    Joined:
    Nov 21, 2011
    Posts:
    103
    I had a different script before but there was a problem:

    Every pole had attached a script that checked for poles or a power house within the range and checked if they had energy. If there was one they turned on themself, too. It worked fine but it had a logically problem: When i had for example 10 poles in a row and turned on the enery house everything worked well so far. But when i removed for example the 4th pole, pole 5 to 10 still had energy because the poles next to them had energy as well.

    By making the script central gaps should be noticed so that the problem I just described should not exist any longer.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ok, so we're probably looking at having two lists, "incoming" and "outgoing". If a node isn't an "energyProvider" (bool, set to true on the powerstation) and the "incoming" is empty that node shuts down and sends a message to all the "outgoing" to remove it from their "incoming" lists before clearing it's "outgoing" list.


    edit: avoiding the feedback would be the tricky bit though... you'd need to refresh the entire network (turn it all off, trigger the update from powerstations) or something... hmmm

    or you a pylon can only exist in the incoming or the outgoing list not both, but then you'll need to update the entire list when a link is broken since some of the paths would have been excluded and may be viable when a node is removed...
     
    Last edited: May 29, 2014
  5. Tayfe

    Tayfe

    Joined:
    Nov 21, 2011
    Posts:
    103
    Thank you for your help, but i wanted to keep my idea because i was sure that it has to work. And after some additionall research I finally solved the problem. I had to add " : IEnumerator" behind the function to make it work. But thank you anyway!

    If someone is interested here is my final script (I did some additional changes):

    Code (csharp):
    1. #pragma strict
    2. var energy : int;
    3. var maxRange = 20;
    4.  
    5. var updateConnections = 0;
    6.  
    7. function Start ()
    8.     {
    9.  
    10.     }
    11.  
    12. function Update ()
    13.     {
    14.     if(updateConnections == 0)
    15.         {
    16.         ResetConnections();
    17.         UpdateConnections(gameObject);
    18.         }
    19.     }
    20.  
    21. function ResetConnections()
    22.     {
    23.     var powerPoles = GameObject.FindGameObjectsWithTag("PowerPole");
    24.        
    25.     for(var specificPowerPole : GameObject in powerPoles)
    26.         {
    27.         if(specificPowerPole.GetComponent(powerPole).nextPowerHouse == gameObject)
    28.             {
    29.             specificPowerPole.GetComponent(powerPole).nextPowerHouse = null;
    30.             }
    31.         }
    32.     }
    33.            
    34. function UpdateConnections (gameObjectToCheck : GameObject) : IEnumerator
    35.     {
    36.     var powerPoles = GameObject.FindGameObjectsWithTag("PowerPole");
    37.        
    38.     for(var specificPowerPole : GameObject in powerPoles)
    39.         {
    40.         //Don't check the gameObject itself again
    41.         if(specificPowerPole.transform.position != gameObjectToCheck.transform.position  specificPowerPole.GetComponent(powerPole).nextPowerHouse == null)
    42.             {
    43.             if(Vector3.Distance(gameObjectToCheck.transform.position, specificPowerPole.transform.position) <= maxRange)
    44.                 {
    45.                 specificPowerPole.GetComponent(powerPole).nextPowerHouse = gameObject;
    46.                 UpdateConnections(specificPowerPole);
    47.                 }
    48.             }
    49.         }
    50.     updateConnections = 1;
    51.     yield WaitForSeconds(3);
    52.     updateConnections = 0;
    53.     }