Problems setting an instance variable

Get help using Construct 2

Post » Wed Mar 14, 2012 8:10 pm

On shift pressed
For each Object
   Object is overlapping Object2
      If Object variable = "Follow"
        Set Object variable = "Stay"
      If Object variable = "Stay"
        Set Object variable = "Follow"


Why is this not working for me? It seems to set the varaible to "Follow" regardless of the previous value. :/
B
4
Posts: 46
Reputation: 617

Post » Wed Mar 14, 2012 8:31 pm

You have to remember the code is executed from top to bottom.

In your case, when the var = "Follow", it is set to "Stay".
And right after, it testes if the var = "Stay" (which is true since we just changed it) and so it sets it to "Follow", but the testes are done and the code executes for another object.

By the way, you don't need the "For each object" in the top level event, this is implicit in C2, the conditions are tested for all instances of the object, and the sub-events will use the picked instances from the first event.

What you would need here is rather :

On shift Pressed
Object is overlapping Object2
..Local variable Temp = 0
..If Object variable = "Follow"
..If Temp = 0
..==>Set Temp to 1
..==>Set Object variable to "Stay"

..If Object variable = "Stay"
..If Temp = 0
..==>Set Temp to 1
..==>Set Object variable to "Follow"

Like in this capx.
You can see the actions are only applied to the instances picked in the top level event.
When you check for overlaping, it only picks the instances overlaping.

That's how events work.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,999
Reputation: 57,793

Post » Wed Mar 14, 2012 8:37 pm

Yea. I can see now what was going wrong.
That's the way I suspected I would have to do it.

Is there no way to break out of an event? instead of using temporary variables like this? :/

B
4
Posts: 46
Reputation: 617


Return to How do I....?

Who is online

Users browsing this forum: MarkThomas, theosciandra and 19 guests