[SOLVED] GMS 2.3.3, How to spawn enemy instances and control each instance separately

Hi all!

I currently have enemy objects (small rockets) that are spawned using a time interval, and when an enemy object's x position matches the player's x position, I have it working where the enemy object's direction changes from 0 to 90, flying towards the player.

The problem is, every enemy object instance flies up towards the player once the first instance's x position reaches the player object's x position.

I've been trying to use instance_id, instance_find, etc., but everything I've tried failed. I tried reading through a of posts here for help but haven't found anything that worked.

Here's what I have that works so far:

o_enemy2 Create:
GML:
hp_ = 4;
direction = 0;
speed = -2
o_enemy2 Step:
Code:
if hp_ <= 0 {
    instance_destroy();   
}

if o_enemy2.x == o_player.x { ///launch rocket when enemy x position = player x position
    direction = 90; ///fly upwards
    speed = 4; ///at speed of 4
}
objSpawn2 Create:
Code:
alarm[0] = random_range(room_speed*.5, room_speed*3);
objSpawn2 Alarm 0:
Code:
instance_create_layer(x, y, "Enemies", o_enemy2);
alarm[0] = random_range(room_speed*.5, room_speed*3);
 

woods

Member
with the limited knowledge that i do have, i wanna say...
i think your problem might be that you are using the dotvariable for o_enemy2 in its own step event.. causing all instances of o_enemy2 to do their thing ;o)
if o_enemy2.x == o_player.x { ///launch rocket when enemy x position = player x position
try changing that to...
Code:
if x == o_player.x { ///launch rocket when enemy x position = player x position
 
As @woods pointed out, I'd assume your problem is using an object index. If you are typing code inside of the object, all variables are assumed to come from that object, so there's no need to reference object.variable, a simple variable will do. To further expand on that, object indices and instance indices are two separate things that you'll need to come to terms with quickly if you're going to avoid an avalanche of bugs like this. Have a read of this topic to get a grounding on the difference: What's the Difference: Objects and Instances.
 
That solved it! Thank you!

I'll make sure to read that "What's the Difference" post.

Since this solved my issue, does I need to mark this as"Solved", or ?
 

basementApe

Member
Since this solved my issue, does I need to mark this as"Solved", or ?
It's always a nice thing to do. That way people know not to check in on the thread anymore, and people with similar problems can tell at a glance if your thread has the solution they need.
 
Top