How to get the AbsoluteSize of an UI element with a math operation

Hello! Does anyone know how to get the AbsoluteSize of an UI element? I already have the size but I don’t know how to do it.

What I’m trying to do is like creating a button in a script and then getting the AbsoluteSize of that button but with a math operation, not getting the AbsoluteSize of the button like this: Button.AbsoluteSize.

Is this already answered? Where? Can you give me a link please? :sparkling_heart::sparkles:

I’m not sure why you’d ever need to do this when the AbsoluteSize property exists. Anyway, you need to start at the top of the hierarchy and work your way down calculating the AbsoluteSize of each one.

So first we need to find all of the object’s parents that are UI elements.

local function getAncestry(object)
	local result = {}
	while object and object ~= game do
		if object:IsA("GuiObject") then
			table.insert(result, object, 1)
		end
		object = object.Parent
	end
	return result
end

Now we have the ancestry such that the first element is the top most ancestor, the second is a child of the first, etc. Now we need to calculate the AbsoluteSize for all of them. If we’re avoiding AbsoluteSize altogether, then I’m gonna use the Mouse to get the size of the screen.

local ViewSize = Vector2.new(Mouse.ViewSizeX, Mouse.ViewSizeY)
local Ancestry = getAncestry(gui)
local AncestorSize = nil

for i, v in ipairs(Ancestry) do
	local ancestor = AncestorSize or ViewSize
	AncestorSize = Vector2.new(v.Size.X.Scale * ancestor.X + v.Size.X.Offset, v.Size.Y.Scale * ancestor.Y + v.Size.Y.Offset)
end

print(AncestorSize)

The main issue here is any UI constraints that override the properties, including Scale, AspectRatio, Size, GridLayout, ListLayout, etc. will result in the incorrect answer.

I also think your Viewport size might not correctly account for when the GUI inset is ignored or kept in, as the mouse properties would stay the same but the absolute size of a scaled object with the inset ignored would not.

@Sandessat what’s the use case? Why can’t you use .AbsoluteSize? Understanding a bit more about it might help to generate a solution that covers any edge cases you might run into.

1 Like

I’m actually using the module called Roact, that creates user UI with scripts and components, etc.

So I’m tyring to make a component that creates a button, I create it by using:

Roact.createElement("Frame", {
     Size = UDim2.new(0.5, 0, 0.5, 0)
}, {
     AspectRatioConstraint = Roact.createElement("UIAspectRatioConstraint", {
          AspectRatio = -- ????
     })
})

You can see in the AspectRatio the comment “???”, that means that I don’t know what to put there because I’m creating the frame first and not saving it into a variable to get it like: frame.AbsoluteSize.

If I get to save it into a variable, it will return a RoactElement, that haves the Type, etc information about the frame, but not the UI object.

So I don’t think there’s a way to get the AbsoluteSize by its property. Maybe I’m wrong…

The problem has been fixed, the solution was using Ref’s that Roact includes.