Jump to content

Inventory help


Recommended Posts

Posted (edited)

Hello, I'm making an inventory and now I've found that the items can be moved, but it's not good, for example, that if I bring it to another item, they don't change places, the slot[5] table contains the items. Can someone help me fix the bugs?

 

function ItemMove()
    for i, slot in ipairs(slots) do
        if isMouseInPosition(sx * slot[1], sy * slot[2], sx * slot[3], sy * slot[4]) then
            if slot[5] then
                if slot[5][1] == DragNDrop[1][1] then
                    if slot[5][2] < items[slot[5][1]][3] then
                        if slot[5][2] + DragNDrop[1][2] > items[slot[5][1]][3] then
                            slot[5][2] = items[slot[5][1]][3]
                        else
                            slot[5][2] = slot[5][2] + DragNDrop[1][2]
                        end
                    else
                        DragNDrop[1] = slot[5]
                        slot[5] = DragNDrop[5]
                    end
                else
                    slots[DragNDrop[2]][5] = slot[5]
                    slot[5] = DragNDrop[5]
                end
            else
                slot[5] = DragNDrop[1]
                print("XD")
            end
        end
    end
end

 DragNDrop = {slot[5], i}

 

Below, I only wrote what is in the DragNDrop table

Edited by Firespider
Link to comment
ddEventHandler("onClientClick", root, function(Button, State)
    if IsInvShow then
        for i, slot in ipairs(slots) do
            if isMouseInPosition(sx * slot[1], sy * slot[2], sx * slot[3], sy * slot[4]) then
                if Button == "right" and State == "down" then
                    if slot[5][1] then
                        ClickOnItem(slot[5], i)
                        break
                    end
                else
                    if not slot[5][1] then
                        if Button == "left" and State == "down" then
                            local x, y = getCursorPosition()
                            WindowMove = {x - InvPos[1], y - InvPos[2]}
                        else
                            WindowMove = {false, false}
                        end
                    else
                        if Button == "left" and State == "down" then
                            DragNDrop = {slot[5], i}
                            slot[5] = {false}	
                        else
                            if DragNDrop[1] then
                                ItemMove()
                            end
                        end
                    end
                end
            end
        end
    end
end)

This is how I call the function

--slot, ID, stack
	{1, 1, 1, ""}

This is the content of the slot table

Link to comment
function ItemMove()
    for i, slot in ipairs(slots) do
        if isMouseInPosition(sx * slot[1], sy * slot[2], sx * slot[3], sy * slot[4]) then
            slots[DragNDrop[2]][5], slot[5] = slot[5], slots[DragNDrop[2]][5]

            local itemId = slots[DragNDrop[2]][5][1]
            if itemId and slots[DragNDrop[2]][5][2] > items[itemId][3] then
                print("Stack overflow in slot", DragNDrop[2])  
            end

            DragNDrop = nil
            return
        end
    end

    slots[DragNDrop[2]][5] = DragNDrop[1]
    DragNDrop = nil
end

ddEventHandler("onClientClick", root, function(Button, State)
    if IsInvShow then
        for i, slot in ipairs(slots) do
            if isMouseInPosition(sx * slot[1], sy * slot[2], sx * slot[3], sy * slot[4]) then
                if Button == "right" and State == "down" and slot[5][1] then
                    ClickOnItem(slot[5], i)
                    return
                elseif Button == "left" then
                    if not slot[5][1] then
                        if State == "down" then
                            WindowMove = {getCursorPosition() - InvPos[1], getCursorPosition() - InvPos[2]}
                        else
                            WindowMove = {false, false}
                        end
                    else
                        if State == "down" then
                            DragNDrop = {slot[5], i}
                            slot[5] = {false}
                        elseif DragNDrop then
                            ItemMove()
                        end
                    end
                end
                return
            end
        end
    end
end)

 

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...