• 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!

GML How does a function within the "with" function works?

M

monamona

Guest
I don't quite understand how this code from the docs work and wants to confirm it.

Code:
with (instance_nearest(x, y, obj_Ball)) { instance_destroy(); }
The docs say the code will destroy the instance of obj_Ball nearest to the instance running the code.
Does that means the x and y in the instance_nearest function refers to the instance that runs the whole code?
Can someone explain how this works? Can't you just use the if statement in this situation?
 

woods

Member
1. Does that means the x and y in the instance_nearest function refers to the instance that runs the whole code?
yes it is the instance calling the code x,y

2. Can someone explain how this works?
kinda ;o) but you already did... the code will destroy the instance of obj_Ball nearest to the instance running the code
say you are playing breakout/pong type game and you hit a "bad brick" it destroys the ball...
Code:
with (instance_nearest(x, y, obj_Ball)) { instance_destroy(); }
vs
Code:
if place_meeting(x,y,obj_ball1)
{
instance_destroy()
}
if place_meeting(x,y,obj_ball2)
{
instance_destroy()
}
if place_meeting(x,y,obj_ball3)
{
instance_destroy()
}
and what about multiple instances of ball1? it would destroy all ball1s in the room

ofc you could use something like
with other
{
// dostuff
}

what about powerups to ONE of your balls vs all of your balls... target the nearest one only


3. Can't you just use the if statement in this situation?
it can get kinda sloppy/messy trying to figure out which obj_ball you are trying to destroy,
checking say 10-20 balls for collisions.. vs find nearest... less resource intensive,and a bunch of if statements and whatnot.. this way is cleaner and faster.

====

i donno if this is entirely accurate, but makes sense in my head.... ish ;o)
 
M

MoncefFennich DZDev

Guest
With sta. means it in place of an object(all instances with the same object) or an instance if you want to do a code or script for this instance use that statment,
Code:
oPlayer.x=x;

//and it's like
with (oPlayer)
{
x=self.x;
}
We use self to refer to the user.
SORRY, I'M NOT GOOD IN ENGLISH
 
Last edited by a moderator:

woods

Member
space shooter game...
i have my player and powerup item

in my player step event
Code:
/// powerup splitshot
if place_meeting(x,y,obj_powerup_splitshot)
{
powerup_splitshot = true
with other
    {
    instance_destroy(obj_powerup_splitshot)
    }
}

what if i was dropping off armor for a space station instead... i dont want to powerup all 3 of my stations, just the one i am delivering to right..



in my player step event
Code:
/// powerup armor
if (carry_armor = true)
{
if place_meeting(x,y,obj_station)
    {
    powerup_armor = false
    with other
        {
        station.armor +=50
        }
    }
}
or
Code:
if (carry_armor = true)
{
if place_meeting(x,y,obj_station)
    {
    with (instance_nearest(x, y, obj_station)) 
    {
    station.armor+=50;
    }
}
 
M

monamona

Guest
1. Does that means the x and y in the instance_nearest function refers to the instance that runs the whole code?
yes it is the instance calling the code x,y

2. Can someone explain how this works?
kinda ;o) but you already did... the code will destroy the instance of obj_Ball nearest to the instance running the code
say you are playing breakout/pong type game and you hit a "bad brick" it destroys the ball...
Code:
with (instance_nearest(x, y, obj_Ball)) { instance_destroy(); }
vs
Code:
if place_meeting(x,y,obj_ball1)
{
instance_destroy()
}
if place_meeting(x,y,obj_ball2)
{
instance_destroy()
}
if place_meeting(x,y,obj_ball3)
{
instance_destroy()
}
and what about multiple instances of ball1? it would destroy all ball1s in the room

ofc you could use something like
with other
{
// dostuff
}

what about powerups to ONE of your balls vs all of your balls... target the nearest one only


3. Can't you just use the if statement in this situation?
it can get kinda sloppy/messy trying to figure out which obj_ball you are trying to destroy,
checking say 10-20 balls for collisions.. vs find nearest... less resource intensive,and a bunch of if statements and whatnot.. this way is cleaner and faster.

====

i donno if this is entirely accurate, but makes sense in my head.... ish ;o)
thanks. for your first answer, if i write other.x or other.y inside instance_nearest() does that even mean something for the instance_nearest() or will it return an error?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Can someone explain how this works? Can't you just use the if statement in this situation?
Think of the code like this:

Code:
var _inst = instance_nearest(x, y, OBJECT);
with (_inst) instance_destroy();
So, the function "instance_nearest" returns an instance ID of the nearest object, which (in the above example) we store in the a variable. We then use the "with" function to destroy that instance only. The example code in your post and in the manual does EXACTLY the same, only it cuts out the "var" part and works on the returned ID directly from the function.

thanks. for your first answer, if i write other.x or other.y inside instance_nearest() does that even mean something for the instance_nearest() or will it return an error?
In the context of the example code "other" means nothing. The other identifier is only useful when you are in a collision event, OR actually inside a "with". For example:

Code:
with (instance_nearest(x, y, OBJECT))
{
x = other.x;
y = other.y;
}
That code would set the nearest instance of OBJECT to the same x/y as the instance that is actually running the code. Does that make sense?
 
M

monamona

Guest
Think of the code like this:

Code:
var _inst = instance_nearest(x, y, OBJECT);
with (_inst) instance_destroy();
So, the function "instance_nearest" returns an instance ID of the nearest object, which (in the above example) we store in the a variable. We then use the "with" function to destroy that instance only. The example code in your post and in the manual does EXACTLY the same, only it cuts out the "var" part and works on the returned ID directly from the function.


In the context of the example code "other" means nothing. The other identifier is only useful when you are in a collision event, OR actually inside a "with". For example:

Code:
with (instance_nearest(x, y, OBJECT))
{
x = other.x;
y = other.y;
}
That code would set the nearest instance of OBJECT to the same x/y as the instance that is actually running the code. Does that make sense?
yeah, thank you!
 
Top