• 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 Function call in other instance, without "with"?

Miradur

Member
Hi, I can't remember, but I may have seen it here in a user's reply.
Can you let another instance call a function without using the "with" statement?
Something like:

other.lengthdir_x(x1, y1, x2, y2) or
obj_Enemy(lengthdir_x(x1, y1, x2, y2)) or
(lengthdir_x(x1, y1, x2, y2).obj_Enemy)

Is there a way to do this or am I just imagining it?

br,
Miradur
 
A

Alex_Beach

Guest
kinda, you can do: enemy.variable = lengthdir(). What really matters is what you throw into the function and who you save it to, not who calls it. If you need local variables put into it, it's better to use "with"
 

Joe Ellis

Member
You defininely can't do other.lengthdir_x(90, 20), it would be good if you could,

but any of those things actually you can't do,

obj_Enemy(lengthdir_x(x1, y1, x2, y2)) - you can't put a function in brackets like that belonging to something.

lengthdir_x(x1, y1, x2, y2).obj_Enemy - and that would mean that lengthdir_x is an object and has a variable called obj_Enemy, which wouldn't be allowed if there was an object called that.

Sorry if I seem like a grammar police kind of thing, but I'm just trying to help with the basic reading of the language
 

Miradur

Member
Thanks for your answers, but it's not quite what I saw, that was just a construct with clamps.
Anyway, I didn't want to question GM, I just wanted to know what was possible :).

Miradur
 

Morendral

Member
I'm not sure exactly what you are trying to do, but you could do something like this

oEnemy.x = oEnemy.x + lengthdir_x............

You could also replace oEnemy with the numbered id of the specific instance you wish to change. Without knowing what exactly you want to do it's hard to help further
 

TheouAegis

Member
Well considering you tried to pass four arguments when it only takes two... Obviously you aren't even thinking of the right function. Lol

You can snag the ID of the object and append that ID to every argument you pass to whatever function as long as the argument you pass can actually receive an id modifier. As was said, functions themselves cannot receive ID modifiers, but variables can. A function can be passed as an argument and that function which is passed as an argument can have variables that have been modified by an ID passed in as arguments so that the value returned from that function to the primary function's argument will in essence be derived from the instance whose ID was used to modify the variables.

draw_sprite(a.sprite_index, a.image_index, a.x+lengthdir_x(a.sprite_width, point_direction(a.x, a.y, x, y)), a.y)
 
Well considering you tried to pass four arguments when it only takes two... Obviously you aren't even thinking of the right function. Lol

You can snag the ID of the object and append that ID to every argument you pass to whatever function as long as the argument you pass can actually receive an id modifier. As was said, functions themselves cannot receive ID modifiers, but variables can. A function can be passed as an argument and that function which is passed as an argument can have variables that have been modified by an ID passed in as arguments so that the value returned from that function to the primary function's argument will in essence be derived from the instance whose ID was used to modify the variables.

draw_sprite(a.sprite_index, a.image_index, a.x+lengthdir_x(a.sprite_width, point_direction(a.x, a.y, x, y)), a.y)
Does anyone know if this is slower than just doing a with? I always imagine something like

draw_sprite(a.sprite_index, a.image_index, a.x+lengthdir_x(a.sprite_width, point_direction(a.x, a.y, x, y)), a.y)

Being slower than

draw_sprite(sprite_index, image_index, x+lengthdir_x(sprite_width, point_direction(x, y, other.x, other.y)), y)

So I use a lot of with's.
 

Simon Gust

Member
Does anyone know if this is slower than just doing a with? I always imagine something like

draw_sprite(a.sprite_index, a.image_index, a.x+lengthdir_x(a.sprite_width, point_direction(a.x, a.y, x, y)), a.y)

Being slower than

draw_sprite(sprite_index, image_index, x+lengthdir_x(sprite_width, point_direction(x, y, other.x, other.y)), y)

So I use a lot of with's.
Depends.
If there are a lot of this instance, then writing dot-operators is going to be slower than using a with statement.
Slight improvement though,
Code:
var xx = x;
var yy = y;
with (oEnemy)
{
    draw_sprite(sprite_index, image_index, x+lengthdir_x(sprite_width, point_direction(x, y, xx, yy)), y);
}
Local variables persist through with() statements and are clearly faster than instance variables + dot-operators.
 

Miradur

Member
I'm sorry, I just wrote my example there like a beginner you should always have a look into the help
before you write a command here. Since my English is practically absent, I may ask the wrong question.
But I think I read here in the forum, the possibility of such a function call in one of the answers, in a thread.

Sorry to have caused you so much trouble.

Miradur

Ps.: another question, where can I mark the thread as solved :)
 
Last edited:
Top