You are not logged in.
Pages: 1
I saw a GMC topic today for a script that may someday find it's way here to join the other resource management scripts. The intent of this one is the map all of the scripts in a project. The point of interest in this topic here is the use of the for loop.
~Dannyboy~ wrote:Side note, if only the comma operator was supported in GML, then the following would work. Oh well.
You mean like this?
// scr_map_scripts(map, max) { var i, j; // We assume there are no more than max ids "missing" in a row for ({i = 0 j = 0}; j < argument1;{i += 1 j += 1}) { if (script_exists(i)) { ds_map_add(argument0, script_get_name(i), i); j = 0; } } }
http://gmc.yoyogames.com/index.php?s=&s … &p=3240850
Interesting, no? I've seen this done before, but I couldn't remember the proper syntax. Might on the rare occasion make porting from other C-like languages a bit simpler. I know I've coded around these compound expressions in for loops before. If you like, you can (and probably should) follow those sub-expressions with a semicolon, or at least use them as a separator.
for ({i = 0; j = 0}; j < argument1; {i += 1; j += 1})
If nothing else, it certainly allows for far too clever code.
Abusing forum power since 1986.
Offline
Same here. I knew of it's existence but I forgot the proper syntax.
I just switched to a while statement when wanting the functionality though.
Offline
Yes, though it can be confusing. like the c++ iif statement which I never use. You know the expression: far too clever for his own good.
I knew a guy who used to use all these shortcuts, For statements a mile long. Leaving us in the dust. No one wanted to take over any of his projects. So all his hard work would end up in the trash.
Offline
Well I did find quite a few uses for these .. Heck I often write an "iif" script in gm to emulate this function, it's usefull if you use 1-statement-expressions. (+ in c++ there's I believe a subtile, but important differnce in "order"? - not sure though)
On the topic of the multiple statements in the for-statement: well it IS usefull if you want to "check in the middle of the code".. Often you have something like this:
while (A) {
A = f();
if (A) {
//more code that happens as long as A = true
}
}
Which in C++ could be abbreviated to:
while (A = f(), A ) {
//more code that happens as long as A = true
}
example (from textbook):
int i,s;
s = 0;
while (std::cin >> i, i > 0) s += i;
Now I wonder/doubt this is possible in gm?
var i,s;
s = 0;
while ({i = get_integer("new number",0); i > 0}) s+=i;
Can't test here, sorry but my pc got blown up a few months ago. (Lightning struck our house while the laptop was on the net).. Am waiting for the new schoolyear before I get a new pc, and writing this post from my old-500MHz-pc. (I haven't even tried installing gm here, even firefox runs slow as hell).
Offline
That while
statement won't work. GML doesn't allow multiple statements in the comparison clause of for
, while
, or until
.
I did all my best GM work on a 500MHz PIII.
Abusing forum power since 1986.
Offline
while (std::cin >> i, i > 0) s += i;
I'm a big fan of the comma operator in C++ -- combined with the ternary operator, it essentially lets you write Lisp-style code. (The type system imposes some restrictions, but it's not too prohibitive.) Admittedly it's not the best thing for legibility, but it can be quite handy in contest programming and such. Also useful for forcing left-to-right evaluation with a terse expression, or just writing nice compact while-loops.
For better or for worse, the comma operator doesn't seem to have made its way into C-like languages like Java or C# (or GML).
Offline
for ({i = 0; j = 0}; j < argument1; {i += 1; j += 1})
Hey, that's neat! I'll have to try remember that.
The only limit is my imagination. lol
Chris
Offline
Hmm, that script looks somehow familiar
Offline
I just cut and pasted it from above. I assumed that if I used 'quote' instead of 'code' the formatting/colouring would be lost.
Chris
Offline
var i,s; s = 0; while ({i = get_integer("new number",0); i > 0}) s+=i;
This can work like so..
for(s=0;i>0;i=get_integer("new_number",0)) s+=1
So your previous example,
while (A = f(), A ) {
Becomes
for (A=f();!A;A=f()) {
Not too much longer.
Offline
Pretty slick.
Abusing forum power since 1986.
Offline
Pages: 1