- Notifications
You must be signed in to change notification settings - Fork2
foreach() loop implementation for GameMaker 2.3 for arrays, ds_lists, ds_maps, ds_stacks, ds_queues, ds_priorities and structs
License
KeeVeeGames/foreach.gml
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
It is aforeach()
loop implementation for GameMaker: Studio 2.3+ for arrays, ds_lists, ds_maps, ds_stacks, ds_queues, ds_priorities and structs.
Note: queue-type data structures such as stacks, queues, and priorities will be empty after full foreach pass.
The general syntax is:
foreach(collectionas(item){// do things with item});
You also can receive additional parameters such as iteration index, key for map, priority for priority queue, and property name for a struct, specifying it insideas
keyword brackets after item var.
Get the latest asset package from thereleases page. Import it into IDE.
Alternatively copy the code from corresponding scripts into your project.
varapples=["red","green","yellow","mac","forbidden"];foreach(applesas(apple,i){show_debug_message(string(i)+": "+apple);});
Output:
0: red1: green2: yellow3: mac4: forbidden
varapples={red :"delicious",green :"eww",yellow :"yum"};foreach_struct(applesas(taste,color){show_debug_message(color+" is "+taste);});
Output:
yellow is yumred is deliciousgreen is eww
varapples=ds_list_create();ds_list_add(apples,"red","green","yellow","mac","forbidden");foreach_list(applesas(apple,i){show_debug_message(string(i)+": "+apple);});
Output:
0: red1: green2: yellow3: mac4: forbidden
varapples=ds_map_create();ds_map_add(apples,"red","delicious");ds_map_add(apples,"green","eww");ds_map_add(apples,"yellow","yum");foreach_map(applesas(taste,color){show_debug_message(color+" is "+taste);});
Output:
yellow is yumred is deliciousgreen is eww
varapples=ds_stack_create();ds_stack_push(apples,"red","green","yellow","mac","forbidden");foreach_stack(applesas(apple,i){show_debug_message(string(i)+": "+apple);});
Output:
0: forbidden1: mac2: yellow3: green4: red
varapples=ds_queue_create();ds_queue_enqueue(apples,"red","green","yellow","mac","forbidden");foreach_queue(applesas(apple,i){show_debug_message(string(i)+": "+apple);});
Output:
0: red1: green2: yellow3: mac4: forbidden
varapples=ds_priority_create();ds_priority_add(apples,"red",10);ds_priority_add(apples,"green",30);ds_priority_add(apples,"yellow",20);foreach_priority(applesas(apple,priority){show_debug_message(string(priority)+": "+apple);});
Output:
30: green20: yellow10: red
You can addbreakeach
keyword in that foreach with a hacky modification, but it's not recommended as a foreach loop is not intended to be breakable.
#macrobreakeachbreakme[@0]=true;functionforeach(array,func){varbreakme=array_create(1);breakme[0]=false;varsize=array_length(array);for(vari=0;i<size;i++){if(breakme[0]==true)break;varelement=array[i];func(element,i,breakme);}}varapples=["red","green","yellow","mac","forbidden"];foreach(applesas(apple,i,breakme){show_debug_message(apple);if(i==2)breakeach;});
Nikita Musatov -MusNik / KeeVee Games
License:MIT
About
foreach() loop implementation for GameMaker 2.3 for arrays, ds_lists, ds_maps, ds_stacks, ds_queues, ds_priorities and structs