GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2008-03-08 00:45:36

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

return - unexpected behavior

OK, this is a really weird one that affects GM5, GM6, and GM7. Long story short: if you call a script that doesn't execute a return statement, the value returned when the script terminates will equal the previously executed assignment, script return, or function return.

Proof of Concept:

1. Create a new Project.
2. Create a new Object.
3. Put this in the Object's Draw Event:

Expandv = 0;
n = 100;
v = nothing();
draw_text(x,y,string(v));

4. Create a new empty Script named "nothing".
5. Create a Room and put the Object in it.
6. Execute the Project.

It will display "100", not "0", or anything else. If the script terminates with "exit", it produces the same results.

7. Add this to the Script:

Expandrandom(100);

Notice: no assignment, no return.

8. Execute the Project.

It now displays random numbers.

This utterly bizarre phenomenon could make certain debugging scenarios a nightmare.

Interestingly, if you move "random(100);" from the Script to immediately after "n = 100;" in the Draw Event, it does NOT draw random numbers, it draws "100" as before. Something about script scope changes the priority of the value an undefined return takes: the previous assignment or the previous function return. Like I said: weird.


Abusing forum power since 1986.

Offline

#2 2008-03-08 02:31:07

Yourself
Member
Registered: 2007-10-09
Posts: 48

Re: return - unexpected behavior

I hate GML.  As a language it's just very poor.  If GM just used Python...

Offline

#3 2008-03-08 03:37:51

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

Re: return - unexpected behavior

Keep dreaming. Have you ever fooled with PyGame?


Abusing forum power since 1986.

Offline

#4 2008-03-08 11:59:45

Grand-High Gamer
Member
Registered: 2007-10-09
Posts: 18

Re: return - unexpected behavior

Keep in mind GM is for beginners.
It's like hating playdough for not being as good as proper modeling plasticine.


sigimage1.png

Offline

#5 2008-03-08 13:08:04

Yourself
Member
Registered: 2007-10-09
Posts: 48

Re: return - unexpected behavior

Keep in mind GM is for beginners.

What does that have to do with anything?  You don't have to make a bad language to make one that's easy to learn or use.  Python is a clear example of this.  It's easy to learn and use but still very powerful.

It's like hating playdough for not being as good as proper modeling plasticine.

What a stupid analogy.  Both substances are meant for completely different purposes and have little in common with a programming language.  Do beginning sculptors use play-doh?  No.  Besides, play-doh doesn't have any real shortcomings.  It does exactly what it's meant to do and it does it very well.  GML does what it was meant to do, but it doesn't do it well.  Big difference.

Have you ever fooled with PyGame?

I've looked at it, but never actually done anything with it.

Offline

#6 2008-03-09 02:40:34

Quimp
Member
Registered: 2007-10-09
Posts: 15

Re: return - unexpected behavior

A striking result. I was well aware that functions need to return a value, hence why the "return" keyword is sometimes unnecessary (bad programming practice) but I didn't know the returned value could have anything to do with assignments from a source outside a script. Ughh, indeed a weird behavior.

Offline

#7 2009-03-30 14:13:29

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

Re: return - unexpected behavior

It turns out this might not be so weird after all.

I was reading the documentation to a fascinating calculator app called Frink and ran across this:

Frink wrote:

If a function does not explicitly return a value, the value returned is the value of the last expression evaluated. A return statement with no value on the right-hand-side returns a special void type.

That certainly got my attention. More on the second part later.

Naturally, I hit up Google to find out where else this is true. Wikipedia has this say:

Wikipedia wrote:

Certain programming languages, such as Perl and Ruby allow the programmer to omit an explicit return statement, specifying instead that the last evaluated expression is the return value of the subroutine.

Huh. Perl and Ruby do it too. Interestingly Pascal/Delphi does not even have a return statement, although they support subroutines and can return values from them.

Now, about that void. GM will complain if you try to execute a return statement without an expression, in fact it won't even compile. But reading that last bit in the Frink docs did inspire me to try something. I created an empty script with no return. I created a room and for creation code I call the script and assign the returned to a variable. Bear in mind there is no "last evaluated expression" when the script terminates. The value returned fails both is_real() and is_string(). Is it a void? Is it useful? And where did I read this before? I swear someone in the GM community mentioned this somewhere recently.


Abusing forum power since 1986.

Offline

#8 2009-04-07 03:32:30

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

Re: return - unexpected behavior

Interesting finding, xot. It's come up in a few places at the GMC, most recently the suggestions topic, but I'm not sure if anyone entertaining the non-returning script idea knew about this special type -- I certainly didn't. I wish I could get on Windows without constant rebooting so I could toy with this.

Offline

#9 2009-05-31 17:19:21

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

Re: return - unexpected behavior

I have known of this null value for quite a long while (I think I knew it in GM5 days even), can't remember where exactly it came up though. I played around with it recently in this topic:
http://gmc.yoyogames.com/index.php?showtopic=419963

I don't think it is exactly very useful but could quite easily be used if wanted.

For the bug you found, it seems more of a gml quirk rather than a bug of sorts so I do not think it fits in with the other confirmed bugs to be moved. Though that said it could cause someone a headache if they do not know about it.. what do you think?

Last edited by flexaplex (2009-06-01 16:16:10)

Offline

#10 2009-05-31 19:27:29

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

Re: return - unexpected behavior

That's why I call it "unexpected behavior" rather than a bug. I think it's worthy of mention. Although it's not a bug, it can easily lead to bugs if this "feature" is misused. As I say a couple of posts up, it's not unique to Game Maker, it seems to be a computing philosophy, or an artifact of stack architecture, or some such technicality that I don't understand. I think it should be listed, but we can clearly state in the topic name and body that it is not truly a bug. There are some other examples of this sort of "not a bug; feature" thing in GM, but they escape my mind at the moment. I think the point of the bug forum/database should be to explain weird things that appear to be bugs, whether they are or not.


Abusing forum power since 1986.

Offline

#11 2009-06-01 16:57:05

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

Re: return - unexpected behavior

I think I would personally put confirmed things like this in a separate forum like "GM special behaviour" or maybe 'strange' or 'unexpected' would be a better word.

There are plenty of other things I can think of that could fit in such a forum though:

- GM indexes continue to increase and do not 'fill in gaps' after a resource is deleted
- how GM orders event in an object
- how GM orders creation events of different objects

- gml rounding to even integers when using round
- gml mod differing from mathematical definition
- gml string handling functions index start at 1 instead of 0
- gml doesn't use lazy evaluation
- gml assigns 0 to all arguments whether when they are not given as a parameter
- gml initialises all index in an array with 0 up to a given index if they are not initialised
- gml runs through all the currently read code before executing things like room changes or deactivation/destroying
- gml issue when not using semi-colon at end of line and then using id in a bracket like (100001). and gml parsing it as a function
- gml draw_text_ext not wrapping long words topic I just recently made
- gml show_message_ext thing that came up in advanced:  http://gmc.yoyogames.com/index.php?showtopic=424954
- gml uses > 0.5 to determine whether an expression is true (this is mentioned in the manual though), there is a strange quirk with this though that:

Expandif (0.5)
{
  show_message("This isn't shown");
}
else if !(0.5)
{
  show_message("This isn't shown either");
}

Now all these are technically not bugs, however they are all useful to know and are mostly not documented. As said not knowing some of them can cause headaches as the behaviour is different to what is expected but some of them are clearly aren't bugs at all, which is why I think a separate forum is better to group them.

Last edited by flexaplex (2009-07-04 17:53:35)

Offline

#12 2009-06-01 17:18:43

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

Re: return - unexpected behavior

Those are all great examples of what I'm talking about. Maybe there are enough of them to warrant a separate forum. Or they can just stay in GML Reference, since the original intent was to explain more clearly the parts of the manual that are unclear or misleading (and eventually use those topics to annotate the online copy of the manual I have on the main site). Since people might not look there when they are looking for what they think is a bug, maybe an "It's not a bug, it's a feature" index would be good to add to the bug forum.


Abusing forum power since 1986.

Offline

#13 2009-06-01 18:15:36

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

Re: return - unexpected behavior

xot wrote:

Since people might not look there when they are looking for what they think is a bug, maybe an "It's not a bug, it's a feature" index would be good to add to the bug forum

Yes that is a good title.

I think this is the best solution for the reason you just said. It is also better for people just generally browsing so they will be able to in-take all the useful knowledge from 1 place. The other advantage is that like the bugs they will then also be separated from unconfirmed/mistaken topics.

ps: I realise now this should of really been discussed in PM it is kind of spamming the topic.

Last edited by flexaplex (2009-06-01 18:43:37)

Offline

#14 2009-06-01 23:48:54

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

Re: return - unexpected behavior

No worries. I prefer an open discussion to get as many opinions in as possible. I would have split this off into another topic if I could have. I may later, I need to apply a mod to the forum software to do it.


Abusing forum power since 1986.

Offline

#15 2010-09-02 16:42:12

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

Re: return - unexpected behavior

A topic that might be about this issue popped up on the GMC forums. In it ParodyKnaveBob points out that the behavior has been changed in GM8.

GM8 Help File > What is New > Corrected bugs wrote:

• Calls to scripts and extension functions now return a value of 0 when they do not assign a return value themselves.


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB