GameMaker Making an enemy shoot at a specific direction

M

MagnusFT05

Guest
So i have like a sniper that i want to shoot at a certain place (so the player has to jump over the shots) And i saw a thread about this which said i needed to add this code to the "create" section of the guy whos shooting the bullet:

firingdelay = room_speed/6;
alarm(0) = room_speed * 5; //shoot every 5 seconds

and to write this in alarm 0:

instance_create_layer(x, y, "Bullets", ObjectBullet);
with (ObjectBullet)
{
direction = point_direction(x, y, ObjectPlayer.x, ObjectPlayer.y); // Give the bullet a direction
speed = 8;
}
alarm[0] = room_speed * 4;

and when i try to run the game the only error is: Object:SniperMan event: Create at line 9 : unknown function or script alarm

indicating that the alarm(0) line in the create event is wrong


How tf can i fix this?
 

FrostyCat

Redemption Seeker
alarm is an array, use square brackets to subindex it.
GML:
alarm[0] = room_speed * 5; //shoot every 5 seconds
Also, if you want to refer to an instance you just created, don't do it by object. Use the return value of instance_create_layer().
GML:
with (instance_create_layer(x, y, "Bullets", ObjectBullet))
{
    direction = point_direction(x, y, ObjectPlayer.x, ObjectPlayer.y); // Give the bullet a direction
    speed = 8;
}
See: What's the Difference: Objects and Instances
NEVER access a single instance by object ID if multiple instances of the object exist. This includes attempts to reference or set object.variable (which is inconsistent across exports) and using with (object) to apply actions to it (this encompasses all instances of the object instead of just the one you want). Verbally, "Dog's colour" makes sense with one dog, but not with multiple dogs.
Created or copied instance. instance_create() (GMS 1.x and legacy) / instance_create_layer() and instance_create_depth() (GMS 2.x) and instance_copy() return the ID of the created or copied instance. NEVER use instance_nearest() to establish this relationship --- something else could be closer by.
 
M

MagnusFT05

Guest
You've got alarm(0) in the Create event and alarm[0] in the Alarm event. Only one is correct.
Thanks it worked, but now the bullet doesnt come from the object that it is supposed to come from
 

Mk.2

Member
Did you try what FrostyCat suggested? Every time a sniper creates a new bullet, all existing bullets will change their direction using that sniper's coordinates, and the player's new coordinates.
 
Last edited:
Top