GML The return statement

Gamer (ex-Cantavanda)

〜Flower Prince〜
The return statement, what does it do?

Hiya! I've been busy learning GML recently, and if there's one thing I don't understand, and just can't find a clear answer for (manual, forums) it's what the term returning a value means, and what the return statement does. Could someone explain it in really simple vocabulary? (No programming terms)

This problem also makes other things unclear, like for example the difference between value++ and ++value is "returning it first", or I can't understand the modulo operator because it revolves around returning.

To where is it returning it? What does it return? And so on.
 

RangerX

Member
"Return" is mostly used when you code a function. Its for your function to actually return something. Just like "place_meeting()" returns 1 or 0, depending the 2 object were colliding or not. You shouldn't have to use it much outside programming functions.

"Modulo" is the remaining of a division. It's that simple:
5 mod 2 = 1
(5 divided by 2 = 2 and a remaining 1)
(2 x 2 +1 = 5)
 

Roderick

Member
Return is used in scripts to give the value back to whatever called the script.

For example, the following script:
Code:
/// script_add(first_num, second_num)
first_num = argument0;
second_num = argument1;
return (first_num + second_num);
If you later used the line script_add(5, 8); nothing would (visibly) happen. But if you used result = script_add(5, 8); the result (13 in this case) would be stored in the variable result.

It's just like when you store an instance id with variable = instance_create() or if (place_meeting()) (in the latter case, place_meeting is returning true or false to the if statement).
 

Tornado

Member
The term "returning" in the context of number++ has nothing to do with return statement itself. It should be understood as "gives".

Here "returning" means the value that is given to the expression where this number participates in.

for example:
var number = 1;
show_debug_message(number++);
show_debug_message(number);

is going to display following:
1
2

whereas

var number = 1;
show_debug_message(++number);
show_debug_message(number);

is going to display following:
2
2

That means in first case the value "returned/visible/accessible" to the show_debug_message will be 1 because number is increased after the whole "expression" is done.

In the second case increment will be performed as first and then the value of 2 is given/returned to the show_debug_message.

Or
var a = 10 + number++;
and
var a = 10 + ++number;

In first case the value of variable "a" will be 11, in second case 12. That means number++ "returns/gives" value 1 to the whole expression and once the whole expression is calculated, then number increases by 1.
In the second case before number takes part in any calculation, it is incremented first and then the value 2 is returned/given to the whole expression.
 
Last edited:
F

fxokz

Guest
Return is basically what a script does.
The easiest way to think of it is with a function we've all used.
Code:
instance_create(x,y, obj_enemy);
This function returns the id of the instance you tell it to create which means you can do some cool stuff like store the id in a variable eg:
Code:
inst = instance_create(x,y,obj_enemy)
Which means you can now use the variable to tweak the instance upon creation or whenever you feel.

For some reason ive had so much trouble when i was learning this stuff. Once you have it figured out it helps you sooo much with other things in gml
 

TheouAegis

Member
Also if you use GM:Studio 1 (I think they got rid of this in Studio 2), if you use exit inside a script, it will exit the entire event the script was called from. In all other versions of GM (maybe even Studio 2), the exit command will only exit the current code, not the event. So in Studio 1, you'd use return 0 to exit a script without exiting the event it was called in.

While you can think of the modulo as the remainder of a/b, it may help to see how it can be used to better understand it.

if n mod 1 == 0
This would tell you if value n is an integer (true) or fraction (false). So 1 would return true; 1.2 would return false.

n mod width
This one I use quite often. Let's assume you had a grid of stuff:

A B C D E F G H
I J K L M N O P

But instead of using a ds_grid or 2D array, you used a ds_list or 1D array or even a buffer. What column is N under? Since you're not using a ds_grid or 2D array, you can't just look up the index it's in. Inside our list/1D array/buffer, N is at index 13. Our "grid" is 8 characters wide. This means N is at 13 mod 8, which is equal to 5.
 
Top