Refer to previous instance within foreach

Discussion and feedback on Construct 2

Post » Tue Jun 28, 2016 2:41 pm

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:
Code: Select all
For each node instance {
    if (loopindex == 0)
   {
       old_uid = node.uid
   }
   if (loopindex > 0)
   {
        pick_instance_of_node by old_uid
       {
         
       }
   }
}

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!
Be nice with others and with yourself!
My musics: https://soundcloud.com/vinians
My C2 Forum:
http://nmbr.forumeiros.com/
My Site:
http://www.dicasebytes.com.br
B
22
S
5
G
6
Posts: 167
Reputation: 4,949

Post » Tue Jun 28, 2016 2:54 pm

Usually a family is needed to do that. Look here for an example:
a-span-class-posthilit-rope-span-on-a-roller_p574146?#p574146
B
92
S
32
G
106
Posts: 5,272
Reputation: 69,455

Post » Tue Jun 28, 2016 3:12 pm

@Vinians

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:

Code: Select all
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
       }
   }
}



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! :)
B
95
S
33
G
20
Posts: 438
Reputation: 17,640

Post » Tue Jun 28, 2016 3:34 pm

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 ?
Be nice with others and with yourself!
My musics: https://soundcloud.com/vinians
My C2 Forum:
http://nmbr.forumeiros.com/
My Site:
http://www.dicasebytes.com.br
B
22
S
5
G
6
Posts: 167
Reputation: 4,949

Post » Tue Jun 28, 2016 3:39 pm

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.
B
92
S
32
G
106
Posts: 5,272
Reputation: 69,455


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 7 guests