Giter Club home page Giter Club logo

foreach.gml's Introduction

foreach.gml

Donate License

It is a foreach() 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(collection as (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 inside as keyword brackets after item var.

Installation:

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

Examples:

Foreach in array:

var apples = ["red", "green", "yellow", "mac", "forbidden"];

foreach(apples as (apple, i) {
    show_debug_message(string(i) + ": " + apple);
});

Output:

0: red
1: green
2: yellow
3: mac
4: forbidden

Foreach in struct:

var apples = { red : "delicious", green : "eww", yellow : "yum" };

foreach_struct(apples as (taste, color) {
    show_debug_message(color + " is " + taste);
});

Output:

yellow is yum
red is delicious
green is eww

Foreach in list:

var apples = ds_list_create();
ds_list_add(apples, "red", "green", "yellow", "mac", "forbidden");

foreach_list(apples as (apple, i) {
    show_debug_message(string(i) + ": " + apple);
});

Output:

0: red
1: green
2: yellow
3: mac
4: forbidden

Foreach in map:

var apples = ds_map_create();
ds_map_add(apples, "red", "delicious");
ds_map_add(apples, "green", "eww");
ds_map_add(apples, "yellow", "yum");

foreach_map(apples as (taste, color) {
    show_debug_message(color + " is " + taste);
});

Output:

yellow is yum
red is delicious
green is eww

Foreach in stack:

var apples = ds_stack_create();
ds_stack_push(apples, "red", "green", "yellow", "mac", "forbidden");

foreach_stack(apples as (apple, i) {
    show_debug_message(string(i) + ": " + apple);
});

Output:

0: forbidden
1: mac
2: yellow
3: green
4: red

Foreach in queue:

var apples = ds_queue_create();
ds_queue_enqueue(apples, "red", "green", "yellow", "mac", "forbidden");

foreach_queue(apples as (apple, i) {
    show_debug_message(string(i) + ": " + apple);
});

Output:

0: red
1: green
2: yellow
3: mac
4: forbidden

Foreach in priority:

var apples = ds_priority_create();
ds_priority_add(apples, "red", 10);
ds_priority_add(apples, "green", 30);
ds_priority_add(apples, "yellow", 20);

foreach_priority(apples as (apple, priority) {
    show_debug_message(string(priority) + ": " + apple);
});

Output:

30: green
20: yellow
10: red

Break:

You can add breakeach keyword in that foreach with a hacky modification, but it's not recommended as a foreach loop is not intended to be breakable.

#macro breakeach breakme[@ 0] = true;

function foreach(array, func) {
    var breakme = array_create(1);
    breakme[0] = false;
    
    var size = array_length(array);
    
    for (var i = 0; i < size; i++) {
        if (breakme[0] == true) break;
        
        var element = array[i];
        func(element, i, breakme);
    }
}

var apples = ["red", "green", "yellow", "mac", "forbidden"];

foreach(apples as (apple, i, breakme) {
    show_debug_message(apple);
    
    if (i == 2) breakeach;
});

Author:

Nikita Musatov - MusNik / KeeVee Games

License: MIT

foreach.gml's People

Contributors

keeveegames avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.