Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is used to make this inventory dragging thing work?

Asked by 4 years ago

https://gyazo.com/1788876609151a856982228d6466bbed

I was thinking using math to check whether the mouse.X is greater or lower than a certain value. But that would just be bad if I decide to make an inventory with many of those. What is used to make this work and what should I use?

1 answer

Log in to vote
2
Answered by
pidgey 548 Moderation Voter
4 years ago

result preview Yes. Check if the mouse's position is within a certain magnitude. I used a collection of these frames in my GIF. Firstly, when the user stops dragging the frame, it sorts the collection of frames by which is closer to the Mouse's position.

--this environment is a function connected to the InputEnded event
do
    local mousePos = Vector2.new(input.Position.X, input.Position.Y) --cast Vector3 to Vector2
    table.sort(frames, function(f1, f2) --frame1, frame2
        --these two variables are the new position that is the center of the frame
        local f1Pos = f1.AbsolutePosition + f1.AbsoluteSize / 2
        local f2Pos = f2.AbsolutePosition + f2.AbsoluteSize / 2

        return (mousePos - f1Pos).Magnitude < (mousePos - f2Pos).Magnitude --returns if f1 is closer than f2 relevant to the mouse
    end)
end

'frames' is the table of frames (frameCollection:GetChildren()) which is sorted after that algorithm. frames[1] is the closest frame to the mouse. You then should be able to assign the marker frame to the closest frame (frame[1]), like so:

marker.Position = UDim2.new(0, frames[1].AbsolutePosition.X, 0, frames[1].AbsolutePosition.Y)

To actually move the marker, I connected a function to RenderStepped which checks for dragging (boolean). If true then move the marker to the Mouse's position. If you're wondering, here is the hierarchy of the Gui. and here is the full Model in case you want it for further study

Hope it helped!

0
Here is the source code: https://pastebin.com/AM4pAPuc - sorry about the lack of documentation. pidgey 548 — 4y
Ad

Answer this question