• 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 Time based collision [RESOLVED]

C

Chickan

Guest
Hello everyone,

I'm brand spanking new to Gamemaker and am starting out bu making a simple space shooter game.

Question:

Is there a way to create an instance of an object but set a timer to prevent that object being able to collide with other objects?

Context:

The player controls a spaceship obj_ship. When they press space bar it creates an instance of obj_laser

Code:
laser = instance_create(x,y,obj_laser);
This is then adjusted to match the angle and direction of the current object (your ship) and then given a speed

Code:
laser.image_angle = image_angle;
laser.direction = image_angle;
laser.speed = 8;
This works fine and, with collision events on the targets, is behaving as expected.

However I have made the game in such a way that, if you miss your target, obj_laser wraps (h & v) when outside the room. In other words I want it to just fly forever until it hits something.

This works for all objects except my ship. I wanted to set a collision event for your ship so that, if you miss your target and the bullet loops around the edge of the screen, you can potentially hit yourself.

Problem is that the obj_laser instance is created at the exact position of the ship meaning that, as soon as it's created, it collides with the player ship. Layer wise it's under the ship so, as it starts to move, it looks like it comes out the cannon at the front - so I don't really want to offset the xy position to create the instance outside the bounds of the ship's hitbox.

Is there a way to delay collision detection for an object? Or can anyone advise a way I might achieve what I'm after?
 

rIKmAN

Member
Set an alarm when the bullet is created, and wrap the player/bullet collision code in a check for whether this alarm has fired.

If it has, run collision code, if not don't.
 
C

Chickan

Guest
Are alarms tied to objects or global?

I.e. I would create alarm0 on obj_laser and then, when a collision is detected with that ship, it would check the other object to see if the alarm had triggered?

So, on the obj_laser:



Then on the obj_ship something like this?


 
Last edited by a moderator:

rIKmAN

Member
Something like...

Bullet Create
Code:
active = false
alarm[0] = 30 // 1sec using a room speed of 30
Bullet Alarm0 Event
Code:
active = true
Bullet Collision Code
Code:
if (active == true)
{
    // run collision code
}
else
{
    // bullet not active yet, don't run collision code
}
Basically just do exactly what you are doing now, but you are checking if the bullet is active first before you allow it to blow up the ship. It doesn't become active until it's 1sec alarm has fired, by which time it will be away from your ship - obviously 1sec is too long but it's just an example.
 
Last edited:
C

Chickan

Guest
Genius! Thank you so much!

My laser collision event was on the ship itself (as I run it for objects that are being hit by the laser, not the laser object itself) but it worked perfectly! :)
 

rIKmAN

Member
Are alarms tied to objects or global?

I.e. I would create alarm0 on obj_laser and then, when a collision is detected with that ship, it would check the other object to see if the alarm had triggered?

So, on the obj_laser:



Then on the obj_ship something like this?


It didn't have the pics when I posted, but in the obj_spaceship_obj_laser1 you might have to get the instance of the bullet to check its alarm.

I don't use DnD (sounds silly but it confuses me lol, I prefer plain code) but that might be checking alarm[0] of the ship?
It might not, as I said I don't really understand how the DnD stuff does things, but keep it in mind as something to look at if it isn't working - set the alarm to 1000 and show_debug_message the contents and see if it has the right value etc.

Better wait til someone who uses DnD can confirm, but you are definitely on the right track!

Edit:
Awesome, congrats and glad to help :)
 
C

Chickan

Guest
I too prefer code to DnD however I figured I could eliminate incorrect code as a cause of my problem by using it for the basics :)

All code now though.
 
Top