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

Question How to check if connected objects using line renderer make a closed loop/circulation

Discussion in '2D' started by Elmo_M, Mar 5, 2021.

  1. Elmo_M

    Elmo_M

    Joined:
    Oct 26, 2020
    Posts:
    3
    Hi,
    I have some kind of inventory grid system and would like to visually connect items together and make a closed loop/circulation. And I need the game to know that the loop is closed and valid. (Each item can only have a of maximum two lines going in/coming out. the direction of the connections are not important)

    Right now I can connect them using a line renderer. And each line/pair creates a gameobject called line1 line2.. etc

    I was thinking of storing each connected pair of two items in a seperate list, and if 2 pairs of items conjoin, then I will combine the two seperate lists into one list (+ the new pair between the two existing pairs) and so forth until it creates one big list.
    And the game needs to check if the loop is closed so I found out if I count the number of pairs in the resulted 'end' list and divide them with the amount of items in this list and if this division equals 1, then there is a closed loop. (Edit: or ofcourse checking if number of pairs == amount of items in 'end' list)

    There could be multiple closed loops inside the inventory grid.
    And each closed loop still has to know which items are in his loop.
    (And you need to connect items one at a time, so each line rendered has two points, you cant keep dragging to connect 3 items)

    Should I use a List or Dictionary for storing the pairs? or something else (array, arraylist, list tuple)?
    Is there maybe a better way to do this?
    This is my first month of unity and c#.

    I hope you could follow what I am trying to say.
    Thanks

    1.png 2.png
     
    Last edited: Mar 5, 2021