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

How do I destroy specific blocks?

O

olson

Guest
I've started using GM:S very recently, and I wanted to implement one mechanic to my game, but I have no idea how to do this.
So basically it works like that:
When you push one of few the buttons in a room, some of the blocks in the room disappear.
To make it easier to understand what I'm going for I've made a drawing, which represents the layout of my room.
The green block with "S" on it when touched destroys the green block, while the red block with "S" on it destroys the red block.
http://imgur.com/LL10h2d
I would by really thankful if somebody could help me with making this mechanic work, thanks in advance.
EDIT: I've forgotten to mention that the player starts right under the red block.
 
I would do this by creating a block object, a wall object, and a macro to define what type of walls are related to a block object.

So, what I am going to outline will relate directly to your drawing, and should be able to scale up fine.

In your default macros add:
Code:
BLOCK_COLOR_GREEN with value 0
BLOCK_COLOR_RED with value 1
Now, create a object, let's call it obj_block_parent, with the following in the create event:
Code:
BlockColor = noone;
Now, I would create two child block objects, one called obj_block_red and the other obj_block_green, and they both inherit from obj_block_parent.
In the obj_block_red create event:
Code:
event_inherited();

//overrides
BlockColor = BLOCK_COLOR_RED;
In the obj_block_green create event:
Code:
event_inherited();

//overrides
BlockColor = BLOCK_COLOR_GREEN;
Now I would do something exactly the same for wall objects. Creating a obj_wall_parent, obj_wall_red, obj_wall_green, and assigning them their respective colors to a variable.
obj_wall_parent's create code:
Code:
ParentBlockColor = noone;
obj_wall_red's create code:
Code:
event_inherited();

//overrides
ParentBlockColor = BLOCK_COLOR_RED;
I would do the same thing for obj_wall_green, just assigning BLOCK_COLOR_GREEN to its ParentBlockColor variable.

Now, in the obj_block_parent's left click event, I would add:
Code:
for(var i = 0; i < instance_number(obj_wall_parent); i++){
  var _wall = instance_find(obj_wall_parent, i);
  if(var_wall.ParentBlockColor == self.BlockColor){
    with(_wall){
      instance_destroy();
    }
  }
}
Hope this helps.
 
An improved version of the destroying code using with:
Code:
with (obj_wall_parent) {
  if (ParentBlockColor == other.BlockColor) {
    instance_destroy();
  }
}
We could even take it a step further and just make a script called scr_get_wall_obj_by_color:
Code:
///scr_get_wall_obj_by_color(color)
switch(argument0){
  case BLOCK_COLOR_RED:
  return obj_wall_red;

  case BLOCK_COLOR_GREEN:
  return obj_wall_green;
}
Then our obj_block_parent click method could just look like:
Code:
with(scr_get_wall_obj_by_color(self.BlockColor)){
  instance_destroy();
}
 

TheouAegis

Member
Or just set the target wall object in the block object, get rid of the parent wall and make the red wall the parent of the green wall, get rid of the parent block and make the red block the parent of the green block, and do away with the macros completely.

obj_wall_red
obj_wall_green (parent = obj_wall_red)
obj_block_red
obj_block_green (parent = obj_block_red)

obj_block_red's Create Event:
wall = obj_wall_red;

obj_block_green's Create Event:
wall = obj_wall_green;

obj_block_red's code for however it gets activated:
with wall instance_destroy();
 
Or just set the target wall object in the block object, get rid of the parent wall and make the red wall the parent of the green wall, get rid of the parent block and make the red block the parent of the green block, and do away with the macros completely.

obj_wall_red
obj_wall_green (parent = obj_wall_red)
obj_block_red
obj_block_green (parent = obj_block_red)

obj_block_red's Create Event:
wall = obj_wall_red;

obj_block_green's Create Event:
wall = obj_wall_green;

obj_block_red's code for however it gets activated:
with wall instance_destroy();
The point of parent objects is so it can scale well.
While a wall may be defined by its color, there are certain properties that are probably similar to each wall.
If we have 50 different types of walls and we want to make a change apply to all of them, then it might be a bit messier to go through all 50 of them and apply the same change.
 

TheouAegis

Member
I didn't say don't use a parent, I said just set one of the existing objects as the parent of the other. Now granted, your point is valid and I should add that the object with the least amount of event codes should be the one designated the parent. Based on the OP, there currently isn't any issue with one object having too much particular code to be a parent.

If you want a change to apply to all of the walls, make that change in obj_wall_red (in my example), since that would be the parent of the other walls.
 
Top