Spynaz Simple Drag GUI Module New Parent and Scaling Issues

I am using @Spynaz 's Simple Dragging GUI module which works great. Im designing an inventory where I drag UI elements then re-parent them to a new inventory slot. This helps with UI resizing and scaling rather than depending on pixel perfect dimensions.

When changing the parent of the object I am dragging the drag deviates. This is very likely due to scaling (as described in this post Spynaz Draggable Gui Module Help). But this is more closely related to the scaling of the new parents.

How do I resolve this? I dont have a video but retargerting a new parent shows the same behavior as in the video of the post.

I modified the module to accommodate this:

function DraggableObject:setParent(parent)
	if parent then
		self.Object.Parent = parent
	end
end

I believe I need to also update the new self.dragStart position relative to the new parents scaling?

4 Likes

Miraculously I figured it out. Heres how to calculate when dragging and dropping onto another parent (useful for things like inventory and slots). GL!

function DraggableObject:setParent(parent)
	if parent then
		-- Calculate the offset between the current parent and the new parent
		local currentPosition = self.Object.Position
		local currentParent = self.Object.Parent
		local currentAbsolutePosition = self.Object.AbsolutePosition
		local currentAbsoluteSize = self.Object.AbsoluteSize

		local newAbsolutePosition = parent.AbsolutePosition
		local newAbsoluteSize = parent.AbsoluteSize

		local offset = currentAbsolutePosition - newAbsolutePosition
		-- update for new delta calculation in update
		self.dragStart = offset
		-- Update the parent of the draggable object
		self.Object.Parent = parent
		-- Update the position of the object to adjust for the parent change
		local newPosition = UDim2_new(
			currentPosition.X.Scale,
			currentPosition.X.Offset + offset.X,
			currentPosition.Y.Scale,
			currentPosition.Y.Offset + offset.Y
		)
		self.Object.Position = newPosition
	end
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.