I'm trying to figure out the "for each" loop, Please help.

Welcome to our brand new Clickteam Community Hub! We hope you will enjoy using the new features, which we will be further expanding in the coming months.

A few features including Passport are unavailable initially whilst we monitor stability of the new platform, we hope to bring these online very soon. Small issues will crop up following the import from our old system, including some message formatting, translation accuracy and other things.

Thank you for your patience whilst we've worked on this and we look forward to more exciting community developments soon!

Clickteam.
  • Hi everyone,

    I'm trying to understand the "for each" action and the "on each object" condition and how they work. I couldn't find anything on them in the help files. Are they related to one and other?

    I thought it was a case where the "for each" action started a loop and the "on each object" was where the events went. But I have 2 events in the loop and I can't seem to get it to work properly. I'm using it on a group, dose that make a difference?

    I'm making a semi-isometric game and I want to give all the buildings a "shadow" object with the same ID. So that the "shadow" object can be used for collision detection and when the building is destroyed it's "shadow" is destroyed too. So there are no invisible walls left behind.

    I have 2 different objects in the group, I spread 0 through the Alterable Value A (witch I named ID) of the group. Then, in the loop I check to see if another veriable is set to 1 or 2. It creates the "Shadow" object but it doesn't set the ID to the ID of the main object. It instead sets it to the last ID of the other set of objects.

    I hope that explanation makes sense, thanks for reading this fare. Any help would be greatly appreciated.

    Follow my game dev on twitter Please login to see this link.

  • Seems like you're trying to apply a slightly older way of doing things and mixing it up with For Each. Used to be there was no For Each, and you would need to Spread Value via FastLoops to give your instances individual IDs. You would do this with different objects to make matching IDs, then pair them. This still works, but For Each, once an extension, is now stock with Fusion, so this technique is a little deprecated.

    For Each is a FastLoop that runs a number of times as the number of instances of that object that you have, and also will only apply actions to each instance one at a time during the loop.

    If I'm understanding, you might want to try "For Each of Two Objects," which will essentially make parent and child objects. You would want to make a shadow every time a building is built, pair the shadows with the buildings, and destroy them when the building is destroyed.


    Number of "Shadow" objects < NObjects "Buildings"
    -> Create "Shadow" at (0,0)

    Always
    -> "Building" - For Each of Two Objects - "Shadow", Loop Name "ID"

    On Each One of "Building", Loop Name "ID"
    + On Each One of "Shadow", Loop Name "ID"
    -> Set "Shadow" Position at (0,0), Relative to "Building"

    On Each One of "Building", Loop Name "ID"
    + On Each One of "Shadow", Loop Name "ID"
    + however "Building" is destroyed
    -> Destroy "Building"
    -> Destroy "Shadow"

    Please login to see this link.

    Edited 2 times, last by Ziplock (February 23, 2016 at 9:37 AM).

  • Hmm... I was trying to go through every building that has been created, create a Shadow for it, and then assign the ID of the building to the the shadow that was just created.

    Anyway, thanks a lot for your advice. I'll try the way you suggest tomorrow.

    I'm still a little confused though. How dose the program know witch shadow is connected to witch building, and wouldn't having a fast loop activate on "Always" cause slowdown? Or is that not a problem? I feel like the pairing shouldn't be checked accept at the start of frame and when a building is destroyed but maybe I'm just being paranoid.

    Follow my game dev on twitter Please login to see this link.

  • Well, if you want to attach an ID to the shadows, why not just attach the fixed value of each building object to them instead of an alterable value?

    My Please login to see this link. (which I actually use), my Please login to see this link. (which I mostly don't use), and my Please login to see this link. (which I don't use anymore pretty much at all really). If there are awards for "'highest number of long forum posts", then I'd have probably won at least 1 by now. XD

  • Ok, I'm using two groups, there are 2 objects in each group. There is a small building, with a corresponding shadow, and a large building with a shadow as well. I'm also testing a variable (Alt Ver B "Size") to see witch type of shadow should be created.

    I'm getting this strange occurrence where the loop will run though the first set of Buildings just fine, but when it goes to the next it keeps setting the ID to the loop index of the last of the first set of Buildings. Here is the code I'm using. Mouse over the buildings and the shadows to display there IDs.

    I feel like this should work.

    Please login to see this link.

    Follow my game dev on twitter Please login to see this link.

  • Here is an ForEach Loop Child -> Parent example,
    It uses Fixed Values, ForEach loops and such

    Please login to see this attachment.
    In this example creating child object is also dynamic, so whenever a new parent object is created, child objects are created too.

    Also, do not worry about slowdown, one for each loop will do nothing unless you use it constantly on a lot of objects (e.g. checking distances between all instances of enemies and allies, about 30 enemies and 30 allies began lagging (900 foreach loops + 900 distance checks, boy))
    I currently use around 5 For Each loops for different objects right now with "Always" as the event.

    As for your "I'm also testing a variable (Alt Ver B "Size") to see witch type of shadow should be created."
    you can use the "Create object by name" action in the built in Create object and use the expression [ 'shadow_'+str$(Alterable Value 'Shadow_Type') ] and name your shadows accordingly.

    - BartekB

    Join the Click Converse Discord! - Please login to see this link.

  • Hi PMBartekB

    Your example really helped, thanks a lot. I decided that the shadows didn't need to be different objects and I could instead just change there direction to make them match up. I'm also using a group for the main "big face" objects. So if you add more it should still work. Check out my edit to see what I mean.
    Please login to see this attachment.

    Thanks to everyone for there help. ^^

    Follow my game dev on twitter Please login to see this link.

  • Hay Marv,

    Sorry about the late reply. Here is my edit of the example from PMBartekB. I uploaded it to my dropbox. It allows you to create a child object, link it to the parent and then change the direction based on variables in the parent. Witch makes it match the parents color. You could probably take it a step further and spawn different objects from the child. But I didn't need to for my purposes. I hope that makes sense. But PMBartekB had nicely commented everything.

    Please login to see this link.

    Follow my game dev on twitter Please login to see this link.

  • I need to disagree with you here, loops are extremely useful, they can be used for pretty much anything.
    For example:
    - Loops becoming functions
    - Moving bullets incredibly fast, with pixel-perfect accuracy
    - Separating objects from each other (e.g. this example on this thread)
    - Level loading
    - Some other things I can't think of at the top of my head

    In addition, everything pretty much takes up memory. And of course, if you're going to make huge loops, it's expected to take a lot of energy from the computer to do the task.

    - BartekB

    Join the Click Converse Discord! - Please login to see this link.

  • I understand all that ,I guess sometimes it can be coded.. if its extremely important.

    what I meant was sorry ...i mean5 that Loops are good by use it when you really need it.

    A bullet collided to an object at speeds higher would need a loop ,things like that.

  • I'm happy to help. I hope you decide to add it to your library. I think other people might find it useful. Loops are still one of the things I've found the hardest to wrap my head around in clickteam products.

    Follow my game dev on twitter Please login to see this link.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!