Jump to content

Calculating difference and using repeat until loop


Recommended Posts

Hello!

I started re-doing my inventory system, I made some functions based on this topic 

I did everything without a problem, everything works fine, except a calculation I am trying to do when giving an item to a container. I have stack limits defined for items inside an ITEM_TABLE.

The stack limit for, let's say Apple item is 5. When calling the giveItem function, I would like to check if the count exceeds the stack limit, then loop the item giving process until the newly calculated count value is less than the stack limit.

So let's say I call

-- itemID, count
giveItem(1, 8) -- give 8 apples, but the apple has a stack limit of 5, so I would like the player to end up with 2 apple items, one with a count of 5 and one with a count of 3

So then I would like the player to have 2 different apple items, one with a count of 5 and one with a count of 3

This is how I was trying to do it, but it seems like I can't use the repeat until/while loop properly, or I am missing calculations, I can't get the result I want.

function ContainerMethods:giveItem(itemID, count)
	local itemDetails = getItemDetails(itemID)
	if not itemDetails then return false end
	count = tonumber(count) or 1
	
	local carrySlot = self:findExistingItemSlot(itemID)
	if carrySlot and carrySlot.count + count <= itemDetails.stacklimit then -- if the container has an item like this and it's less than the stack limit, add the count
		carrySlot.count = carrySlot.count + count
	else
		local tempCount = count
		
		repeat -- i don't know if i should use repeat until, or while loop, repeat until seems like the correct way of doing it
		--while tempCount > itemDetails.stacklimit do
			local slotX, slotY = self:findFreeSlotForItem(itemID)
			if slotX and slotY then -- i am working with a grid inventory with different item sizes
				for x = slotX, slotX + itemDetails.size.x - 1, 1 do
					for y = slotY, slotY + itemDetails.size.y - 1, 1 do
						self.grid[x][y] = true
					end
				end
				
				local newItem = {
					["itemID"] = itemID,
					["count"] = tempCount, -- should i make calculations here as well?? i don't think so but i could be wrong
					["slot"] = {x = slotX, y = slotY},
				}
				
				table.insert(self.items, newItem)
				
				tempCount = tempCount - itemDetails.stacklimit -- tempCount is the difference
				print(tempCount)
			else
				print("no empty space for item '" .. itemID .. "' in container '" .. self.name .. "'")
				return false
			end
		--end
		until tempCount < itemDetails.stacklimit -- repeat adding a new item until the count is less than the stack limit
	end
	
	return true
end

But the result I get is:

spacer.png

Notice that the debugscript prints 3, but my inventory has only one item with the count of 8, instead of 5 and 3. How can I make this function properly, so I don't have to worry about it in the future when I give an item to the player?

Thank you for your help in advance!

Link to comment
  • Moderators

Step 1: Find ALL existing slots

35 minutes ago, Dzsozi (h03) said:
local carrySlot = self:findExistingItemSlot(itemID)

This method does not do that unfortunately. So you will have to create a new one.

local carrySlots = self:findExistingItemSlots(itemID)

Step 2: Fill those up.

Step 3: Create new slots for the remaining items.

Repeat until is fine. ?

repeat 
	local slotX, slotY = self:findFreeSlotForItem(itemID)
	if slotX then
		local countInsert = math.min(count, itemDetails.stacklimit)

		-- ... 
 
		local newItem = {
			["itemID"] = itemID,
			["count"] = countInsert,
			["slot"] = {x = slotX, y = slotY},
		}
		count = count - countInsert
    
 		-- ...
	end
until count == 0 or not slotX

 

Step 4: Return the items that do not fit, so that you can decide what to do with those.

local remainingItems = object:giveItem(1, 8) 
if #remainingItems > 0 then
	iprint("items do not fit", #remainingItems)
end

 

 

 

 

  • Thanks 1
Link to comment
27 minutes ago, IIYAMA said:

Step 1: Find ALL existing slots

This method does not do that unfortunately. So you will have to create a new one.

local carrySlots = self:findExistingItemSlots(itemID)

Step 2: Fill those up.

Step 3: Create new slots for the remaining items.

Repeat until is fine. ?

repeat 
	local slotX, slotY = self:findFreeSlotForItem(itemID)
	if slotX then
		local countInsert = math.min(count, itemDetails.stacklimit)

		-- ... 
 
		local newItem = {
			["itemID"] = itemID,
			["count"] = countInsert,
			["slot"] = {x = slotX, y = slotY},
		}
		count = count - countInsert
    
 		-- ...
	end
until count == 0 or not slotX

 

Step 4: Return the items that do not fit, so that you can decide what to do with those.

local remainingItems = object:giveItem(1, 8) 
if #remainingItems > 0 then
	iprint("items do not fit", #remainingItems)
end

 

 

 

 

Thank you so much, it works and I also managed to do the step 4 to manage remaining items, so I can add to main inventory or drop them. I appreciate it and your time ❤️

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...