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

GML Help with instance id from collision event please

S

shrunkenmaster

Guest
Trying to get to grips with instance id, but struggling a bit...
How can I instantly destroy my player (oRay) from this collision event? At the moment it doesn't get destroyed until after the alarm that destroys the dead player (oRaydead). Can I call oRay_id ? Everything I've tried so far has not worked :(

oPlayer (create)
Code:
/// @description initialise player
var _oRay = instance_create_layer (240, 320, "Ray", oRay);
oRay_id = _oRay;
oRay (oBuzz collision event)
Code:
raydeath = true;
instance_destroy (other);

var _raydead  = instance_create_layer (x,y,"Ray", oRaydead);
oRaydead_id =_raydead;
alarm [0] =60;

if (lives >0) {
   lives -=1
   alarm [1] = 60;
   }
   
else if (lives=0) {
   var _gameover = instance_create_layer (0,0,layer,oGAMEOVER);
   }
Alarm 0
Code:
instance_destroy(oRaydead_id);
Alarm 1
Code:
room_restart();
 

samspade

Member
Yes. But I have a couple questions. What do you mean by player? The code you posted has oRay being created by oPlayer. So oRay can't be your player or oPlayer isn't your player.

I never use collision events and I can't remember exactly how they work. Do they run inside the object with the event, or with the object colliding? I bring this up because you are only destroying one thing with instance_destroy(other). If other in a collision event refers to oRay then it is being destroyed instantly. If it appears to not be destroyed instantly then you have some other code interfering.

Also, it's not necessary to create a temporary variable and then assign it to an instance variable. You can just do:

Code:
oRaydead_id = instance_create....
 
S

shrunkenmaster

Guest
Yes. But I have a couple questions. What do you mean by player? The code you posted has oRay being created by oPlayer. So oRay can't be your player or oPlayer isn't your player.

I never use collision events and I can't remember exactly how they work. Do they run inside the object with the event, or with the object colliding? I bring this up because you are only destroying one thing with instance_destroy(other). If other in a collision event refers to oRay then it is being destroyed instantly. If it appears to not be destroyed instantly then you have some other code interfering.

Also, it's not necessary to create a temporary variable and then assign it to an instance variable. You can just do:

Code:
oRaydead_id = instance_create....
Hi Sam, thanks for the reply. I guess this whole problem begins with me having trouble with instance_create_layer. If I use the following in the create event I get a recursion depth error:
Code:
var _oRay = instance_create_layer (240, 320, "Ray", oRay);
oRay_id = _oRay;
So I created the oPlayer object with only that code, which seemed to work. I feel this is the root of the problem...
 

samspade

Member
Most likely. I'm still not sure I fully understand, but it seems like you have two versions of oRay (or more accurately one version of oRay and one version of oPlayer which is identical to oRay). One of which is being destroyed, the other isn't. You could check this by running the debugger (F6) and either turning on live debugging or pausing it and checking the instance tab.

If oPlayer is the exact same as oRay and you place a version of oPlayer in the room (or create it with code) and then in oPlayer's create event have oPlayer create oRay (which is the same thing with a different name) both of them will exist.

The reason you would have a recursion depth error in your first case is that if you have oRay and in oRay's create event you create another oRay, then in that oRay's create event it will create another oRay, and in that oRay's create event it will create another oRay, and in that oRay's create event it will create another oRay, and in that oRay's create event it will create another oRay, and in that oRay's create event it will create another oRay, and so on forever - GameMaker detects this and gives you an error message.

Separate oRay into oPlayer and oRay breaks the chain - as oRay doesn't create any more oRays - but you still have two.

So you should only have one, and it should likely be oRay. How does oPlayer get in and why not just substitute that with oRay?
 
S

shrunkenmaster

Guest
OK, If i delete the oPlayer object completely and have the "var _oRay = instance_create_layer (240, 320, "Ray", oRay); " in oRay, i get the error. How else do I get oRay in?
 

samspade

Member
Post your new code.
  • What error do you get?
  • How are you putting oRay in (e.g. room editor or by code)?
 
S

shrunkenmaster

Guest
Just realised I had placed oRay in the room editor as well, what an idiot! Anyway, game runs now, but oRay not showing up. What else do I need to do with the instance_create_layer part?

Code:
spd = 2; //player speed
global.carrying=false; //player not carrying
trondir = 0; //player bullet direction
raydeath = 0;
firingdelay = 0;
moveX = 0;
moveY = 0;
timer = room_speed *3; // in seconds
flash = 0 // set flash variable


var _oRay = instance_create_layer (240, 320, "Ray", oRay);
oRay_id = _oRay;
 

samspade

Member
If you're placing oRay in the room with the editor you should not be creating oRay in the create event.

If oRay isn't showing up that's an entirely different probably. Could be that it isn't getting created, could be that it isn't being drawn. Could be that it is being created then immediately destroyed. You'd probably need to run the debugger and see whether it exists to start with.
 
S

shrunkenmaster

Guest
I've already deleted the oRay placed in the room editor, so there's just the one instance_create_layer in the oRay create event. Ran the debugger, not stopping at anything, everything looks fine except no oRay.
 

samspade

Member
I've already deleted the oRay placed in the room editor, so there's just the one instance_create_layer in the oRay create event. Ran the debugger, not stopping at anything, everything looks fine except no oRay.
Code only runs in objects that exist in the room. So no code in the oRay object in your resource tree runs until you place it in a room. And as noted above you should not be creating another instance of oRay inside of oRay's create event or it will create an infinite regression. Remove the code that creates oRay from the create event and place a single instance of oRay in the room.

It might be worth going through this tutorial series: https://www.youtube.com/playlist?list=PLhIbBGhnxj5I8HuwoNhjgjDC8OQIf8D3Z. Watching it takes about an hour, doing it along with probably is twice that, but it would might save a lot of time in the future by (possibly) preventing issues like this.
 
S

shrunkenmaster

Guest
Ok, got it now! Thank you so much for your time, I’ll make those videos my next step.
 
Top