• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

SOLVED Problems identifying specific object and thus changing its variables.

Lens

Member
Good morning/afternoon/evening! :potato:

I have many instances in a game called oBomber. Also I have an object called oTank, which has a range (collision rectangle). Once oBomber gets inside the oTank's collision rectangle, it should lose HP (to make it simpler, as for testing, now it just reduces down to 0). However, instead of just the specific oBomber losing its HP, all of the oBombers lose it. What should I do? (Code for better understand)

GML:
// This is at oBomber's "Create"
bomberHP = 6;

// And here is inside oBomber's "Step"
if self.bomberHP <= 0
{
    instance_destroy()    // this destroys itself
}

// And last but not least, inside oTank's "Step"

tankrange = collision_rectangle(x - 150, y - 60, x - 350, y + 30, oBomber, false, true);

if tankrange != noone
{
        oBomber.bomberHP = 0;   //This destroys all the bombers, subsequently
}
 

Slyddar

Member
I'd suggest reading the manual to see what collision_rectangle does, specifically what it returns.

It will return one instance id, so your code is looking if it returned one instance id, and then setting every single oBomber's hp to 0. You should look into collision_rectangle_list, and use the example code to only change the hp of the instances that were collided with.

Also you need to read about the difference between an instance and an object - https://forum.yoyogames.com/index.php?threads/whats-the-difference-objects-and-instances.29005/
 

Lens

Member
I'd suggest reading the manual to see what collision_rectangle does, specifically what it returns.

It will return one instance id, so your code is looking if it returned one instance id, and then setting every single oBomber's hp to 0. You should look into collision_rectangle_list, and use the example code to only change the hp of the instances that were collided with.

Also you need to read about the difference between an instance and an object - https://forum.yoyogames.com/index.php?threads/whats-the-difference-objects-and-instances.29005/
Alright, thank you!
 

ophelius

Member
Code:
inst = collision_rectangle(x - 150, y - 60, x - 350, y + 30, oBomber, false, true);

if(inst != noone)
{
       inst.bomberHP = 0;   //Only destroys the instance it collided with
}
oBomber.bomberHP = 0 would set that variable for all the oBomber objects
inst.bomberHP = 0 is the inatance the collision detected. the variable inst caries the instance ID.
Once you have the ID of an instance, you can reference any of its variables with the dot.
 

Lens

Member
Code:
inst = collision_rectangle(x - 150, y - 60, x - 350, y + 30, oBomber, false, true);

if(inst != noone)
{
       inst.bomberHP = 0;   //Only destroys the instance it collided with
}
oBomber.bomberHP = 0 would set that variable for all the oBomber objects
inst.bomberHP = 0 is the inatance the collision detected. the variable inst caries the instance ID.
Once you have the ID of an instance, you can reference any of its variables with the dot.
Thank you very much ophelius! The oBomber that touched it indeed got destroyed, and the one outside remained intact ^^
 
Top