Satisfying a Request using Structures

The structure “demand” goes as follows.

index value (integer)
apple 56
banana 34

In this demand, a customer wants 56 apples, and 34 bananas.

Below is my inventory, which is also a structure in GDevelop.

index value (integer)
apple 124
banana 67
orange 12

After satisfying my customers demands, my inventory becomes:

index value (integer)
apple 68
banana 33
orange 12

How could I do this in GDevelop using events? Please also consider the following.
If my inventory cannot fit the demands, then the transaction will not be completed, and I won’t lose anything. It should also be obvious that any extra indexes, such as “oranges” should not be tampered with, as the customer did not demand them.

Something like this should do the trick. It iterates over all the customer’s demands, reducing your inventory by the requested amount. The demand key is stored in fruitName,and the demand value is stored in demandAmount:

The child exists check in the sub event isn’t absolutely necessary, but it makes for cleaner conditions.

This will only subtract from individual indexes if they are greater. It has to make sure I am able to satisfy the customers request.

If I have 5 apples, but 13 oranges, and the request is for 11 oranges and 6 apples, it will leave me with 2 oranges but 5 apples, and I was unable to fulfill my customers request. If I can’t fulfill the request, then I the transaction should not be done at all.

Greater or equal to.


So, it’s a case of if not all of the order can be met, then none of them are actioned? I’ve given you the tools you can use to achieve this. It will require 2 iterations over the customerDemand structure, one to confirm that all orders can be met, the other to take it out of the inventory. You will probably need to use a boolean variable to indicate that the order can be satisfied.