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

Legacy GM How to destroy all colliding instances on command?

P

pepperjack22

Guest
Hi, Jack Here

I'm having trouble with a coding issue, I have three objects named Obj_Barrier_Trigger, Obj Barrier, and Obj_Gem.

in my game the player can pick up Obj_Gem and take it to an Obj_Barrier of their choice. Obj_Barrier_Trigger is basically a collision box around Obj_Barrier, but with a small buffer zone. the player will hit the trigger before the barrier.

the idea is that when the player collides with the trigger, the trigger will check to see if the player has a gem. and if so, the trigger will delete all barrier objects that it is in contact with. the problem, is that i cant even begin to comprehend how the trigger would be able to do this in the collision with Obj_Player event.

all i have at the moment is:

if global.Gem = 1
{
//do something here...
}

help?
 
P

Puhloo

Guest
You can create a variable on every trigger object in the create event like for example "triggered" and set it to false.
Once the player collides with the trigger object, you check if the player has the gem. If the player has the gem, set triggered to true.
Then in the trigger object, add a collision for it and the barrier object. Check if triggered is true, then destroy barrier.
 
P

pepperjack22

Guest
ah ok, that sounds like ti would work, out of curiosity, since the barrier blocks will be inside the trigger object does that mean the trigger object will be doing that check every frame? if so i hope that wont insite lag..
 
S

Snail Man

Guest
It seems like it would be more efficient to just do away with the trigger objects altogether, then check in the step event if the distance_to_object(player) < [some number], because collisions are so costly in general

Edit: or even better, make it so that in the player object, you check for nearby barriers, then if you have the gem, destroy the nearest one. This would cut down the operations-per-step dramatically
 
M

Mishtiff

Guest
Hello Jack,

My answer would be to initialize a variable in the barrier_trigger to hold a variable that links the instances you want destroyed. here is a small example...

trigger
-on create-
trigger_id = //different for each barrier. 1-10?

Then in your on collision with player:
if(global.Gem == 1){
with obj_Barrier {
if(trigger_id == other.trigger_id){
instance_destroy();
}
}
}

barriers
on-create
trigger_id = //match the id of the barrier you want to be its owner.

Hope this is a complete thought, im trying to play overwatch at the same time lol
 
Last edited by a moderator:
P

pepperjack22

Guest
i tried the first idea and the way i have it setup wouldnt quite let it work, ill give yours a shot mistiff. the game is set up kinda like a zelda game, all rooms are set to persistant, with what are essentially keys and locked doors, but gems and barriers instead. i need to be able to pick up a gem (completed) and walk up to a barrier. when close enough the barrier will remove the gem from the inventory (still not the problem) and delete itself and all connected barriers of the same color. so if i had a red, blue, and green, version of both barriers and gems. the red gem would only remove the red barriers i collide with if its on its own, but if there is a wall of red barriers, it would remove them all if i get close to one. and then the same for each color.

like i said, ill give the id thing a try but i would like a more modular system if possible so that i can focus more on making the levels and the puzzles in each room instead of having to change the creation code id's every time i change how i want a room to be.
 
use distance to object

Code:
//in the wall
if distance_to_object (obj_player) < 2 and global.gem = 1 //adjust distance as needed
{
glob = instance_nearest (obj_player.x,obj_player.y,obj_gem) // this might cause a conflict if you have a gem on the floor next to the one grabbed, you could just set up a global.gemid to store the id of the gem picked up

/\/\/\/\/
if glob.image_index = 1 and image index = 1 //presuming you line up so the wall and the sprites go in the same colour order
{
with (glob)
{
instance_destroy() //destroy gem being held
}
instance_destroy() //destroy wall
}
/\/\/\/\/
//
repeat the part inbetween the zigzag as needed for each of the three colours of gems matching each of the three colours of walls
}
and there you have it hope it helps and good luck
 
A

Aura

Guest
I agree with @Snail Man on this one. Don't overcomplicate things by using all of the objects, simply use the player object.

Keep different variables for different colours of gems. And when the player object is near the trigger object, destroy all the barriers of that type:

Code:
with (instance_place(x, y, obj_trigger))
{
   if (global.red_gem)
   {
      with (obj_redgem)
     {
         instance_destroy();
      }
   }
}
 
M

Mishtiff

Guest
alright, here is another suggestion...

You can store the objects that you want to destroy into variables. Lets say that you have 4 Barriers.

//note that the x and y must be found on the map, and then manually input here
//here I am assuming that your wall is horizontal
barrier[0] = instance_create(100, 100, obj_Barrier);
barrier[1] = instance_create(120, 100, obj_Barrier);
barrier[2] = instance_create(140, 100, obj_Barrier);
barrier[3] = instance_create(160, 100, obj_Barrier);

Then in your on collision with player:
if(global.Gem == 1){
for(var i = 0; i < 4; i++){
with barrier[ i ]{
instance_destroy(); //destroys each saved barrier​
}​
}
instance_destroy(); //destroys the trigger as well​
}

EDIT: I also greatly like the code Aura has above!
 
Top