# GMLscripts.com

Discuss and collaborate on GML scripts

You are not logged in.

## #1 2009-03-23 20:14:21

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

### returns: make sure a scripts always does it (or not)

Well as was shown in another topic on the forums (can't find it right now): GM acts not really in a normal way when it expects a value from a scripts: yet isn't given one.... (script ends without return).

This makes me believe that scripts should either ALWAYS return something or NEVER return something.
Many scripts currently on the site return "-1" in case of an error, yet don't return anything when the script functioned well... However the user still would check the returned variable: and in some cases it might thus give a false-positive and return "-1". - Or even worse: the returned value might be of the type "string", and actually CAUSE an error!

So I propose that all scripts should return 'true' in case of successfull operation.. Or simply return nothing EVER at all (so no -1 in case of errors either)!

Offline

## #2 2009-03-25 04:55:43

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

### Re: returns: make sure a scripts always does it (or not)

I beleve it was xot who made a similar report somewhere. If I remember right, if you return nothing, the result received by the caller will be the value of the last script that returned something (if that makes any sense)

BUT I have found (GM7) that if you foget to return anything, the retun value will be the result of the last variable assignment

script0
{
var t;
t = 10;

if(argument0==1) t = 100;;
}

show_message(string(script0(0))) //10!!
show_message(string(script0(1))) //100!!

Offline

## #3 2009-03-27 03:02:09

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

### Re: returns: make sure a scripts always does it (or not)

http://www.gmlscripts.com/forums/viewtopic.php?id=86

So I propose that all scripts should return 'true' in case of successfull operation.. Or simply return nothing EVER at all (so no -1 in case of errors either)!

I'm not sure I completely understand this proposal, in particular, the second idea, but I am considering a requirement that all scripts return a meaningful value in all cases. Of course, well written code should never fall victim to this return bug/feature, but we can't all be perfect. I don't remember how I stumbled onto this issue, but it wasn't in a debugging scenario. I think I was just curious about what value, if any, was returned by a script without a return statement.

Abusing forum power since 1986.

Offline

## #4 2009-03-27 08:16:17

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

### Re: returns: make sure a scripts always does it (or not)

Well here's a header to explain my "problem" a bit better:

/*
**  Usage:
**      sprite_invert_color(sprite)
**
**  Arguments:
**      sprite      sprite to change
**
**  Returns:
**      (-1) on error
**
**  Notes:
**      This script inverts the colors of a given sprite.
**      No new sprites are created, the given sprite is changed.
**
**  GMLscripts.com
*/

That function only returns "on error": when not giving an error it ends the script without a return statement. Now I would like to propose such functions to either DON'T return anything on error (no error checking), or return "true" when the function behaved succesfully. - But definatelly not a "return if"!

Right now the error checking is just useless!

Offline

## #5 2009-03-27 09:20:52

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

### Re: returns: make sure a scripts always does it (or not)

I see where you are coming from, especially in light of what you posted earlier about real/string mix-ups. When those scripts were written the assumption was that zero was returned if a return statement wasn't executed. What do they say about assumptions, again?

At this point I think ALL scripts should ALWAYS return a value. I guess that's because I've always sort of equated scripts with functions rather than procedures or subroutines. Regardless of semantics, and regardless of GM's behavior, a function that only returns a useful value some of the time is a poorly written one, so your point is well taken.

Abusing forum power since 1986.

Offline