GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2009-07-26 20:04:11

xot
Administrator
Registered: 2007-08-18
Posts: 1,201

for statement - compound expressions

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.

Pinpickle wrote:
~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

#2 2009-07-27 09:50:38

flexaplex
Member
Registered: 2008-12-11
Posts: 72

Re: for statement - compound expressions

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

#3 2009-07-28 00:47:29

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

Re: for statement - compound expressions

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

#4 2009-07-30 04:22:03

paul23
Member
Registered: 2007-10-17
Posts: 110

Re: for statement - compound expressions

Well I did find quite a few uses for these tongue.. 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

#5 2009-07-31 01:44:42

xot
Administrator
Registered: 2007-08-18
Posts: 1,201

Re: for statement - compound expressions

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. tongue


Abusing forum power since 1986.

Offline

#6 2009-07-31 19:11:58

xDanielx
Member
Registered: 2009-01-02
Posts: 38

Re: for statement - compound expressions

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

#7 2009-09-03 17:22:16

Flatlander
Member
Registered: 2009-09-03
Posts: 9

Re: for statement - compound expressions

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

#8 2009-10-04 06:43:00

~Dannyboy~
~hoqhuue(|~
From: Melbourne, Australia
Registered: 2009-10-02
Posts: 21
Website

Re: for statement - compound expressions

Hmm, that script looks somehow familiar wink

Offline

#9 2009-10-04 18:12:41

Flatlander
Member
Registered: 2009-09-03
Posts: 9

Re: for statement - compound expressions

I just cut and pasted it from above. I assumed that if I used 'quote' instead of 'code' the formatting/colouring would be lost. smile

Chris

Offline

#10 2011-05-10 23:07:13

Daniel
Member
Registered: 2011-05-04
Posts: 25

Re: for statement - compound expressions

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

#11 2011-05-11 01:06:22

xot
Administrator
Registered: 2007-08-18
Posts: 1,201

Re: for statement - compound expressions

Pretty slick.


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB