This page has been translated automatically.
Getting Started
Migrating to UNIGINE 2.0
C++ API Migration
UnigineScript
Core Library
Engine Library
Node-Related Classes
GUI-Related Classes
Plugins Library
High-Level Systems
Samples
Usage Examples
C++ API
API Reference
Integration Samples
Usage Examples
C++ Plugins
注意! 这个版本的文档是过时的,因为它描述了一个较老的SDK版本!请切换到最新SDK版本的文档。
注意! 这个版本的文档描述了一个不再受支持的旧SDK版本!请升级到最新的SDK版本。

Foreach

The foreach construct is used to iterate over vectors and maps.

Syntax

Source code (UnigineScript)
foreach(value_variable; container; loop_increment) { 
	// some_code;
}

Parts

  • value_variable is the value of the current container element.
  • container is a vector or a map.
  • loop_increment is executed at the end of each iteration.

On each iteration, the value of the current element is assigned to value_variable and the internal container cursor is moved to point at the next container element. The type of value_variable doesn't matter. loop_increment is optional.

Modifications of elements inside the foreach block affect the container; synchronization occurs at the end of each iteration.

Examples

  • For each vector element:
    Source code (UnigineScript)
    int vector[4] = ( 1, vec3(1,2,3), -7.2e-2, "end" );
    foreach(int i; vector) {
    	log.message("%s\n",typeinfo(i));
    }
    
    The example displays the following:
    Output
    int: 1
    vec3: 1 2 3
    float: -0.072
    string: "end"
  • For each map element:
    Source code (UnigineScript)
    int map[] = ( 1 : "begin", 2 : 2, 3 : "end" );
    foreach(int i; map) {
    	log.message("%s\n",typeinfo(i));
    }
    
    The result is:
    Output
    string: "begin"
    int: 2
    string: "end"
  • Increment:
    Source code (UnigineScript)
    foreach(int i, j = 0; vector; j++) {
    	log.message("%d: %s\n",j,typeinfo(i));
    }
    
    The output is:
    Output
    0: int: 1
    1: vec3: 1 2 3
    2: float: -0.072
    3: string: "end"
Last update: 2017-07-03
Build: ()