Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Coroutines and collections of sorted objects

Discussion in 'Editor & General Support' started by Dahlvash, Jun 30, 2014.

  1. Dahlvash

    Dahlvash

    Joined:
    Feb 9, 2014
    Posts:
    54
    Hi there!

    I'm trying to get into the habit of using coroutines instead of relying on the Update method.
    I have (semi) built an on-line card fame but have got the point where I feel that I should rectify my reliance on Update() before continuing.

    Currently, Each player has a 'Hand' that contains a list of cards (arranged in a fan pattern) and a 'PlayedCardsArea' that has a list of that players cards that have been played.
    At the moment each card in the hand is sorted into the frame arrangement every frame update by calculating each cards position (in the fan arrangement) based on how many cards are in-hand, the offset from a central point to fan-around and a 'padding' of how large the angle between each card should be.
    Similarly with the cards in play but without the fan arrangement and just a side by side self centring arrangement.

    What makes this a little more complex, is that I also have a script attached to each card that handles any interaction from the user. Namely, dragging around the screen (using a 'Dragging' boolean when OnMouseDown is invoked and setting to false when OnMouseUp is invoked; Overriding the sorting done by 'Hand' or 'CardsInPlay'.

    All sorting is done with either Slerp, Lerp or moveto.

    What I'd like to do is only move a card into its place when it is out of place (a coroutine would be ideal) but I cant get my head around how I would arrange such a system given that there can be multiple systems fighting to place the card at one time (Hand/CardsInPlay vs user dragging). But also allow the other cards to be lerped/slerped etc while another is being dragged.
    To me with my current (limited) knowledge of how Unity coroutines work, there would be conflicts of startCoroutine and stopCorouting stopping and starting all cards at once when some cards may need to stop but other may not.

    Any help on the matter would be greatly appreciated!
     
  2. MakeCodeNow

    MakeCodeNow

    Joined:
    Feb 14, 2014
    Posts:
    1,246
    I'd recommend you look into implementing some sort of finite state machine (FSM) for each card, as it's a good way to represent systems like this. Allow each state to have a currently active coroutine, and terminate that coroutine when the state changes/ends.
     
  3. Dahlvash

    Dahlvash

    Joined:
    Feb 9, 2014
    Posts:
    54
    That sounds like a natural progression from the current boolean Dragging I mentioned. Will definitely look into this, thanks!