How do I make "FOR" loop subevents trigger in order? SOLVED

Get help using Construct 2

Post » Sat Sep 23, 2017 12:29 pm

Image

Basically, here's the code. If I have 1+ enemycores (works fine with just 1), it starts assigning random values to random enemycores (probably because all 3 subevents trigger simultaneously).

Please help me figure out how do I make events in Construct loops trigger in order; or at least how do I trigger something each time for goes full circle.

Thanks :geek:
Last edited by HellMax on Sun Sep 24, 2017 10:42 am, edited 1 time in total.
B
6
S
3
Posts: 122
Reputation: 1,632

Post » Sat Sep 23, 2017 3:22 pm

No, subevents are not triggered simultaneously, they are triggered in order.
You are doing something wrong, but it's hard to say what from just this screenshot.

So lets say enemycount=5, then you repeat this loop 5 times and update 5 enemycore instances (which IDs are 0,1,2,3,4).
Is this correct?

Are you sure you need to reset the global variable buildup_threat_lvl to 0 on every loop pass?
Since you are setting it to 0, for enemycores with ID=1,2,3,4 the threat_lvl will be either 0 or 20 or 19 or -1 (depending on Line of sight conditions).


I suggest you create a text field on your layout and use it for debugging.
For example:
DebugText append text "i=" & i
DebugText append text " enemycores.picked=" & enemycores.PickedCount
DebugText append text " buildup_threat_lvl=" & buildup_threat_lvl
Add lines like this in several key points of your loop and you'll get much better understanding what's going on.


By the way, why are you not using For loop? It will automatically increment "i" for you.
Code: Select all
For "i" from 0 to enemycount
  ....
  Front.ID=loopindex
  ...
  Back.ID=loopindex
  ...
  enemycore.ID=loopindex
Image

Did I help to fix a problem in your game? You can buy me a cup of coffee :)
B
17
S
9
G
71
Posts: 1,063
Reputation: 38,689

Post » Sat Sep 23, 2017 7:42 pm

Thanks, will switch to for (i just felt more familiar with repeat setup)
Will do some debugging just now.

And yepp, that is correct, 0/20/19/-1 is exactly what I want them to be.
B
6
S
3
Posts: 122
Reputation: 1,632

Post » Sat Sep 23, 2017 8:02 pm

Image
Mk.II suffers from the same problem - values being assigned to wrong instances.
B
6
S
3
Posts: 122
Reputation: 1,632

Post » Sun Sep 24, 2017 2:52 am

I didn't mean that For loop will fix this issue, it just makes the code easier to read.
Could you provide an example of "wrong instances"?

Your code looks ok, except for the part where you reset buildup_threat_lvl only starting from ID=1.
So for ID=0 this variable could contain any value assigned to it somewhere else in your code. But hey, maybe this is what you need. If not, move this action to the beginning of the loop.

Have you tried debugging? I would do something like this:
Image

If this doesn't help, could you share the CAPX? You can remove unrelated stuff from it.
Image

Did I help to fix a problem in your game? You can buy me a cup of coffee :)
B
17
S
9
G
71
Posts: 1,063
Reputation: 38,689

Post » Sun Sep 24, 2017 8:38 am

Debugging made clear the fact that It always only assignes value to second instance (the one with i=1) and NEVER to i=0 (threat_lvl always 0). I'll keep on looking.

UPD : further testing showed that even if you add 4 instances ---> the only one which gets the right threat lvl is #1. Very odd. I'll package the .capx in a bit.
B
6
S
3
Posts: 122
Reputation: 1,632

Post » Sun Sep 24, 2017 9:03 am

capx : https://goo.gl/KjZijp

*instance standing with front (red part) towards the player is supposed to have threat of +25; while the one standing with the back to the player is -25
B
6
S
3
Posts: 122
Reputation: 1,632

Post » Sun Sep 24, 2017 10:25 am

You need to move all your off-screen sprites to another layout. I believe they were causing most of the problems.
As you know, in order to spawn objects Construct 2 needs at least 1 "default" instance of each object to be added to one of the layouts. But it should not necessarily be your main game layout. Create a blank layout, call it "Unused" and move all off-screen sprites there.

I also made some changes to your code, see my comments in the event sheet.
I added drag&drop behavior to enemies, try dragging them around, see if everything is working as it should.
https://www.dropbox.com/s/cl5907ze1k2af ... .capx?dl=0
Image

Did I help to fix a problem in your game? You can buy me a cup of coffee :)
B
17
S
9
G
71
Posts: 1,063
Reputation: 38,689

Post » Sun Sep 24, 2017 10:40 am

YAAAAY GOT FIXED
The problem was that x2 fronts and x2 backs were assigned id=0 due to my spawn system malfunctioning.
Sorry to bother, great thanks for the help :P
B
6
S
3
Posts: 122
Reputation: 1,632

Post » Sun Sep 24, 2017 10:41 am

dop2000 wrote:You need to move all your off-screen sprites to another layout. I believe they were causing most of the problems.
As you know, in order to spawn objects Construct 2 needs at least 1 "default" instance of each object to be added to one of the layouts. But it should not necessarily be your main game layout. Create a blank layout, call it "Unused" and move all off-screen sprites there.

I also made some changes to your code, see my comments in the event sheet.
I added drag&drop behavior to enemies, try dragging them around, see if everything is working as it should.
https://www.dropbox.com/s/cl5907ze1k2af ... .capx?dl=0


Will check out, thanks a lot.
Blank layout solution is top, cheers. Otherwise I would have tried to make all transparent stuff have 'destroy outside layout' (which would involve lots of clickery)
B
6
S
3
Posts: 122
Reputation: 1,632

Next

Return to How do I....?

Who is online

Users browsing this forum: Yahoo [Bot] and 9 guests