Reputation: 21
I am having an issue with my for loop in godot and hopefully you can help please!
For my game inventory system, I am using a for
loop to update the quantity of items in the inventory. For example, if I fire an arrow, the for
loop will loop through all the inventory slots, find one with arrows and reduce the amount by one, as shown here:
for slot in range(0, inventory_maxSlots):
if (inventory[String(slot)]["id"] == String(item)):
var newAmount = inventory[String(slot)]["amount"] - 1
if (newAmount == 0):
inventory_updateItem(slot, 0, 0)
update_slot(slot)
else:
inventory[str(slot)] = {"id": str(item), "amount": int(newAmount)}
update_slot(slot)
This works perfectly except for when I have stacks of arrows in more than one slot. If I have 3 slots filled with 10 arrows each, for example, after firing 1 arrow, all stacks will be reduced by 3. So I will end up with 3 stacks of 7 arrows. If I had 4 stacks of arrows, then each get reduced by 4 etc.
I just want the loop to start, find the first slot with arrows, reduce its amount by 1 and then stop. I tried using a Break
but it only seemed to stop the loop once it had run through all of the slots, and not just the first slot with arrows it finds.
I am only a couple of months into learning programming so I have probably missed something really obvious and I would greatly appreciate any help!
Thanks.
Upvotes: 1
Views: 12115
Reputation: 21
Thanks for your help on this one and I have solved it now. You were correct a Break was exactly what was needed. The problem was that I had another for loop earlier in the code which I also needed to add a break to. Thanks again!
Upvotes: 1