"For each" question

This forum is currently in read-only mode.
  • I'm having alot of trouble with multiple objects of the same kind behaving as a group instead of individuals.

    When exactly is it best to use "for each"?

    For example, I want object "Enemy" to play a walking a animation when value "Walk" is 1, with "Trigger once" so the animation won't stay on the first frame.

    It works with one enemy on the screen, but if two are walking at the same time only one will animate.

    If I add "For each" to the event then they all animate whenever one of them is walking.

    This is killing me whenever I try to add some AI, could anyone throw some tips my way?

  • Can you throw together a quick example .cap? For Each triggers all the conditions below it, and actions and subevents, once per instance. It's a bit like saying "For i = 1 to Object.Count" and for each iteration, it picks a specific instance. Essentially, it is used to force system actions and conditions to work on a per-instance basis.

    For example:

    + On any key pressed

    + Sprite X < 320

    Create object 'ring' and position by Sprite

    In this case, when the event triggers, if a Sprite is left of X=320, only one 'ring' is created. The action is a system action, so simply executes one time. The ring is positioned by the first picked Sprite.

    Using For Each:

    + On any key pressed

    + Sprite X < 320

    + For Each Sprite

    Create object 'ring' and position by Sprite

    This forces the actions to be run once per instance, so a 'ring' object is created for every 'sprite' left of 320.

    Another use:

    + Compare values: distance(A.x, A.y, B.x, B.y) less than 100

    Destroy A

    In this case, because Compare Values is a system expression, it simply runs once, retrieving the values for the first A and B instances, calculating a single distance, and making a single comparison. This isn't useful if you have multiple A and B objects - unless you force the condition to check once for every combination of A and B:

    + For each A

    + For each B

    + Compare values: distance(A.x, A.y, B.x, B.y) less than 100

    Destroy A

    This destroys any A if it comes within 100 pixels of any B object, instead of only applying to the first instances.

  • I posted a .cap, that till some level demonstrates what you seek, in the uploads section. (help3 page 2)

    The presents of "for each" confused me for long time. Long time i tried 'loops' for picking objects. Wich is the wrong aproach.

    Forget "for each ", its usaly not usable.

    1/ Loops u need

    to repeat an ACTION ... (x times) ... till the condition of the loop is met. You can use the loopcounter to alter variables in the loop.

    Like building a row of objects spacing 20 pixels, 10 objects in total

    for loop "ThisLoop" 1 to 10

    _________set objects x to .X + ThisLoops' counter * 20

    2/ Pickin objects you do with events.

    Most events that pick an object, pick a group of objects. Pick specifiek object you do with an event combined with a condition. The condion works as a filter.

    You can also only use a condition.

    Concrete:

    If u have 2000 of the same instances on your screen

    an event like

    Sprite: Value 'PrivateV' Equal to 1

    Will set all Instances with private variable 'PrivateV' Equal to 1 as picked ..

    Every Action with this event will only target the picked Instances

    How u do that ?

    Add an event ...

    pick the object

    click on the tab private variables (on bottom)

    click on compare private variable

    pick ur private variable

    give the value u want to be compared.

    So un ur case ..

    the event

    Enemy: Value 'walk' Equal to 1

    Will "pick" out all Instances called "enemy" only those that have the private variable "walk" set to 1, and will pass "the picked objects" to the actions combined to the event.

    Those private variables you can set when

    A/bringin the object into the layout in the Layout editor.

    B/When u create an object in an action, the created object is "picked". Create u do in an action. So every next line u put in the same action block, targets the just created object. (Its picked)

    Including setting private variables and the use of "self" in expressions.

    C/And ofcourse u can set a private variable in every action anywhere, by plain targeting (pick) the object and set a private variable. But this u know.

    So in general. Think out of loops for picking objects. Thats the wrong aproach.

    Pick Objects with events. Most events return a group of objects. And filter out the object you need by conditons. Most Events pick objects by default. By instance, the collision detection will set all involved objects as "picked". When u use an action after this, the action will target the 'picked objects', in this case those that are involved in the collision

    You can use Condtions alone too.

    It took me long time to understand this, its not in the docs, and ne one explained me.

    Most poeple who onces programmed in another object-based language, think in LOOPS and LOOPS and LOOPS.

    Ashley made the looops we need to "pick objects' buildin, fast and automaticaly, all you need to do is "filter" the objects u need by conditions. Usaly based on comparing to a private variable.

    Try to avoid using GLobal Variables. THey are slow, and are not as accesable and flexible as Private variables. Even prefer to make a Control object, that you only use for holding variables as private variables, over using global variables.

    Also Use the Family option to make groups of "clones" or differend "instances". Events targeting a family sets the whole group of objects in the family to "picked".

    Al u need is a condition that filters out the objects you need.

    Like

    Always

    event: Sprite "red balloons"

    ____conditon: "red Balloons" smaller then 5

    _________________________________action: destroy

    Hope this helped you.

    j0h

  • Asley plz write a document about the concept of "picked objects".

    My english still sucks.

    Captain struck me with the light to see.

    But i still think it would be easyer to be able to to pick a system object in the actions wizzard thats holding the "picked objects"

    You could give it usable propertys too, like

    That u can pick one of the objects in the "picked group" in the action wizzard, like u pick private variables, in a drop down menu.

    You could give it the property "picked count" ..

    and so many things.

  • [quote:35lcx7ap]Try to avoid using GLobal Variables. THey are slow

    I haven't seen any evidence to suggest global variables are slow; in fact, it's reasonable to expect them to perform faster, since they are in runtime memory and not wrapped by plugin code. However, this kind of micro-optimisation generally has no effect to improve performance, due to CPU/GPU parallelism.

    I'll write a wiki article on picking when I have time, it's a good idea I don't see how a 'picked group' is useful though, every object acts as a group of picked objects, because the actions only run on filtered instances.

    BTW, the system expression CountMatching can get the picked count.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • There are 3 reasons i ask for it.

    Most starters programmed at least some in object based languages.

    They think in "loops"

    That was the base of his question, u see ?

    he asked about "for each object" to pick objects .. wich looks like a "loop"

    and this kind of questions return regular.

    Adding this object would

    A/ make the system easyer to understand and more close to your initial target... namely make a visual "game constructor" by pick and drop, and the action wizzard is a very important concept in all this. So bring the "picked objects" to the action wizzard is a plus !

    B/ make .caps easyer to read

    C/ its easyer to imagine what happens behind the lines, and thats half of the debugging. right ? Especialy when u can see wich objects are "picked"

  • Thanks guys, the combined tips in this thread helped me pretty much figure it out and have a real breakthrough with my engine!

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