what exactly does return?

First I used as programator in c#, this engine have the language pretty same. But something what I run from c# to gml was return, public and void... why I run from them, becouse I dont understand return, but unlucky I found it here... in game maker language!

So what exactly does do return, and what cases are for use him, in game maker studio(if you know in c# good, please explain in both languages)
 

TheouAegis

Member
If you write your own scripts, if you put return in them, the script will end immediately and whatever value you returned will be available to save immediately. For example, the function instance_position(x,y,obj) will return the ID of the instance found at (x,y). So you can save that returned value to a variable like so:

inst = instance_position(x,y,obj);

Another useful attribute of return is it will end the script but not the event the script is in. One of the changes between GM:8 and GM:S is in GM8 you can put exit in a script and it will end that script then continue running the rest of the event; in Studio, if you put exit in a script, it will end the script AND the event that the script is called from. So just putting return 0 instead of exit in a script will let you simply end the script without breaking the rest of the event.
 
P

ParodyKnaveBob

Guest
Howdy again, DB, $:^ J

The return keyword is good stuff. Did you try reading about it in the manual? Either way, I'll give a quick sample.

You know how instance_create() works, right? It's a built-in function which creates an instance using these three arguments:
argument0 -> x
argument1 -> y
argument2 -> object_index
But, it doesn't simply create an instance and then leave you hanging. It also returns the id of the newly created instance. This way you can go...

id_bullet = instance_create(x, y, o_bullet);
id_bullet.image_blend = irandom($ffffff);

...which stores the returned id in a variable (id_bullet), then uses that variable to access that instance to change its image_blend to some random color.

Same with choose() returning whatever random option and max() returning the highest value.

You can create scripts that do this same thing -- scripts which return a value!

Code:
/// value = scr_random_element(array);

var _arr_input = argument0,
    _index_random;

if (!is_array(_arr_input) {
  return undefined; // NOTE: the script STOPS running if it hits this line!
}

_index_random = irandom(array_1d_length(_arr_input));

return _arr_input[_index_random];
So now you can plug any (1D especially) array into your new script and it'll output some random value from it...

// after setting my_awesome_array, such as in a Create Event
my_new_something_something = scr_random_element(my_awesome_array);
if (scr_random_element(my_awesome_array) == BEANS) { /* do stuff */ }
draw_text(x, y, string(scr_random_element(my_awesome_array)); // which will show a new random element from the array every drawing frame
// etc. !

Is it clear after all this time, or was the example too abstract/whatever?
Bob
 
P

ParodyKnaveBob

Guest
(to complement what @Aura said)
...much how using draw_text() does something without returning any value. (You can't really use variable=draw_text(); for example.)

I've never used C#. I've used Javascript and PHP, though, both of which return values. I'd 1. look at whatever documentation came with your C# stuff and see how it compares to what GM's manual and we are saying about return in GML -- and 2. try some experiments in both GML and C# concerning return. Just note, though, this is a GM forum (the GM forum, really,) thus asking about C# might belong more in the Off-Topic section.

I hope this helps,
Bob $:^ J
 
In case anyone is curious, if you capture the value of a script call that doesn't utilize the 'return' keyword, the script will have a return value of '0'. Just recently tested this myself, as I needed to know for a system I was writing. Not sure if built in functions do the same.
 
I

IndieGameMaker

Guest
Return statements are a language construct that jumps back to the address in memory where the function was invoked. When the return statement executes, if there is a value to return it leaves the value on the top of the function stack to be retrieved after the jump. Every function must have a return statement, even void functions, to avoid undefined behavior caused by executing code in higher memory addressed beyond the memory addresses used by the current function. However, most languages implicitly generate the return statements automatically behind the scenes if one isn't provided to avoid such problems, so it is not necessary to add a return statement to a void function but you could technically add one if the language syntax allows it and it is semantically correct.
 
Top