Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
gdevelop5:events:foreach [2020/12/26 13:56]
127.0.0.1 external edit
gdevelop5:events:foreach [2021/11/22 23:06] (current)
Line 1: Line 1:
 # For Each events # For Each events
  
-A For Each event is a kind of an [[gdevelop5:events|event]] that will take an object as a parameter. Then, during the game, for every instance of the object, the conditions and actions will be repeated.+"For Eachevent is a special [[gdevelop5:events|event]] that takes an object as a parameter. Then, during the game, for every instance of the object, the conditions and actions will be repeated.
  
-Usually, you don't need to use "For Each" events. Conditions and actions will automatically run on all of the objects. They are defined by their conditions. However, from time to time, "For Each" events can be useful to run an action that depends on each instance of the object.+In many cases, you don't need to use "For Each" events. This is because conditions and actions automatically run on all instances of an object However, if you need to make sure that your conditions and actions are run independently on every instanceyou should use a "For Each" event.
  
 ## Examples  ## Examples 
Line 24: Line 24:
 {{ :gdevelop5:events:for-each-example.png?nolink |}} {{ :gdevelop5:events:for-each-example.png?nolink |}}
  
 +## For each child variable (of a structure or array)
 +
 +There is a special version of "For Each" that can be used to loop through the [[gdevelop5:all-features:variables:|child variables]] of a structure variable.  The "For Each Child Variable" event enables conditions and actions to be run on all of the child variables under a structure variable (or an array).
 +
 +This event will store the value of each child variable (as a string or number) and the name of the child variable (as a string) into scene variables.  The names of these scene variables are defined in the event parameters.  
 +
 +## Example
 +
 +- Define a structure variable (Fruits) with multiple child variables (Apple, Avocado, Banana)
 +
 +{{:gdevelop5:events:foreverchildvariables.png?direct |}}
 +
 +- Loop through the child objects and change the text to show the name and value of the child object. 
 +
 +{{:gdevelop5:events:foreverchildevents.png?direct |}}
 +
 +- The game preview will shows the correct values
 +
 +{{:gdevelop5:events:foreverchildouput.png?direct |}}