I really don't understand "for each"

0 favourites
From the Asset Store
Tap the ball, control it, avoid touching spikes and score higher!
  • 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

    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....

  • 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:

    For each enemyBox that is moving along a Path
         For each enemyBox
              For each enemyMonitor where enemyBox.UID = enemyMonitor.UID[/code:tpschi5w]
    
    The "For each enemyBox" is redundant. It should just be:
    
    [code:tpschi5w]For each enemyBox that is moving along a Path
         For each enemyMonitor where enemyBox.UID = enemyMonitor.UID[/code:tpschi5w]
    
    From the manual:
    
    [quote:tpschi5w]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
  • 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.

  • 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

  • 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.

  • Great tut Arima. Faved

  • Thanks guys. I'll look at it again when this beer dies down.

  • 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.

  • 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.

  • I think you need to put FOR EACH as first event, and all other state events underneath it. Also you can put Every dt*2 Seconds above FOR each to not have it run every tick ( not needed ) . Also within each DistanceTraveled and Is events, add condition with your variables for example MoveClose = False. Try that.

  • See if this helps any. As you click each car, it updates it's text object independent of the others, and tracks it's own progress and which trigger to check next.

  • Are you able to extract a relevant portion of the capx? I can see it's pretty large.

    Edit: never mind. see if attached capx helps. It's based on the code in your screenshot.

  • Thanks guys. I'll look at your examples first thing tomorrow. It's getting late. I'm going to get to the bottom of this yet! I've come too far to be slowed down by something so small.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • codah Hi, I was having a bit of trouble getting your capx to work with more than one enemyBox. This is the same problem I am having in my game. One enemy is fine, but more than one and it stops working. Let me know if you can get yours to register right for 2 or more enemyBoxes.

    blackhornet That capx is interesting. I am going to tweak it and try to adapt it to the kind of conditions my game will have and see if it works.

    megatronx I may have already tried putting the for each first. It's hard to say what combinations I've done since I've tried a dozen variations. I "think" I know what you are saying about adding another condition. I may be misunderstanding you though, but I think I know what you mean.

    I'll hit this hard tomorrow and try to get it sorted one way or another!

    For you guys wondering why I'm going through all this trouble, I'll share a bit about my game. It is a turn-based RPG roguelike. I currently have it set up on a tile system (the tiles follow you around) so you can only move a certain distance (like 200 pixels for example).

    Here is a screenshot. I am NOT ready to start showing off my game yet, so this is the first public screenshot I have shown. Please ignore the Golden Axe sprites. I was about to start making my own character sprites when I got distracted with this step.

    I'm actually tempted to just make it so you can move any distance, and it will only use 1 turn. This would free up the movement a bit. I haven't decided yet though. The system I have is similar to the Civilization games where you take your turn, and the computer takes theirs. Each attack will cost one turn, and tasks like a health potion will also take one turn.

    The heroes are working fine. This problem I am running into with multiple enemies is one of the major issues I've had so far. Once I get this taken care of, it's back to smooth sailing until I hit another major snag. I've been working at this game since December. Of course, I just got Construct 2 in October, so I'm still learning as I go which is why it's taking so long.

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