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

GameMaker How do I set an alarm for every Instance of an Object individually?

M

moichelm

Guest
Hey Community!
This is my first post since my recent introduction to Game Maker and I am loving it!
I've now run into a problem, that Google and browsing the forum doesn't seem to fix.

I trying my hand at a top-down shooter and I am struggling with something:

I have multiple instances of my Enemy object and I want them to shoot individually at a random rate.
Right now I have an alarm that sets itself everytime it triggers + a random range.
Problem with that is, that only one instance shoots...

How would I go about solving this problem?
 

TheMagician

Member
This may be the wrong forum for your question.

Anyway, from what you're describing the instances should all shoot individually. So we will need to see some code in order to find the error.
 
M

moichelm

Guest
Oh my bad, I will post it in the other forum plus the code.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Oh my bad, I will post it in the other forum plus the code.
No need! I've moved the topic for you. And you needn't make a new topic if you post in the wrong forums, simply hit the Report button and request that a moderator move it for you. ;)

As for your issue, we really need to be able to see what the code/DnD is for both shooting and for the bullet movement. It could be many things, but from what you say you want to check how you are setting the bullets to move and their position, and also how you create them. One obvious thing though would be that maybe you are using an object index instead of an instance ID when you create them?
 

TheouAegis

Member
If your code even remotely looks like the codes below or part of the codes below
Code:
instance_create(ship.x,ship.y,bullet);
bullet.direction = point_direction(ship.x,ship.y,player.x,player.y)
ship.alarm[0] = 5;
Code:
instance_create(x,y,bullet);
with bullet direction = point_direction(x,y,player.x, player.y)
then you messed up already.

Referencing instances by their object names like in the first code is a no-no. The first line uses object variable reads; since only one instance can be read at a time, only one particular ship will have its coordinates fetched and it may not be the ship you want. The second and third line contains an object variable write (and reads, but we covered that); when writing to an object, all instances of that object are affected, so in this case all bullets would be affected.

Using with() on an object name is a no-no. It will make the following code affect all instances of that object, not the last one created.

Your code should instead look like one of these two:
Code:
if can_shoot { 
with instance_create(x,y,bullet) direction=point_direction,x,y,player.x,player.y);
can_shoot=false;
alarm[0] = 5;
}
or
Code:
if can_shoot {
var inst = instance_create(x,y,bullet);
inst.direction = point_direction(x,y,player.x, player.y);
can_shoot = false;
alarm[0] = 5;
}
 

CMAllen

Member
It should also be said that unless you can be absolutely certain any run-time created objects will be destroyed properly, objects that create other objects should have some sort of array or list of the IDs of any objects created so that you can check in on them and destroy any errant objects manually (say, once every few seconds). Both examples above operate under the assumption that the objects being created will always destroy themselves by some inescapable event. If they don't, you get a memory leak.
 
Top