0

I have been trying to loop over a scriptable object (the orderList) to determine if an item (included in the orderList) can be delivered (which means, if for a specific job, the craft time allocated is enough to have the item created). The problem is that I found out that the second item in the orderList was always missed while looping over and I can't identify why.

I checked the parameter of the second itemObject and the attributed job is correct, as well as the craft duration. I added a debug log to check if the if condition rejected the item, but it seems that the function does just not loop over the second item, and goes directly from orderList.Container[0] to orderList.Container[2].

Does anybody have an idea on why the second item is ignored?

Here is the code for information:

foreach (var job in Job.GetValues(typeof(Job)))
        {
            Debug.Log("job: "+ job);
            int duration = 10;
            for (int i=0; i < orderList.Container.Count; i++)
            {
                Debug.Log("item: " + orderList.Container[i].item.title);
                Debug.Log("pre-duration: " + duration);
                if ((orderList.Container[i].item.métier == job.ToString()) & (orderList.Container[i].hour <= duration))
                {
                    duration -= orderList.Container[i].hour;
                    Debug.Log("post-duration: " + duration);
                    inventory.AddItem(orderList.Container[i].item, orderList.Container[i].amount);
                    deliveryList.AddItem(orderList.Container[i].item, orderList.Container[i].amount);
                    orderList.RemoveItem(i);
                }
            }
        }

1 Answer 1

1

You don't give enough code (the precise types involved) but I'll take a side-bet that the problem has to do with logic like this...

i = 0: process orderList[0]...  remove orderList[0]  
// now the item that used to be orderList[1] is orderList[0]  
i = 1: process orderList[1] // the item that used to be orderList[2]  
// (skipping the original orderList[1])
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It fixed the issue. It seems so obvious now that you say it, but I guess I will need to be more careful now with deletion in list/objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.