gnysek
Member
I was playing a little with functions today, and I wanted to reproduce same code from JS into GML.
So, first, JS code:
so in GML:
So it seems that
Seems to work OK.
My question is - is this a bug, that in first case it doesn't work, am I missing something, or I'm just wrongly assume that closures should work in GML similar to other languages?
So, first, JS code:
JavaScript:
function test(a) {
let b = a;
return function(c) {
return b + c;
}
}
var t1 = test(1);
var t2 = test(2);
console.log(t1(10)); //returns 11
console.log(t2(10)); //returns 12
GML:
function test(a) {
b = a;
return function(c) {
return self.b + c;
}
}
var t1 = test(1);
var t2 = test(2);
show_debug_message( t1(10) ); // shows 12
show_debug_message( t2(10) ); // shows 12
b
becames available in instance scope, and I can't do same. However, changing function test
to:
GML:
function test(a) {
return method({b: a}, function(c) {
return self.b + c;
});
}
My question is - is this a bug, that in first case it doesn't work, am I missing something, or I'm just wrongly assume that closures should work in GML similar to other languages?
Last edited: