• 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 to destroy/remove an object by killing something?

pixeltroid

Member
Scenario 1:
I've made a barrier object in room A which blocks the players path. The barrier can only be destroyed if a boss is killed in room B. Once the boss is killed, the player has to backtrack to room A, and pass through.

Scenario 2:
The barrier and the boss are in the same room. And again I need to kill the boss in order to remove the barrier.

I want to incorporate both scenarios in my project. I'm guessing the code to perform these actions goes into the bosses step events?

What should I do?

The objects I am working with are : obj_player, obj_boss, obj_barrier.
Room names : room1, room2
 
Last edited:
T

TDSrock

Guest
What I would do is add in a always persistent object. Lets call it "controller"

in controllers create event I would do:
Code:
blockA = false;
blockB = false;
then when the boss in scenrio 1 dies I say before the boss dies:
Code:
//bos death
   controller.blockA = true;
Then in the blockś step event I would have this:
Code:
if(controller.blockA){
   instance_destroy();
}
Do the same for any other block. Due to our controller objects persistence it will never be removed and is essentially a global value as a result.

Why not use a global instead?
Using a global will work just fine, the thing is that I don't know how gamemaker treats globals in ALL scenario's as a result I'd rather have an object of which I know more what GM does with it.

EDIT: if you want to keep reusing the same barrier object you can simply add in the create event in the room editor of the object an ID that for the barrier(s) and bind that to the controller object.
 
A

Aura

Guest
I'd personally go with using global variables (or a global array if you've got a large number of boss-barrier combinations) that are defined in the creation code of a boot room.

Code:
global.boss_one_dead = false;
global.boss_two_dead = false;
room_goto(room1); //Go to the room that you want your game to "start" in
Upon victory, I'd set the related variable to true and run this in the Step event of the control object:

Code:
if (global.boss_one_dead) {
   with (obj_barrier) {
      if (my_boss == 1) {
          instance_destroy();
      }
   }
}
...where my_boss tells the number of boss upon whose defeat, you want the barrier instance to be destroyed; define it in the creation code of each barrier instance.
 

pixeltroid

Member
What I would do is add in a always persistent object. Lets call it "controller"
Ok. So I need to make a new object and call it obj_controller?

in controllers create event I would do:
Code:
blockA = false;
blockB = false;
then when the boss in scenrio 1 dies I say before the boss dies:
Code:
//bos death
   controller.blockA = true;
Then in the blockś step event I would have this:
Code:
if(controller.blockA){
   instance_destroy();
}
Do the same for any other block. Due to our controller objects persistence it will never be removed and is essentially a global value as a result.

Why not use a global instead?
Using a global will work just fine, the thing is that I don't know how gamemaker treats globals in ALL scenario's as a result I'd rather have an object of which I know more what GM does with it.

EDIT: if you want to keep reusing the same barrier object you can simply add in the create event in the room editor of the object an ID that for the barrier(s) and bind that to the controller object.
Ok, I'm going to try that code out now. Let's see what happens!
 

pixeltroid

Member
What I would do is add in a always persistent object. Lets call it "controller"

in controllers create event I would do:
Code:
blockA = false;
blockB = false;
then when the boss in scenrio 1 dies I say before the boss dies:
Code:
//bos death
   controller.blockA = true;
Then in the blockś step event I would have this:
Code:
if(controller.blockA){
   instance_destroy();
}
Do the same for any other block. Due to our controller objects persistence it will never be removed and is essentially a global value as a result.

Why not use a global instead?
Using a global will work just fine, the thing is that I don't know how gamemaker treats globals in ALL scenario's as a result I'd rather have an object of which I know more what GM does with it.

EDIT: if you want to keep reusing the same barrier object you can simply add in the create event in the room editor of the object an ID that for the barrier(s) and bind that to the controller object.

Wait I did what you posted and I'm getting an error message!

Variable obj_barrier.controller(100016, -2147483648) not set before reading it.
at gml_Object_obj_barrier_StepNormalEvent_1 (line 1) - if(controller.blockA)

what did you mean by "blockA"? Was that supposed to be the naem of my barrier object>?
 

pixeltroid

Member
@Aura

Code:
global.boss_one_dead = false;
global.boss_two_dead = false;
room_goto(room1); //Go to the room that you want your game to "start" in
^ Where does this code go into?

Upon victory, I'd set the related variable to true and run this in the Step event of the control object:

Code:
if (global.boss_one_dead) {
   with (obj_barrier) {
      if (my_boss == 1) {
          instance_destroy();
      }
   }
}
Is that the complete code for the control object step event?

...where my_boss tells the number of boss upon whose defeat, you want the barrier instance to be destroyed; define it in the creation code of each barrier instance.
Can you give me an example? like, what do I need to type into the creation code?


I'm not sure I understood. It would be helpful if you could please specify exactly what objects step / create events I need to paste those codes into?

My objects are named as the following:

obj_boss
obj_barrier
obj_barriercontroller

Can I get a step-by-step instructions please? :/
 
Last edited:

jo-thijs

Member
Wait I did what you posted and I'm getting an error message!

Variable obj_barrier.controller(100016, -2147483648) not set before reading it.
at gml_Object_obj_barrier_StepNormalEvent_1 (line 1) - if(controller.blockA)

what did you mean by "blockA"? Was that supposed to be the naem of my barrier object>?
No, blockA is a boolean variable of obj_controller, which indicates whether barrier A should be destroyed.

The error message is telling you that "controller" is not defined inside obj_barrier.
controller should have been obj_controller instead.
Also, make sure the persistent checkbox is checked in obj_controller.

o_O

I'm not sure I understood. It would be helpful if you could please specify exactly what objects step events I need to paste those codes into?

My objects are named as the following:

obj_player
obj_boss
obj_barrier
obj_barriercontroller
Global variables are variables that are shared by all objects and every instance.
You use global variables by putting "global." in front of your variable name.

Basically, you have a global variable that keeps track of whether barrier A has to be destroyed.
You set it to false at the start of your game.
When the boss is defeated, you set it to true.

Then you have some object check this global variable every step.
If the variable is true, you destroy every barrier object.

The exact codes you should use for this are already given.

Code:
global.boss_one_dead = false;
global.boss_two_dead = false;
room_goto_next();
This goes into the room creation code (can be found when editing a room) of the first room in your project (which should be empty).

Code:
if (global.boss_one_dead) {
   with (obj_barrier) {
      if (my_boss == 1) {
          instance_destroy();
      }
   }
}
This goes in the step event of obj_barriercontroller (which should either be persistent or be placed in every room you've got barrier A in).

Code:
global.boss_one_dead = true;
And this would be executed whenever the boss dies.
This might be in the destroy event of obj_boss.


I personally, would advice to do this though:
Room creation code of first empty room in the project:
Code:
global.boss_defeated = false;
Create event of obj_boss:
Code:
if global.boss_defeated
    instance_destroy();
Destroy event of obj_boss:
Code:
global.boss_defeated = true;
with obj_barrier
    instance_destroy();
Create event of obj_barrier:
Code:
if global.boss_defeated
    instance_destroy();
 

pixeltroid

Member
@jo-thijs

Hi. I used your method and it worked!

However, there is one problem - killing the boss object caused EVERY barrier object in the level to disappear. I need to link the death of the boss with one particular barrier object.

For the next barrier that shows up as I progress in the game, I would need to kill something else. How do I make that work?


Should I name the barriers differently?
 
T

TDSrock

Guest
When placing the barrier in the room in the editor, right click it and edit it's creation code. Add an ID of some sort there and add in that only certain ID's look for certain boss deaths. (just like my original post said)
 

pixeltroid

Member
When placing the barrier in the room in the editor, right click it and edit it's creation code. Add an ID of some sort there and add in that only certain ID's look for certain boss deaths. (just like my original post said)
ok. So I opened the creation code window of the barrier in the room editor.
What should I type in to add the ID?
 

pixeltroid

Member
ok. So I opened the creation code window of the barrier in the room editor.
What should I type in to add the ID?
When placing the barrier in the room in the editor, right click it and edit it's creation code. Add an ID of some sort there and add in that only certain ID's look for certain boss deaths. (just like my original post said)
How do I do all that in code?

Also, am I supposed to combine your codes with jo-thijs?
 
T

TDSrock

Guest
Jo-thijs wrote out what I told you to do originally but excluded the ID's section.

Adding the id is as simple as saying ID = whatever integer you want
 

pixeltroid

Member
Jo-thijs wrote out what I told you to do originally but excluded the ID's section.

Adding the id is as simple as saying ID = whatever integer you want
if you want to keep reusing the same barrier object you can simply add in the create event in the room editor of the object an ID that for the barrier(s) and bind that to the controller object.
Ok. How do I bind it to the controller object? I'm sorry, but I really need these things spelled out! :-/ I know next to nothing about how coding works

Jo-thijs method seems to be working without a controller object. So am I supposed to make one now?
 
T

TDSrock

Guest
I know next to nothing about how coding works
Truly here, my advice. Start learning.

His method works through globals. My method would work the same by making a controller object which is essentially going to hold values just like the global would. There is no difference besides Globals vs an instance.
 
Top