I really don't understand "for each"

Get help using Construct 2

Post » Thu Jul 24, 2014 12:54 am

I've been banging my head against the monitor for 2 days now. Maybe 3. All I want is to have multiple enemies of the same type in a turn-based game.

Each enemy can move a max of 200 pixels per turn. It is supposed to take off a "move" at 10, 100, and 200 so it adds up to 3 turns. I gave each enemy a box that pathfinds, then it adds the distance to monitor sprite.

No matter what I try, it will not let me do it with more than one enemy. I got code working fine for just one character (Not pictured), but my attempts at making code for 2 or more enemies of the same type have all failed.

Either each box gets 3 when one gets 3, or one gets 3 and the rest get zero, or one gets 500 and the other get zero. No matter what I do, it's wrong. Each one is supposed to get 3 turns added once it reaches 200. That's it.

So, it should work like this:

enemy: distance travelled is 200, enemyMonitorTurn=3 (when the enemy walks 200 pixels, he uses all his turns (3).

Here is a picture

Image

It's kind of a mess because I've moved things to every possible location. I've put in "for each", I've put in a enemy monitor sprite with tied to a enemy.UID, I've tried everything. I've moved stuff around. I've removed or added things. I just don't know what it wants.

I finally got them to track the distance separately, but they won't add their turns up separately. I'm getting a headache....
B
79
S
30
G
35
Posts: 340
Reputation: 22,996

Post » Thu Jul 24, 2014 1:33 am

Picking instances is like doing a For each anyway. So in your first event, it's not needed.

You might read the first event as:

"For each enemyBox that is moving along a Path..."

Then your event with the for each doesn't 'sound' right:

Code: Select all
For each enemyBox that is moving along a Path
     For each enemyBox
          For each enemyMonitor where enemyBox.UID = enemyMonitor.UID


The "For each enemyBox" is redundant. It should just be:

Code: Select all
For each enemyBox that is moving along a Path
     For each enemyMonitor where enemyBox.UID = enemyMonitor.UID


From the manual:

For Each is commonly mis-used or used redundantly - actions already apply for each instance picked by conditions, so it often is simply not needed.


Edit: added a capx. ** bring up the browser developer window with F12 & select Console to see the output **

In the capx, the For each in events 2 and 3 are redundant. Try enabling them and there will be no difference to the output. However the For each in event 1 is necessary; try disabling it and the results are different. It needs to be there so that there is a logger message displayed for every enemy.

Hope it helps
You do not have the required permissions to view the files attached to this post.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Thu Jul 24, 2014 3:12 am

@codah Thanks. I'm going to play with this some more and see what happens. My code is pretty big. I have 3 main characters. There are also 150 events or so just for this one enemy. So, I may copy the enemy folder and simplify it while I play with this. I'll post again when it works or when I get stuck.
B
79
S
30
G
35
Posts: 340
Reputation: 22,996

Post » Thu Jul 24, 2014 4:45 am

No problem. I think the rule of thumb is, don't initially use For Each. If you think you need to in a particular case, post it and get an opinion.

I understand your code is complex, but as long as it's not due to using unnecessary code :)

Definitely take a step back, maybe create a new project and experiment with some things outside of the complexity of the rest of the project. And use logging. A lot. And functions :)
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Thu Jul 24, 2014 7:04 am

Kudos for digging to find the answer. You have the right mindset. It is good to learn 'For each' I wont say since others have covered it but the good news is that for each is a universal programming concept that you can read about in lots of books too or even watch on youtube. The basic concept is that you are iterating through a group of similar objects to 'do something to' or 'glean something from' them all. Perhaps increment a local variable, test their X position etc.
Image
B
36
S
14
G
11
Posts: 142
Reputation: 8,465

Post » Thu Jul 24, 2014 7:08 am

Moderator
B
95
S
34
G
33
Posts: 3,006
Reputation: 27,874

Post » Thu Jul 24, 2014 7:14 am

Great tut Arima. Faved ;)
Image
B
36
S
14
G
11
Posts: 142
Reputation: 8,465

Post » Thu Jul 24, 2014 11:31 pm

Thanks guys. I'll look at it again when this beer dies down.
B
79
S
30
G
35
Posts: 340
Reputation: 22,996

Post » Fri Jul 25, 2014 1:03 am

DrewMelton wrote:Thanks guys. I'll look at it again when this beer dies down.


Basically u use for each when u r building your own custom behaviors, logic or state machines with events. For behaviors from plugins it is not needed.

U might just want to add for each above your first monitor enemy event in the second tree, under walking and distance.
My professional Royalty Free Music at Scirra Assets Store
--------------------------------
Specs: i5 2500, 16gb of ram, gtx 770, win 7, Focusrite Scarlett 8i6, Mackie mr8mk2, Alesis 320, browsing the net on chrome.
B
89
S
30
G
22
Posts: 1,985
Reputation: 20,099

Post » Sat Jul 26, 2014 10:41 pm

@codah or @Arima or @megatronx or anyone!

I am still having trouble getting it to set my instance variables right. I just need each enemy to reach 3 turns after walking 200 pixels. But it will not let me do it no matter what I do.

I either get one enemy with 3 and the others with 0, or 1 enemy with several hundred and the others with zero, or some weird combination of whatever the game can think of.

No matter how many combinations I try, no matter where I move the conditions or events, I get the same results of one of the above.

I can get the enemy box to calculate the proper distance, and they will set their boolean variables as well usually. So, I guess that's a start, but I'm running out of ideas.

I have tried using just the enemy box since it is the one doing the pathfinding. I gave it boolean variables and instance variables, and still it will not calculate them properly. It evens ignores my trigger once events and does it infinite times or no times at all sometimes.

I just don't know what it wants. I don't know why it is so picky.

Here is a screenshot of my latest attempt of many. Like I said, I have tried many combinations even with different objects.

Image
B
79
S
30
G
35
Posts: 340
Reputation: 22,996

Next

Return to How do I....?

Who is online

Users browsing this forum: beguinner, mcscooter, totoe, Yahoo [Bot] and 21 guests