SOLVED Local Variable Keeps Resetting Back To 0

jenixelle

Member
GML:
//Create Event 
timer=room_speed*10
seat = 0;

//Step Event
if (timer <=1)
{
    if(seat<=6)
    {
        show_debug_message(seat);
        instance_create_layer(irandom(200),irandom(400),"Instances_1", oCustomer);
        seat ++;
    }
    timer = irandom_range(room_speed*10, room_speed*20);
}

timer--;


I want to create instances of oCustomer based on a timer and variable. If (timer <=1) and (seats <= 6) it should stop making instances of oCustomer. It creates the instances one at a time but it doesn't stop making them at 6 and over time the instances are created quicker and quicker. What I noticed was from show_debug_message(seat) that the seat is trying to be reset back to zero constantly meaning seats is never = 6 which is why the instances don't stop creating themselves. The problem is I don't know where seats is being interfered with. I have no other objects where oCustomer is being interacted with and all the code for oCustomer is in Create and Step.
 
Wait, so you have this in the customer object itself? Every time a customer is created, they're told to create 6 more, and then those 6 end up making 6 more each, and then the city becomes overpopulated and it's then faster to walk than it is to drive on the highway because it's congested everywhere.

You should use a control object to handle this instead.
 

jenixelle

Member
Wait, so you have this in the customer object itself? Every time a customer is created, they're told to create 6 more, and then those 6 end up making 6 more each, and then the city becomes overpopulated and it's then faster to walk than it is to drive on the highway because it's congested everywhere.

You should use a control object to handle this instead.
Oh thank you very much, where can I put this then instead of an object?
 
Last edited:
An object is where you would still put it, you just don't put it inside of the object (or rather, the instance) that is being spawned. This is an example of a coding "schema", or whatever, called a "spawner". The idea is that you want things to be created at a certain rate, a certain number of times or at a certain frequency, etc, so you create something (in the case of GMS, it's an object) that is the "spawner" for the thing you want to be created. In your case, I would have an oCustomerSpawner object, that runs the code you originally had in the oCustomer object. Doing it this way sidesteps the recursive problem as there's only ever one oCustomerSpawner (it's not spawning itself, it's spawning oCustomers), while there can be many oCustomers and never the twain shall meet (or something like that).
 

jenixelle

Member
An object is where you would still put it, you just don't put it inside of the object (or rather, the instance) that is being spawned. This is an example of a coding "schema", or whatever, called a "spawner". The idea is that you want things to be created at a certain rate, a certain number of times or at a certain frequency, etc, so you create something (in the case of GMS, it's an object) that is the "spawner" for the thing you want to be created. In your case, I would have an oCustomerSpawner object, that runs the code you originally had in the oCustomer object. Doing it this way sidesteps the recursive problem as there's only ever one oCustomerSpawner (it's not spawning itself, it's spawning oCustomers), while there can be many oCustomers and never the twain shall meet (or something like that).
Thank you so much! I had no idea it was as simple as that
 
Top