Jump to content

[HELP] inventory system


iwalidza

Recommended Posts

2 minutes ago, iwalidza said:

i now it how i can do it i want check if player is still clicking

i use on client click

its do that

I think that with "clicking" you are not referring to the repeat motion of clicking the mouse button BUT the motion of leaving your finger on the mouse button clicked? You can query this information using the getKeyState function by passing the "mouse1" string to it for the left mouse button, "mouse2" for the right mouse button.

Link to comment
34 minutes ago, The_GTA said:

I think that with "clicking" you are not referring to the repeat motion of clicking the mouse button BUT the motion of leaving your finger on the mouse button clicked? You can query this information using the getKeyState function by passing the "mouse1" string to it for the left mouse button, "mouse2" for the right mouse button.

function Inventory:drawSlot (slot,count)
    local mousePosition = Vector2(getCursorPosition())
    self.mouseX = (mousePosition.x * screen.x- (self.iconSize / 2)
    self.mouseY = (mousePosition.y * screen.y- (self.iconSize / 2)
    if not self.getSlotInfo[4then return end
    dxDrawImage(self.mouseX,self.mouseY,self.iconSize,self.iconSize,"image/items/"..tonumber(self.items[self.getSlotInfo[4]][1])..".png")
    local countWidht = dxGetTextWidth(self.items[self.getSlotInfo[4]][2],1,"default-bold")
    dxDrawText(self.items[self.getSlotInfo[4]][2] ,(self.mouseX + (self.iconSize)) - countWidht,(self.mouseY + (self.iconSize - 20) ),0,0,tocolor(255,255,255),1,"default-bold",'left','top',false,false,true)
    dxDrawRectangle(self.mouseX,self.mouseY,self.iconSize,self.iconSize,tocolor(10,10,10,50))
end
 
function Inventory:drag(button,state,mouseX,mouseY)
    if button == 'left' then
        if state == 'down' then
            self.getSlotInfo = {self:getSlot(mouseX,mouseY)}
            firsSlot = self.getSlotInfo[4]
            addEventHandler('onClientRender',root,self.fSlot)
        end
        if state == 'up' then
            self.getSlotInfo = {self:getSlot(mouseX,mouseY)}
            print(self.getSlotInfo[4])
            self:moveSlots(firsSlot,self.getSlotInfo[4])
            removeEventHandler('onClientRender',root,self.fSlot)
        end
    end
end
 
i can make it better?
and work good .
that not work

@The_GTA

 

what i want :

i want if player still clicking in the slot he can drag him and change slot of item

Link to comment
2 minutes ago, The_GTA said:

I don't see any fundamental design problems in your code. But you seem to have trouble with taking the right parameter to the right place. What does the getSlot method do and how is it defined?

function Inventory:getSlot (x,y)
    for i = 1self.slots do
        local t = i - 1
        local xOffset = self.startX + (t*(self.iconSize + self.padding)) 
        local yOffset = self.startY + (math.floor(t/6)*(self.iconSize + self.padding))
        xOffset = xOffset - math.floor(t/6)*(6*(self.iconSize + self.padding))
 
        if isInBox(x,y,xOffset,xOffset + self.iconSize,yOffset,yOffset + self.iconSizethen
            return self.type,xOffset,yOffset,i
        end
    end
    return nil,nil,nil,nil
end
Link to comment
2 minutes ago, The_GTA said:

Oh, I remember. How about moveSlots?

function Inventory:moveSlots(slot1,slot2)
    if not slot1 and not slot2 then return end
    
    local selectedSlot1 = self.items[slot1]
    local selectedSlot2 = self.items[slot2]
    
    self.items[slot1] = selectedSlot2
    self.items[slot2] = selectedSlot1
end
 
Link to comment
local function invMoveSlots(sinv, sslot, tinv, tslot)
    if not sslot or not tslot or not sinv or not tinv then return end

    local selectedSlot1 = sinv.items[sslot]
    local selectedSlot2 = tinv.items[tslot]

    sinv.items[sslot] = selectedSlot2
    tinv.items[tslot] = selectedSlot1
end

local isdragging = false;
local draginv = false;
local dragslot = false;
local slotrender = false;

function Inventory:drag(button,state,mouseX,mouseY)
    if button == 'left' then
        if state == 'down' then
            self.getSlotInfo = {self:getSlot(mouseX,mouseY)}
            firsSlot = self.getSlotInfo[4]
            if (firsSlot) then
                isdragging = true;
                draginv = self;
                dragslot = firsSlot;
                slotrender = self.fSlot;
                addEventHandler('onClientRender',root,self.fSlot)
            end
        end
        if state == 'up' then
            if (isdragging) then
                self.getSlotInfo = {self:getSlot(mouseX,mouseY)}
                print(self.getSlotInfo[4])
                if (self.getSlotInfo[4]) then
                    invMoveSlots(draginv, dragslot, self, self.getSlotInfo[4])
                end
                removeEventHandler('onClientRender',root,slotrender)
                isdragging = false;
                draginv = false;
                dragslot = false;
                slotrender = false;
            end
        end
    end
end

This is just an example and does not contain the code for cross inventory dragging. You have to implement the list of inventories to do it.

Edited by The_GTA
Link to comment
3 minutes ago, The_GTA said:
local function invMoveSlots(sinv, sslot, tinv, tslot)
    if not sslot or not tslot or not sinv or not tinv then return end

    local selectedSlot1 = sinv.items[sslot]
    local selectedSlot2 = tinv.items[tslot]

    sinv.items[sslot] = selectedSlot2
    tinv.items[tslot] = selectedSlot1
end

local isdragging = false;
local draginv = false;
local dragslot = false;

function Inventory:drag(button,state,mouseX,mouseY)
    if button == 'left' then
        if state == 'down' then
            self.getSlotInfo = {self:getSlot(mouseX,mouseY)}
            firsSlot = self.getSlotInfo[4]
            if (firsSlot) then
                isdragging = true;
                draginv = self;
                dragslot = firsSlot;
                addEventHandler('onClientRender',root,self.fSlot)
            end
        end
        if state == 'up' then
            if (isdragging) then
                self.getSlotInfo = {self:getSlot(mouseX,mouseY)}
                print(self.getSlotInfo[4])
                invMoveSlots(draginv, dragslot, self, self.getSlotInfo[4])
                removeEventHandler('onClientRender',root,self.fSlot)
                isdragging = false;
                draginv = false;
                dragslot = false;
            end
        end
    end
end

 

out error bad use 

                addEventHandler('onClientRender',root,self.fSlot)
 
 
Link to comment
function Inventory:constructor(type, slots, startX, startY, iconSize, padding, isOpen, color)
    self.type = type or 'player'
    self.items = {}
    self.slots = slots or DEFAULT_Slots
    self.startX = startX or 0
    self.startY = startY or 0
    self.iconSize = iconSize or DEFAULT_iconSize
    self.padding = padding or DEFAULT_padding
    self.isOpen = isOpen
    self.color = color or DEFAULT_Color
 
    self.fDraw = bind(self.drawself)
    self.fDrag = bind(self.dragself)
    self.fSlot = bind(self.drawSlotself)
    if self.isOpen then 
        self:registerEvent("onClientRender"rootself.fDraw)
        self:registerEvent("onClientClick"rootself.fDrag)
    end
end
1 minute ago, The_GTA said:

Yea so what? What is your fSlot anyway? Are you being serious?

function Inventory:drawSlot (slot,count)
    local mousePosition = Vector2(getCursorPosition())
    self.mouseX = (mousePosition.x * screen.x- (self.iconSize / 2)
    self.mouseY = (mousePosition.y * screen.y- (self.iconSize / 2)
    if not self.getSlotInfo[4then return end
    dxDrawImage(self.mouseX,self.mouseY,self.iconSize,self.iconSize,"image/items/"..tonumber(self.items[self.getSlotInfo[4]][1])..".png")
    local countWidht = dxGetTextWidth(self.items[self.getSlotInfo[4]][2],1,"default-bold")
    dxDrawText(self.items[self.getSlotInfo[4]][2] ,(self.mouseX + (self.iconSize)) - countWidht,(self.mouseY + (self.iconSize - 20) ),0,0,tocolor(255,255,255),1,"default-bold",'left','top',false,false,true)
    dxDrawRectangle(self.mouseX,self.mouseY,self.iconSize,self.iconSize,tocolor(10,10,10,50))
end
Link to comment
local function invMoveSlots(sinv, sslot, tinv, tslot)
    if not sslot or not tslot or not sinv or not tinv then return end

    local selectedSlot1 = sinv.items[sslot]
    local selectedSlot2 = tinv.items[tslot]

    sinv.items[sslot] = selectedSlot2
    tinv.items[tslot] = selectedSlot1
end

local isdragging = false;
local draginv = false;
local dragslot = false;
local slotrender = false;

function Inventory:drag(button,state,mouseX,mouseY)
    if button == 'left' then
        if state == 'down' then
            if (isdragging == false) then
                self.getSlotInfo = {self:getSlot(mouseX,mouseY)}
                firsSlot = self.getSlotInfo[4]
                if (firsSlot) then
                    isdragging = true;
                    draginv = self;
                    dragslot = firsSlot;
                    slotrender = self.fSlot;
                    addEventHandler('onClientRender',root,self.fSlot)
                end
            end
        end
        if state == 'up' then
            if (isdragging) then
                self.getSlotInfo = {self:getSlot(mouseX,mouseY)}
                print(self.getSlotInfo[4])
                if (self.getSlotInfo[4]) then
                    invMoveSlots(draginv, dragslot, self, self.getSlotInfo[4])
                end
                removeEventHandler('onClientRender',root,slotrender)
                isdragging = false;
                draginv = false;
                dragslot = false;
                slotrender = false;
            end
        end
    end
end

Your system is fundamentally flawed because it does not contain the global inventory list but you are ignoring my messaging so I won't remind you any further.

Edited by The_GTA
Link to comment
7 minutes ago, The_GTA said:
local function invMoveSlots(sinv, sslot, tinv, tslot)
    if not sslot or not tslot or not sinv or not tinv then return end

    local selectedSlot1 = sinv.items[sslot]
    local selectedSlot2 = tinv.items[tslot]

    sinv.items[sslot] = selectedSlot2
    tinv.items[tslot] = selectedSlot1
end

local isdragging = false;
local draginv = false;
local dragslot = false;
local slotrender = false;

function Inventory:drag(button,state,mouseX,mouseY)
    if button == 'left' then
        if state == 'down' then
            if (isdragging == false) then
                self.getSlotInfo = {self:getSlot(mouseX,mouseY)}
                firsSlot = self.getSlotInfo[4]
                if (firsSlot) then
                    isdragging = true;
                    draginv = self;
                    dragslot = firsSlot;
                    slotrender = self.fSlot;
                    addEventHandler('onClientRender',root,self.fSlot)
                end
            end
        end
        if state == 'up' then
            if (isdragging) then
                self.getSlotInfo = {self:getSlot(mouseX,mouseY)}
                print(self.getSlotInfo[4])
                if (self.getSlotInfo[4]) then
                    invMoveSlots(draginv, dragslot, self, self.getSlotInfo[4])
                end
                removeEventHandler('onClientRender',root,slotrender)
                isdragging = false;
                draginv = false;
                dragslot = false;
                slotrender = false;
            end
        end
    end
end

 

slot not change

if (self.getSlotInfo[4]) then
        print('move')
        invMoveSlots(draginv, dragslot, selfself.getSlotInfo[4])
end
nothing out 
Edited by iwalidza
Link to comment
  • 5 weeks later...
On 27/01/2022 at 17:54, The_GTA said:
    local slot = #self.items
    while (count > 0) and (slot > 0) do
        if self.items[slot][1] == itemID then
            local canTake = math.min(count,self.items[slot][2])
            self.items[slot][2] = self.items[slot][2] - canTake
            if self.items[slot][2] == 0 then
                table.remove(self.items,slot)
            end
            count = count - canTake
        end
        slot = slot - 1
    end
    if (count > 0) then
        outputDebugString("not taken entire count");
    end

slot parameter to Inventory:takeItem has been neglected.

how i can do it to add items

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