Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

The #mfunc is useful, and i can do this with it:

#mfunc foreach(i, ins, ary) \
for (var i##__ = array_length_1d(ary), i = 0; i < i##__; i++) { var ins = ary[@ i];

var array = [100, 101, 102];

foreach (i, ins, array) 
    show_debug_message(i);
    show_debug_message(ins);
}

It's cool,  but missing a left brace.

I wonder if i can write it with the left brace just like this in some way:

#mfunc foreach(i, ins, ary){  \
for (var i##__ = array_length_1d(ary), i = 0; i < i##__; i++) { var ins = ary[@ i];

var array = [100, 101, 102];

foreach (i, ins, array)  {
    show_debug_message(i);
    show_debug_message(ins);
}

You can use for-loops to "chain" declarations and statements,

#mfunc foreach(arr, val)\
    for (var val##_arr = arr, val##_len = array_length_1d(val##_arr), val##_ind = 0;\
    val##_ind < val##_len; val##_ind++) for (var val = val##_arr[val##_ind];;break)
foreach([1,2,3], v) {
    trace(v);
}

(note: if you want "break" to work, you'll need a bit of extra logic)

Or write them out like you would in higher-end programming languages [with actual AST processing in macros],

#mfunc foreach(arr, val, block)\
    for (var val##_arr = arr, val##_len = array_length_1d(val##_arr), val##_ind = 0;\
    val##_ind < val##_len; val##_ind++) { var val = val##_arr[val##_ind]; block; }
foreach([1,2,3], v, {
    trace(v);
});

Get it, thanks for the answer.