#Worth using nested function in this case?

1 messages · Page 1 of 1 (latest)

balmy shadow
#
local Items = require(script.Parent.Items)

local Inventory = {}
local Inventories = {}

Inventory.__index = Inventory
function Inventory.new(userID, data)
    if not userID then return end
    local self = setmetatable(data or {}, Inventory)
    self.MaxInventorySpace = self.MaxInventorySpace or 50
    self.MaxStacks = self.MaxStacks or 1000
    self.MaxExtraSpace = self.MaxExtraSpace or 25
    self.ItemCount = self.ItemCount or 0
    self.ExtraItemCount = self.ExtraItemCount or 0
    self.Items = self.Items or {}
    self.ExtraItems = self.ExtraItems or {}
    Inventories[userID] = self
    return self
end


function Inventory.AddItem(self, item, amount)
    if type(item) ~= "table" or type(amount) ~= "number" then return end
    if not Items[item.Name] or item.ID ~= Items[item.Name].ID then return end
    
    local function sortExtraItems(item, amount) -- This function
        --Worth?
    end
    
    local itemName = item.Name
    local itemAmount = self.Items[itemName]
    
    if item.Stackable and not itemAmount then -- If item is stackable and is not found in the inventory
        self.ItemCount += 1
    elseif not item.Stackable then -- If item is not stackable then no need to track if it needs to occupy a space
        self.ItemCount += 1
    end
    
    if self.ItemCount > self.MaxInventorySpace then -- If itemCount exceeds the maximum invententory limit
        self.ItemCount = self.MaxInventorySpace
       -- This statement will use the sortExtraItems()
        return
    end
    
    self.Items[itemName] = if itemAmount then itemAmount + amount else amount -- Finally the main purpose of this method
    
    if self.Items[itemName] > self.MaxStacks and (self.ItemCount + 1 <= self.MaxInventorySpace) then -- If amount of stacked items exceeded the maxStacks limit
        -- This statement will also use the sortExtraItems()
    end
end

return Inventory

tbh I didnt think a simple add item to inventory would introduce alot of edge cases so so many ifs 😛

balmy shadow
#

welp brb gonna refactor everything. This what i get for rushing

#

math.min for the win

balmy shadow
#
local Items = require(script.Parent.Items)

local Inventory = {}
local Inventories = {}
local ConvertToStacks

Inventory.__index = Inventory
function Inventory.new(userID, data)
    if not userID then return end
    local self = setmetatable(data or {}, Inventory)
    self.MaxInventorySpace = self.MaxInventorySpace or 50
    self.MaxStacks = self.MaxStacks or 1000
    self.MaxExtraSpace = self.MaxExtraSpace or 25
    self.ItemCount = self.ItemCount or 0
    self.ExtraItemCount = self.ExtraItemCount or 0
    self.Items = self.Items or {}
    self.ExtraItems = self.ExtraItems or {}
    Inventories[userID] = self
    return self
end


function Inventory.AddItem(self, item, amount)
    if type(item) ~= "table" or type(amount) ~= "number" then return end
    if not Items[item.Name] or item.ID ~= Items[item.Name].ID then return end

    local itemName = item.Name
    local itemAmount = self.Items[itemName] or 0

    -- stackable
    if item.Stackable then
        local totalAmount = itemAmount + amount
        local newStacksNeeded = math.ceil(totalAmount / self.MaxStacks) - math.ceil(itemAmount / self.MaxStacks)

        -- Update the item count with new stacks if needed
        self.ItemCount = self.ItemCount + newStacksNeeded
        if self.ItemCount > self.MaxInventorySpace then
            self.ItemCount = self.MaxInventorySpace
            -- Send signal or something or let the player know
            return
        end

        -- Update the number of items in the stack
        self.Items[itemName] = totalAmount % self.MaxStacks
        if totalAmount > self.MaxStacks then
            -- If we have exceeded a stack, place the excess in a new stack
            self.ExtraItems[itemName] = (self.ExtraItems[itemName] or 0) + math.floor(totalAmount / self.MaxStacks)
        end
    else
        -- non-stackable
        if amount > 0 then
            local availableSpace = self.MaxInventorySpace - self.ItemCount -- Space in the main inventory
            local availableExtraSpace = self.MaxExtraSpace - self.ExtraItemCount -- Space in the extra inventory

            local amountToAdd = math.min(amount, availableSpace) -- Amount that can be added to the main inventory
            local excessAmount = amount - amountToAdd -- Amount that overflows the main inventory

            -- Add items to the main inventory
            self.Items[itemName] = (self.Items[itemName] or 0) + amountToAdd
            -- Update ItemCount but do not exceed MaxInventorySpace
            self.ItemCount = math.min(self.ItemCount + amountToAdd, self.MaxInventorySpace)
            print(itemName .. " added to main inventory: " .. amountToAdd)

            -- Add excess items to the extra inventory if there's space
            if excessAmount > 0 and availableExtraSpace > 0 then
                local amountToAddToExtra = math.min(excessAmount, availableExtraSpace)
                self.ExtraItems[itemName] = (self.ExtraItems[itemName] or 0) + amountToAddToExtra
                -- Update ExtraItemCount but do not exceed MaxExtraSpace
                self.ExtraItemCount = math.min(self.ExtraItemCount + amountToAddToExtra, self.MaxExtraSpace)
                excessAmount = excessAmount - amountToAddToExtra -- Update excessAmount if some were added to extra
                print(itemName .. " added to extra inventory: " .. amountToAddToExtra)
            end

            -- we do something with any remaining excess items
            if excessAmount > 0 then
                -- Tell the player to stop hacking man
                print(itemName .. " could not be added (excess): " .. excessAmount)
            end
        end

    end
end
#

well for those of you interested on my refactored code + handles the excess amount

#

i keep finding newer edge cases so i think doing it this way will allow me to be more flexible if i found more

hazy chasm
#

if you can think of a way to not to use a nested function then dont but if you cant see a way not to then just do it

balmy shadow
#

yeah like i refactored my script once again. Theres just endless amount of stuff to handle alot of edge cases. I think imma settle for one then move on to the next method