GML Script Return don't work

MIkadini

Member
I've try to make a simple script that simply increment a number that we pass in the argument. In my object i call this script function every step and the variable is crate in the create section and not in the step section. this is my code, the resoult in the console isn't increment and simply remain equal to the original value.

This is in the step section:

ciccia = Aumento(ciccia);
show_debug_message(string(ciccia));

This is in the script function:
return argument0++;

The debug output is the same number that i create in the create section (was 0)
 

MIkadini

Member
I resolve using ++arugment0; i find soo strange this chose, i always use ++ after the var in other language
 

Nidoking

Member
I resolve using ++arugment0; i find soo strange this chose, i always use ++ after the var in other language
++ before - increment before running the line
++ after - increment after running the line

You are literally saying "first return argument0, then inc- hey, wait, where are you going? I wasn't done yet!" But you were done.

Why not just use ++ instead of making a script? ciccia++; does the same thing but makes sense to people who see it and won't cause problems or confusion.
 

MIkadini

Member
++ before - increment before running the line
++ after - increment after running the line

You are literally saying "first return argument0, then inc- hey, wait, where are you going? I wasn't done yet!" But you were done.

Why not just use ++ instead of making a script? ciccia++; does the same thing but makes sense to people who see it and won't cause problems or confusion.
i start yesterday to use this engine, it's simple an experiment for try to understand how the script section work.
 

Yal

šŸ§ *penguin noises*
GMC Elder
argument0 is a copy of the value you pass in, it's discarded at the end of the script. So incrementing it has no effect. And like others have said, after++ changes the value after it's used, ++before changes the value before it's used. So you increment argument0 but not the return value.
 

Roderick

Member
In every language that I've used that uses ++, there's always been a section in the docs that explains the difference between using it before and after the variable, and usually an addendum saying that it rarely matters which you use.

I'm fairly certain that this is the first time I've ever seen it matter.

I religiously use brackets around everything to make sure everything is done in the right order, even when I know that the default order of operations matches what I want. I wonder if return(value++) and return(++value) have the same result. I'll have to test at some point.
 
Last edited:

gnysek

Member
I resolve using ++arugment0; i find soo strange this chose, i always use ++ after the var in other language
But do you use it in return expression? That's the difference.

GML:
// PRE-INCREMENT - adding to be will be done before copying value to a
a = ++b;
// same as:
b += 1;
a = b;

// POST-INCREMENT - adding to be will be done after copying value to a
a = b++;
// same as
a = b;
b += 1;
It's same in all languages I know (so a++ isn't a shortcut from a+=1, even if result is same if you're not assigning it to another variable.

Then return a++; in fact can be translated to:
GML:
var b = a;
a +=1;
return b;
 

FrostyCat

Member
In every language that I've used that uses ++, there's always been a section in the docs that explains the difference between using it before and after the variable, and usually an addendum saying that it rarely matters which you use.
Such an addendum is full of crap. There are plenty of book lines involving ++ and -- where the choice of prefix and postfix forms is absolutely instrumental.

Here are a few of those book lines:

Looping N times in zero-indexing using while:
GML:
var i = N;
while (i--) {
    /* i runs N-1 to 0 in here */
}
Try an action, then retry it on failure up to N times in total:
GML:
var fails = 0;
do {
    /* Action */
} until (/* Action successful */ || ++fails == N)
/* fails now holds the number of failed attempts */
Building up an array bottom-up:
GML:
var arr = [];
var n = 0;
<loops and/or conditions> {
    arr[n++] = <new content>;
}
/* n now holds the actual size of the array */
Using the opposite form (i.e. prefix in place of postfix or vice versa) in any of the above causes off-by-one errors.

I wonder if return(value++) and return(++value) have the same result. I'll have to test at some point.
Absolutely not. For example, if value comes in as 5, the first returns 5 and the second returns 6.
 

saffeine

Member
But don't the parentheses still mean "Do this first", just like they would in something like 6 * (5 + 3) ?
as far as anyone's led to believe, yes. the thing is though, ++var and var++ return two completely different things, so you get two different results.
GML:
var = 0;
variable = ( var++ ); // variable is set to var, and then var is incremented.

var = 0;
variable = ( ++var ); // var is incremented, and then variable is set to var.
replacing variable = with return will do the same thing.
the former will return var and then increment it, the latter will increment var and then return it.
 
Last edited:

FrostyCat

Member
But don't the parentheses still mean "Do this first", just like they would in something like 6 * (5 + 3) ?
It still means "do this first", but pay attention to the original context of the sentence I'm responding to:
I wonder if return(value++) and return(++value) have the same result.
There's only one thing to do in both cases, so it will be the first thing done whether there are brackets there or not. The main difference here is the return value of value++ and ++value, not the use of brackets.
 

Roderick

Member
So, just to make sure I understand correctly:

++ or -- is not actually part of the equation then?

If before, it is always parsed before the whole equation is evaluated, and if after, it is parsed afterwards?

I've always looked at it as a mathematical function, and I guess that's where I (and the OP) am getting hung up.
 

saffeine

Member
So, just to make sure I understand correctly:

++ or -- is not actually part of the equation then?

If before, it is always parsed before the whole equation is evaluated, and if after, it is parsed afterwards?

I've always looked at it as a mathematical function, and I guess that's where I (and the OP) am getting hung up.

i think it's local to the variable it's attached to, not the equation or sum. in fact, i don't think it can even be applied to anything that isn't a variable ( like trying to execute 6++ ).
as @gnysek mentioned previously:
GML:
var i = 0;
return i++; // returns 0.

//    - is the equivalent of -

var i = 0;
return i; // returns 0.
i += 1; // this wouldn't be called if used like this, seeing as return would interrupt it, i include it here purely for explanation purposes.



var i = 0;
return ++i; // returns 1.

//    - is the equivalent of -

var i = 0;
i += 1;
return i; // returns 1.
think of it like a macro more than a mathematical function. it basically makes quick work of trivial decrements / increments so you aren't typing more than you need to.
also as a side note, i've been using ++ mostly, but the exact same thing applies to --. if put before the variable, it will decrease the value by one before returning it, if put after, it will return it and then decrease the value.
 
Last edited:

FrostyCat

Member
So, just to make sure I understand correctly:

++ or -- is not actually part of the equation then?

If before, it is always parsed before the whole equation is evaluated, and if after, it is parsed afterwards?

I've always looked at it as a mathematical function, and I guess that's where I (and the OP) am getting hung up.
This has nothing to do with order of operations, this has to do with the difference in return value between 4 logically distinct operations.
  • ++v increases the value by 1, and returns the altered value.
  • --v decreases the value by 1, and returns the altered value.
  • v++ increases the value by 1, but returns the unaltered value.
  • v-- decreases the value by 1, but returns the unaltered value.
The prefix forms all return the altered value, and the postfix forms all return the unaltered value.

Example:
GML:
v = 0;
A = /*operation*/;
B = v;
When /*operation*/ is:
  • ++v: A = 1, B = 1
  • --v: A = -1, B = -1
  • v++: A = 0, B = 1
  • v--: A = 0, B = -1
 

Nidoking

Member
Don't think of ++ and -- like functions that return values. They're not, and they don't. They're slipping additional instructions into the code between the ones that are already there. Prefix vs. postfix determines whether the added instruction goes before or after the line it's on.

return (x++) translates to:

return x;
x+=1;

Now, would anyone expect the return value of that to be x + 1? I sure hope not, although I have encountered many people who seem to feel that way.

return (++x) at least becomes:

x+=1;
return x;

which gets what you want. But why are you incrementing x just to throw it away? return x+1 does EXACTLY the same thing, but confuses no one. Even the people who are scratching their heads at the above ideas can tell you what x+1 does.

The simple solution is this: If you don't know how they work, don't use them at all. If you've read this far in the thread and still think you have something relevant to say, then you don't. Learn to use +=1 and deal with the couple of extra keystrokes, for your own sake. Come back to this later and maybe you'll understand it then.
 

Roderick

Member
Yeah, my confusion isn't with regards to the return command, as in the OP, I'm just piggybacking off the existing conversation to get some more knowledge myself.
 
Top