Refer to previous instance within foreach

0 favourites
  • 5 posts
  • Hello guys, I need to pin the actual instance to the previous instance on a foreach loop. For that, I created a global var and tryied this algoritm:

    Pseudo code:

    For each node instance {
        if (loopindex == 0)
       {
           old_uid = node.uid
       }
       if (loopindex > 0)
       {
            pick_instance_of_node by old_uid
           {
              
           }
       }
    }
    [/code:6xs7h4g8]
    So I have a picked instance by old_uid and need to pin it to the outer for each instance, how can I refer to that outer instance?
    
    Thanks!
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Usually a family is needed to do that. Look here for an example:

  • since your loop is "For each Node" and you want to pin that to another Node, you will need to first create a family (called NodeFamily) and put Node in it.

    Then you can have a "Node" and a "NodeFamily" selected at the same time and pin one to the other.

    so, you will end up with something like this:

    For each node instance {
        if (loopindex == 0)
       {
           old_uid = node.uid
       }
       if (loopindex > 0)
       {
            pick_instance_of_NodeFamily by old_uid
           {
              pin node to NodeFamily
              old_uid = node.uid
           }
       }
    }[/code:2g6gtpu2]
    
    I usually create instance variables (called NextNode and PreviousNode) and then save the old_uid in Node.PreviousNode and save node.uid in NodeFamily.NextNode
    That way you can randomly pick any node and know who it is pinned to, and who is pinned to it - and follow the chain if necessary.
    
    EDIT:  OH!! ROJOhound beat me again!  I need to learn to type faster!
  • Nice solution guys, thank you both!

    I was wondering about the previous versions of C2 which we did have families, any way to do that without families ?

  • Without families you can't pick two separate instances to pin them together. You'd either have to have two different object types and alternate them on the chain or do the pin manually with events.

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