• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Returning a function in a script? (SOLVED)

Fixer90

Member
After getting GMS 2, I noticed that one of the compatibility scripts (the one for instance_create) used return before its function. Here's the code for reference:
Code:
var d = object_get_depth(argument2);
return instance_create_depth(argument0, argument1, d, argument2);
What I'm wondering is, what difference does it make? What is the difference between returning a function in a script, and just putting it there?
 

Mr. RPG

Member
instance_create_depth returns the instance id of the instance that was created by the function so that it can be referenced later.

I assume you're familiar with with (instance)?
 

Fixer90

Member
instance_create_depth returns the instance id of the instance that was just created by the function.
You misunderstand my question.

I'm wondering what would happen if the script didn't have return in it, in comparison to it having return in it.
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
If it didn’t have the return how would it return the ID of the new instance?! (You are not returning a function, you are returning the return of a function)
 

mimusic

Member
It seems to me that you are unaware that instance_create_* does 2 things, not just 1:
  1. Creates a function an instance (such a silly typo I've made)
  2. Returns the ID number associated with that new instance you just created
For many, knowledge of the first result (creation) is usually all that's necessary. For users who want more control over and object they've created, however, that second result (ID number) is very important, because it grants direct access to the newly created object. For example:
Code:
var _obj = instance_create_depth( x, y, 0, oBullet );
_obj.speed = 4;
_obj.direction = direction;
This code uses the ID returned from instance_create_* to modify its speed and direction, which is something you'd often want to do when shooting bullets.

For your case, removal of the return statement would mean that you can no longer get that object's ID directly at creation time. Even if you don't need its ID, there's really no reason to remove it, and it's better to have it and not need it than vice versa.
 
Last edited:

Fixer90

Member
It seems to me that you are unaware that instance_create_* does 2 things, not just 1:
  1. Creates a function
  2. Returns the ID number associated with that new instance you just created
For many, knowledge of the first result (creation) is usually all that's necessary. For users who want more control over and object they've created, however, that second result (ID number) is very important, because it grants direct access to the newly created object. For example:
Code:
var _obj = instance_create_depth( x, y, 0, oBullet );
_obj.speed = 4;
_obj.direction = direction;
This code uses the ID returned from instance_create_* to modify its speed and direction, which is something you'd often want to do when shooting bullets.

For your case, removal of the return statement would mean that you can no longer get that object's ID directly at creation time. Even if you don't need it's ID, there's really no reason to remove it, and it's better to have it and not need it than vice versa.
Ah, thank you. That makes a lot of sense.

While I'm on the topic of scripts and how they return things, I'll ask another question:
Can scripts refer to variables within the object they will be put in freely? For example, let's say I have this script, which I plan to put in the Draw Event of my object(s):
Code:
draw_sprite(argument0, image_index, x, y);
Would the script refer to the object's image_index, x, and y variables that the script is in?
 

Nux

GameMaker Staff
GameMaker Dev.
Can scripts refer to variables within the object they will be put in freely? For example, let's say I have this script, which I plan to put in the Draw Event of my object(s):
Code:
draw_sprite(argument0, image_index, x, y);
Would the script refer to the object's image_index, x, and y variables that the script is in?
Yes, but I would personally consider that very bad practice (with a few niche exceptions like abstracting a whole system behind scipts, such as a custom animation system). You should really pass the image index and position as arguments instead of doing that.
 
FAO : Gamemaker Staff. Mimusic added a strikethrough 'a function'. However, when it was quoted it was not stricken through. Is this a bug or a feature?
 

Evanski

Raccoon Lord
Forum Staff
Moderator
FAO : Gamemaker Staff. Mimusic added a strikethrough 'a function'. However, when it was quoted it was not stricken through. Is this a bug or a feature?
Not excatly who your trying to summon with FAO : Gamemaker Staff. but yeah thats just how the quote system works with the forum framework
 
No, I understand. I did that because there were no staff members to quote. This was not something anyone but staff can address because of it's forum-related nothing to do with GML. I just wanted to know if this was portrayed as a feature or a bug. I may have quoted you by mistake. I'm sorry about that. Since we are here, feature or bug?

I apologize if I have broken forum etiquette. I'm not a socialized creature.
 
Last edited:
Top