How do I solve "Infinite Loop Detected"?

Hello everyone,
I’ve been having some issues with this loop I’m trying to make. This loop activates when the amount of items that have to be added to the inventory surpasses the stack size for said item.

IFor each loop, it should create a full stack of that item and decrease the amount to add to the inventory by the stack’s size amount. It should repeat until the amount to add to the inventory is less than the stack size, in which case it should add an additional stack with the items that remain.

The problem is that the loop gives out an infinite repetition error, and I can’t seem to find where that error is.

Can you help me out on this?

The loop node I mentioned is higlighted in yellow.

I don’t see a for loop in your blueprint code though, just a while loop. Just use the for loop with break node.

Hey @impawel

what happens if you delete this connection:

A for loop is used when you know how many iterations of that loop you will need.

I need the loop to continue as long as the variable is above zero and below a given “stack” size, and every iteration of the loop reduces that variable’s value by a “stack” amount

I don’t think the for loop would be useful here, since I can’t put a specific number of iterations, the value of “stack” is variable depending on the item type, as well as the amount to add/subtract from the inventory

Exactly! You should use a for loop with break for such a task. Using a while loop is definitely not the way to go here. You can set the last index as whatever’s the max possible iteration is in your specific task and break when your desired condition is met.

Hey @impawel

if you want to use a ForLoop you can use:

truncate(Quanity / StackSize) => Last ForLoop Index, starting with 1 OR
truncate(Quanity / StackSize)-1 => Last ForLoop Index, starting with 0

(Quanity % StackSize) => remaining number of items which are not enough for a full stack

% is called modulo

Oh my god. Thank you so much. Yesterday I was struggling a lot with this, but as soon as I implemented this logic it worked. Im infinitely grateful for the help.

1 Like

I wanted to thank you for suggesting to use a “for loop”. Although I did end up doing it the way @L1z4rD89 suggested, this has helped me a lot.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.