Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

foreach() loop implementation for GameMaker 2.3 for arrays, ds_lists, ds_maps, ds_stacks, ds_queues, ds_priorities and structs

License

NotificationsYou must be signed in to change notification settings

KeeVeeGames/foreach.gml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

DonateLicense

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.

Installation:

Get the latest asset package from thereleases page. Import it into IDE.
Alternatively copy the code from corresponding scripts into your project.

Examples:

Foreach in array:

varapples=["red","green","yellow","mac","forbidden"];foreach(applesas(apple,i){show_debug_message(string(i)+": "+apple);});

Output:

0: red1: green2: yellow3: mac4: forbidden

Foreach in struct:

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

Foreach in list:

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

Foreach in map:

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

Foreach in stack:

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

Foreach in queue:

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

Foreach in priority:

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

Break:

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;});

Author:

Nikita Musatov -MusNik / KeeVee Games

License:MIT


[8]ページ先頭

©2009-2025 Movatter.jp