How do I 'convert' an AbsolutePosition to Position?

For example, let’s say I have a 640x480 (using offset) container frame inside of a gui, centered with its anchor point set to 0.5;0.5 and its position set to (0.5, 0, 0.5, 0).

Now, If I have a new frame inside the container, this new frame’s size being 20x20 and its position being (0, 300, 0, 300) (just an example size & position) and then an image label inside this new frame, its position being (0, 10, 0, 0).

If I want to move this image label to the position (0, 20, 0, 460), relative to the original container frame, how could I do it? Since the image label’s position is relative to its parent frame, the 20x20 one?

An idea I had was to ‘convert’ the AbsolutePosition the imagelabel should have to an UDim2, but I have no idea how to do it…

get absolute position of parent frame, subtract it from absolute position of child frame, there you go, maybe also add half of the absolute size of the parent frame

1 Like

Assuming:

  • main frame (Size: 640x480 — Pos: (0.5, 0, 0.5, 0) — Anchor point: (0.5, 0.5))

    • absolute position: 319, 60
    • absolute size: 1280, 960
  • child frame (Size: 40x22 — Pos: (0.5, 20, 0.5, 403) — Anchor point: (0.5, 0.5))

    • absolute position: 639, 518
  • the image label (the object i’m trying to move, parented to the child frame)

    • size: 16x16
    • position: (0, 0, 0, 0)
    • anchor point: (0, 0)

It would be something like this, right?:
(639-319)+(1280/2), (518-60)+(960/2)
Trying to set the imagelabel’s position to that just moves the imagelabel off-screen, to (0, 960, 0, 938)

the same issue occurs even without adding half of the parent frame’s absolute size

local function getUDim2(guiObject):UDim2
	local absPosition = guiObject.AbsolutePosition 
	return UDim2.fromOffset(absPosition.X,absPosition.Y)
end

local mainFrameUDim2 = getUDim2(mainFrame)
local childFrameUDim2 = getUDim2(childFrame)
local addedPosition = UDim2.fromOffset(20,460)
local newPosition = mainFrameUDim2 + addedPosition - childFrameUDim2
imageLabel.Position = newPosition
1 Like

I’m wondering why you need do convert absolute position to relative position, maybe a custom cursor? Anyways, here’s how I would do it:

I will explain it using two frames.
The given absolute position: 500, 500
The absolute position of the parent frame: 300, 300
We have to subtract the parent frame’s absolute position from the child frame’s absolute position, giving you a relative position of 200, 200. (so (0, 200, 0, 200).
You also have to account for any offsets, ie if the anchorpoint of the parent frame is 0.5, 0.5, then you have to add half the size to the x and y.
For instance, the parent frame’s size is 400, 400, and the anchorpoint is 0.5, 0.5, meaning you have to add 200 to x & y.
This gives us a final relative position of (0, 400, 0, 400)

Shown in red is the starting absolute position 500, 500, black is the child frame at 400, 400 with an absolute position of 500,500
image
image

function AbsToUDim2Pos(ParentPos:UDim2, ParentSize:UDim2, ParentAnchorPoint:Vector2, AbsolutePos:UDim2)
	local child_x = AbsolutePos.X.Offset - ParentPos.X.Offset
	local child_y = AbsolutePos.Y.Offset - ParentPos.Y.Offset

	child_x += ParentSize.X.Offset * ParentAnchorPoint.X
	child_y += ParentSize.Y.Offset * ParentAnchorPoint.Y

	return UDim2.fromOffset(child_x, child_y)
end

local ParentPos = UDim2.fromOffset(300,300)
local ParentSize = UDim2.fromOffset(400, 400)
local ParentAnchorPoint = Vector2.new(0.5, 0.5)
local AbsolutePos = UDim2.fromOffset(500, 500)

print(AbsToUDim2Pos(ParentPos, ParentSize, ParentAnchorPoint, AbsolutePos))

-- LIVE DEMO: 
-- move the frame "target absolute position 500, 500"
-- and the child frame will move to the same position.
-- (((Apologies for the horrible frame names.)))
while task.wait() do
	local ParentPos = script.Parent["Parent Frame"].Position
	local ParentSize = script.Parent["Parent Frame"].Size
	local ParentAnchorPoint = script.Parent["Parent Frame"].AnchorPoint
	local AbsolutePos = script.Parent["Target absolute position: 500,500"].Position
	script.Parent["Parent Frame"]["Child frame"].Position = AbsToUDim2Pos(ParentPos, ParentSize, ParentAnchorPoint, AbsolutePos)
end

Feel free to modify the snippet to suit your needs.
If you would tell me why you need to convert absolute positions to relative positions, I could help you come up with a more robust solution.

1 Like

As for why I’m trying to do this, well, after thinking more clearly about it, using AbsolutePosition probably wouldn’t work, but my idea was to have a specific UDim2 location inside a 640x480 frame, and then have an imagelabel that’s inside of a small frame inside yet another frame inside the previously mentioned 640x480 frame, and then move the imagelabel to that position (relative to the original 640x480 frame).
I need to figure this out as I’m trying to make an accurate (or as accurate as it can be) undertale game system in Roblox, and I can’t figure out how to tween the Soul image (which is parented to the player, which is parented to the map, which is parented to a ‘World’ frame which contains all of the overworld’s contents) to a position relative to the ‘World’ frame.
This is what I have so far:

The code I’m currently using doesn’t work at all (as the soul gets tweened from the player to some random position in the map, instead of the correct position which is inside the ‘Fight’ button, where I have a copy of the soul already located), and I thought maybe using AbsolutePosition could help out but well, like I mentioned at the top of this reply, now that I’m thinking a little harder I realize that using AbsolutePosition probably wouldn’t actually solve my problem.
Regardless, thanks for the explanation, as I’m probably going to have to work with AbsolutePosition for many parts of the game so it’s definitely going to come in handy soon

if you want to base a GuiObject’s position on a different GuiObject as if it’s parented to the other GuiObject, I don’t know why my script above doesn’t work. just make sure that imageLabel is the GuiObject you want to move, childFrame is imageLabel’s parent, and mainFrame is the other GuiObject (the 640x480 frame in your game, I think)

The reason I didn’t try your code is because I realized that in the end AbsolutePosition wouldn’t work for what I’m trying to do, so I ended up with a much simpler solution (parent the imagelabel to the main frame, set the imagelabel’s initial position to the map’s position, then add the player’s position to it), and that ended up working

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