GML Instance_create sometimes works and sometimes doesn't

Hello, I completed the traffic spawning system in my GTA 2 style game, and it worked, but after a bit it stopped. The way it works is it checks the player distance, and then creates a car, and then a pedestrian in the car to drive it. Problem is, the pedestrian only spawns about 20% of the time, and I don't see any pattern to when it works and when it doesn't (The spawning system is an object that I place everywhere I want a car to spawn. Parked cars without peds in them are not supposed to exist).
Code:
///Traffic Spawning System Core
distance = distance_to_object(obj_player);

///Spawn

if( distance < 445 and distance > 405 and !collision_circle( x , y , 140 , obj_car , false , true ) and instance_number(obj_car) < 20 ){
    instance_create( x , y , obj_car);
    instance_create( x , y , obj_ped);
}
The despawn distance for the peds and the cars are the same. I do have pedestrian spawners, that spawn people walking down sidewalks, but those aren't connected to the traffic spawners in any way. I've had a few instances of code that looked okay not working as it should.
 

YoSniper

Member
I'm guessing this is in the Step Event of the spawner object?

I recommend setting pointers to the car and pedestrian objects spawned (and de-spawned) by this object so that you have consistency.

For example:
Create Event
Code:
my_car = noone;
my_ped = noone;
Step Event
Code:
if instance_exists(obj_player) {
    distance = distance_to_object(obj_player);
    
    if distance < 445 and not instance_exists(my_car) and not instance_exists(my_ped) {
        if not collision_circle(x, y, 140, obj_car, false, true) and instance_number(obj_car) < 20 {
            my_car = instance_create(x, y, obj_car);
            my_ped = instance_create(x, y, obj_ped);
        }
    }
    //Include code for de-spawning here, too
}
 
I'm guessing this is in the Step Event of the spawner object?

I recommend setting pointers to the car and pedestrian objects spawned (and de-spawned) by this object so that you have consistency.

For example:
Create Event
Code:
my_car = noone;
my_ped = noone;
Step Event
Code:
if instance_exists(obj_player) {
    distance = distance_to_object(obj_player);
  
    if distance < 445 and not instance_exists(my_car) and not instance_exists(my_ped) {
        if not collision_circle(x, y, 140, obj_car, false, true) and instance_number(obj_car) < 20 {
            my_car = instance_create(x, y, obj_car);
            my_ped = instance_create(x, y, obj_ped);
        }
    }
    //Include code for de-spawning here, too
}
Put the despawn code in the spawner object? I don't think that will work, because the car and peds check the distance from the player and then delete themselves when they are far enough to be off-screen. I have this:
Code:
if(distance_to_object(obj_player) > 440){
    despawn = true;
}

if(despawn == true){
    instance_destroy();
    myGuide.despawn = true;
}
 
Top