stopping array_length_1d when cycling through customers food orders

chefdez

Member
Hello, everyone I have a simple cooking game here and I am using array_length_1d to cycle through my customers to see who entered the room first and has a particular food order.

Code:
if cooked >= 100 && distance_to_object(o_chef) < 25 {
        with(o_customer) {
            for (var i = array_length_1d(myCount) - 1; i > -1; i--;) { // myCount is an array that stores which customer walked in the restaurant first
                if custy[1] = foods.meat { // Here is where I am checking to see if my customer has a MEAT food order.
                    state = customer_state.EATING;  // Changing that particular customers state to eating now that the food is cooked.
                }
            }
        }
    instance_destroy(); // Destroy object in our hand
}
I'm not sure how to get array_length_1d to cycle through the customers and see who is the first customer that walked in with a meat order. I've thought about using other but it won't specifically target the first customer to walk in and since array_length_1d moves backwards, I'm trying to figure out how to stop it at the earliest order. Am I overthinking this? Haha! Thanks
 
Last edited:

NightFrost

Member
What's custy[1]? If your customer instance IDs are stored in myCount, you are looping through them twice, as your with already loops through them, although in an order that you must consider essentially random.
 

chefdez

Member
What's custy[1]? If your customer instance IDs are stored in myCount, you are looping through them twice, as your with already loops through them, although in an order that you must consider essentially random.
custy[1] assigns a food order to the customer when the customer instance is created, myCount is not the instance ID but rather a variable that assigns them a number 1-8 to keep count of who walked in first. The restaurant has 8 customers max, what I am trying to do is cycle through those 8 customers and see which one came in first with a meat order.

For example:
customer 1: eggs
customers 2: meat
customer 3: fish
customer 4: meat

if i cook the food and put up meat, customer 2 should get that meat order because they walked in first. sry if confusing im working on my coding hah
 

NightFrost

Member
I assume you also need to be able to remove customer once order has been fulfilled and eaten, so rather use ds list, since you can delete from the middle with those.

I would propose creating the system as follows: as customer walks in and you spawn their instance, also save the food order they make into that instance's variables. Add the customer's instance id to the end of a ds list, which makes the list remember the order they came in. When you serve food, loop through the ds list starting from the beginning - oldest customer first. Take an instance id from the list, check the instance's saved order via that id, and if it matches your food, serve that customer. When customer leaves, delete them from the ds list (loop through, compare id to stored, delete that position when match is found).
 

TheouAegis

Member
Just breaking won't check the customer place in line if he didn't set up proper removal of them from the line.
 

Nidoking

Member
No, but it is essential for finding the "first" of anything using a for or with loop.

Incidentally, the with in this construct is worthless, and you need to get rid of it. If the array is global, I don't even understand how you're referencing it, but you don't need to run that for loop in the customer context. If it's part of the customer, then why are you storing an array of all customers inside the customer? You're not even using the contents of the myCount array - just the length of it.
 

Yal

šŸ§ *penguin noises*
GMC Elder
Array indexing starts at zero, so shouldn't it be custy[0] ...? (an array with max 8 members will use indices 0...7, not 1...8)
 

Nidoking

Member
Array indexing starts at zero, so shouldn't it be custy[0] ...? (an array with max 8 members will use indices 0...7, not 1...8)
I have to assume, for my own sanity, that every o_customer has a custy array with various values in it relating to aspects of that customer.
 

chefdez

Member
Thank you for the kind suggestion NightFrost, I'm unsure about these instructions.

Take an instance id from the list, check the instance's saved order via that id, and if it matches your food, serve that customer.
Code:
Create:
custylist = ds_list_create();
ds_list_add(custylist, ds_list_size(custylist)-1, instance_id); // Adding our instance id to the back of the list
Code:
Step:
if cooked >= 100 && distance_to_object(o_chef) < 10 {
        with(o_customer) {
            var listSize = ds_list_size(custylist)           
            var index = 0;
            
            for(index = 0; index < listSize; index++) { // Cycling through the list
                var ourCusty = ds_list_find_index(custylist, instance_id)
                    if ourCusty.foodOrder = foods.meat {
                        ourCusty.state = customer_state.EATING;
                }
            }
        }
    instance_destroy();
}
I'm spit balling here as always when I code, feel free to abandon ship in response to my skill level :D No hard feelings.
If you're still here, I'm still unsure how to grab the particular ID and then stop as opposed to reading through all of them.
 

chamaeleon

Member
Thank you for the kind suggestion NightFrost, I'm unsure about these instructions.



Code:
Create:
custylist = ds_list_create();
ds_list_add(custylist, ds_list_size(custylist)-1, instance_id); // Adding our instance id to the back of the list
Code:
Step:
if cooked >= 100 && distance_to_object(o_chef) < 10 {
        with(o_customer) {
            var listSize = ds_list_size(custylist)         
            var index = 0;
          
            for(index = 0; index < listSize; index++) { // Cycling through the list
                var ourCusty = ds_list_find_index(custylist, instance_id)
                    if ourCusty.foodOrder = foods.meat {
                        ourCusty.state = customer_state.EATING;
                }
            }
        }
    instance_destroy();
}
I'm spit balling here as always when I code, feel free to abandon ship in response to my skill level :D No hard feelings.
If you're still here, I'm still unsure how to grab the particular ID and then stop as opposed to reading through all of them.
Why are you adding the array containing all active instances (which is what instance_id is) in the room regardless of object type to a ds_list that will only have that one array as its member, to all instances of type o_customer? Rather than spit balling it, take some time to figure out what you are trying to do. Do you have the concept of an object and an instance that serves as the proper place to hold a ds_list of o_customer instances, that is not o_customer itself?
 

chefdez

Member
Why are you adding the array containing all active instances (which is what instance_id is) in the room regardless of object type to a ds_list that will only have that one array as its member, to all instances of type o_customer? Rather than spit balling it, take some time to figure out what you are trying to do. Do you have the concept of an object and an instance that serves as the proper place to hold a ds_list of o_customer instances, that is not o_customer itself?
Sure it seems like you're talking about a customer controller as the object that holds a ds list of o_customers as opposed to referencing the o_customer itself? I am putting in some effort here, programming isn't easy at all for me clearly. I'm just seeking a bit of guidance and I appreciate you taking the time off to aid me!
 

chamaeleon

Member
Sure it seems like you're talking about a customer controller as the object that holds a ds list of o_customers as opposed to referencing the o_customer itself? I am putting in some effort here, programming isn't easy at all for me clearly. I'm just seeking a bit of guidance and I appreciate you taking the time off to aid me!
I want to make sure you understand that all event code you write for, for instance, o_customer, will execute for each instance of o_customer, with the implication that instance variables (like custylist) are relative to the instance who is currently the target of the code execution, lets call it the context of execution (and, again, every instance gets its own custylist, which on the surface does not look correct and indicates a flawed game state model).

If you have another, completely different object, and only one instance of it, you can use its create event to do something one time only, like create a new ds_list (because you have only one instance, the create event only runs once) which is then stored as an instance variable on this kind of object, or alternatively, depending on your preferences, a global variable.

Use the create event to initialize things that pertain to a given instance, use multiple objects to create separation of concerns. o_customer keeps track of what the customer want to eat, and whether they are eating or not, etc., while o_game (for lack of a better name, of which you might then only have one instance) keeps track of the list of customers, and other game state information.
 
Top