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

SOLVED with(object)

Hi

I tried this:

with(id)
{
instance_destroy()
}

but that doesn't work obviously, because id is an instance, not an object. Is there some way that have that functionality with function instead of id?
 

CloseRange

Member
what are you trying to do exactly?
GML:
with(object_index) {
    // code
}
might be what you want.
using 'id' should delete whatever object called it
using object_index should delete all instances of the calling object (if obj_enemy called it then all obj_enemy's will be destroyed)
 

FrostyCat

Redemption Seeker
but that doesn't work obviously, because id is an instance, not an object.
Why would you assume that? with statements accept instance IDs too. The piece of code you showed destroys the current instance.

If you're trying to apply instance_destroy() to an instance ID returned by a function, just use that function as-is:
Code:
with (instance_place(x, y, obj_thing)) {
  instance_destroy();
}
 
Ok. Now when I move around there is a trail of meters.?

meter_percentage = ((hp/hp_max*100))*10

with (instance_place(x, y, HealthMeterObj100)) {
instance_destroy();
}
with (instance_place(x, y, HealthMeterObj80)) {
instance_destroy();
}

with (instance_place(x, y, HealthMeterObj60)) {
instance_destroy();
}

with (instance_place(x, y, HealthMeterObj40)) {
instance_destroy();
}

with (instance_place(x, y, HealthMeterObj20)) {
instance_destroy();
}

with (instance_place(x, y, HealthMeterObj0)) {
instance_destroy();
}

show_debug_message("hp/hp_max: " + string(hp/hp_max))
show_debug_message("HPTOTAL: " + string(hp/hp_max*100))
//instance_create_depth(x, y, -1600, HealthMeterObj100);

show_debug_message("Health is x: " + string(x));
show_debug_message("Health is y: " + string(y));
show_debug_message("Crown is x:" + string(x))
show_debug_message("Crown is y:" + string(y))
if (meter_percentage > 80)
{
instance_create_depth(x, y, -1600, HealthMeterObj100);
}

else if (meter_percentage > 60)
{
instance_create_depth(x, y, -1600, HealthMeterObj80);
}

else if (meter_percentage > 40)
{
instance_create_depth(x, y, -1600, HealthMeterObj40);
}

else if (meter_percentage > 40)
{
instance_create_depth(x, y, -1600, HealthMeterObj40);
}

else if (meter_percentage > 20)
{
 

Nidoking

Member
You're really determined to do this the hard way? Make a common parent like HealthMeterObj and just with (HealthMeterObj) instance_destroy();

As for why this isn't working, does the object that's running the code you posted have a sprite? I suspect that there's no bounding box for the instance_place to find anything, so none of the old meters are being destroyed.
 
You're really determined to do this the hard way? Make a common parent like HealthMeterObj and just with (HealthMeterObj) instance_destroy();

As for why this isn't working, does the object that's running the code you posted have a sprite? I suspect that there's no bounding box for the instance_place to find anything, so none of the old meters are being destroyed.
Yes. I'm notorious for doing things the hard way. :) :) And yes, the code I posted has a sprite.


As for why this isn't working, does the object that's running the code you posted have a sprite? I suspect that there's no bounding box for the instance_place to find anything, so none of the old meters are being destroyed
I admit I don't know that much about instance_place. Why none of the older meters being destroyed? Shouldn't it (with the inheritance) just delete the HealthMeterObj and move on. After that instance_destroy() is the following code:

if (meter_percentage > 80)
{
instance_create_depth(x, y, -1600, HealthMeterObj100);
}

else if (meter_percentage > 60)
{
instance_create_depth(x, y, -1600, HealthMeterObj80);
}

else if (meter_percentage > 40)
{
instance_create_depth(x, y, -1600, HealthMeterObj40);
}
LIke I said it leaves a trail of meters or also sometimes it just leaves one behind.
 

Nidoking

Member
I admit I don't know that much about instance_place. Why none of the older meters being destroyed? Shouldn't it (with the inheritance) just delete the HealthMeterObj and move on. After that instance_destroy() is the following code:
There's a whole document that can Help you learn about any GML function. People can offer you Help, but there's no better Help than the built-in Help.

I still believe that your instance_place calls are failing to return instances. How certain are you that the instance running this code will overlap the meter you want to destroy? And might there be more than one of each type? The code you posted would only delete the first one of each type that it finds.

And if you can get past your objection to doing sensible things, have you considered using different sprites for these meters instead of different objects? Or a single sprite with multiple frames? Do the meters have different functionality based on how much meter there is?
 
There's a whole document that can Help you learn about any GML function. People can offer you Help, but there's no better Help than the built-in Help.

Yeah the manual, right? Checked the manual. I'm not sure how the diference is between instance_place, position_meeting, and place_meeting. All I know is some of them return a Boolean and some return an instance.

I still believe that your instance_place calls are failing to return instances. How certain are you that the instance running this code will overlap the meter you want to destroy? And might there be more than one of each type? The code you posted would only delete the first one of each type that it finds.

So if It doesn't delete the previous meter, or one thing since its using with statement? That what would mean left over meters? That weren't deleted. Am I right?

And if you can get past your objection to doing sensible things, have you considered using different sprites for these meters instead of different objects? Or a single sprite with multiple frames? Do the meters have different functionality based on how much meter there is?
I did previously have a meter that was in the location but not the same functionality.
 

FrostyCat

Redemption Seeker
You need to learn the difference between objects and instances. Objects are types, and they have inheritance. Instances are individuals, and they don't have inheritance.

This destroys every instance of HealthMeterObj100:
GML:
with (HealthMeterObj100)
{
    instance_destroy();
}
This only destroys one colliding instance of HealthMeterObj100 if there is one:
GML:
with (instance_place(x, y, HealthMeterObj100))
{
    instance_destroy();
}
By the way, your code is leaving a trail because it is continually streaming out new instances of your health meter objects every step, but when they're no longer "lit", they are being removed only one at a time. The rate at which you produce far outstrips the rate at which you remove. Rather than manually managing the instances, you should just draw the bar sprites instead in the Draw event.
Code:
var meter_percentage =  ((hp/hp_max*100))*10;
if (meter_percentage > 80)
{
    draw_sprite(sHealthMeterObj100, 0, x, y);
}
if (meter_percentage > 60)
{
    draw_sprite(sHealthMeterObj80, 0, x, y);
}
if (meter_percentage > 40)
{
    draw_sprite(sHealthMeterObj60, 0, x, y);
}
if (meter_percentage > 20)
{
    draw_sprite(sHealthMeterObj40, 0, x, y);
}
Also, the instance_place() I cited is only one example of many sources that can return instance IDs. Read the Manual and find the right one for your needs, don't just take one isolated example and use it everywhere.
 
@FrostyCat I'm having trouble differentiate between types and instances because every other language that I know, even Ada which is considered strongly-typed, An object is an instance of a class, and may be called a class instance or class object; instantiation is also known as construction. Do you understand my confusion. I'm a Java and C++ programmer so I'm a little confused at times with GMLs terminology.

with (HealthMeterObj100) { instance_destroy(); }
Question: HealthMeterObj with the HealthMeterObj as a parent. Would that destroy everything inherited from HealthMeterObj?

with (instance_place(x, y, HealthMeterObj100)) { instance_destroy(); }
I understand this. You are picking one instance and destroying it.

I"ve read that damn manual so much its giving me chronic migraines.

(edit)

We FINALLY got Corona Virus testing in my county. 🤟
 
Last edited:
Question: (And I did read the manual on draw_sprite before I posted this)
with (HealthMeterParent) {
instance_destroy();
}

if (meter_percentage > 70 && meter_percentage <= 100)
{
draw_sprite(HealthMeter100, 0, x, y);
}
if (meter_percentage > 60 && meter_percentage <= 70)
{
draw_sprite(HealthMeter80, 0, x, y);
}
if (meter_percentage > 50 && meter_percentage <= 60)
{
draw_sprite(HealthMeter60, 0, x, y);
}
if (meter_percentage > 40 && meter_percentage <= 50)
{
draw_sprite(HealthMeter40, 0, x, y);
}
if (meter_percentage > 30 && meter_percentage <= 40)
{
draw_sprite(HealthMeter20, 0, x, y);
}
if (meter_percentage <= 20 && meter_percentage <= 30)
{
draw_sprite(HealthMeter0, 0, x, y);
}
It doesn't show the meter at all.
 
Top