Default Anchor Point

Currently, the default anchor point is always set to (0,0) while I always use it (0.5,0.5). Is there a way of changing the default anchor point, so I don’t have to do it all the time?

(This is not a future request, I’m just asking)

1 Like

You could easily create a plugin to change the anchorpoint on DescendantAdded. If you want I could quickly make it tomorrow.

4 Likes

That would be much appreciated!

Here’s an example of what @GammaShock was saying. Maybe he has something better, but thought I’d share what I’ve done in the past for things like this:

local ANCHOR_POINT = Vector2.new(0.5, 0.5)

local runService = game:GetService("RunService")

game:GetService("StarterGui").DescendantAdded:Connect(function(object)
	if (runService:IsRunMode()) then return end
	if (object:IsA("GuiObject")) then
		object.AnchorPoint = ANCHOR_POINT
	end
end)

Something to note though: DescendantAdded isn’t just fired when you create a new object. It fires when an object’s parent is set to be a descendant. Thus, I can imagine this affecting UI building in unexpected ways, especially if you’re using code to generate any of your UI. That being said, the code turns itself “off” if it’s in run-mode.

Optionally, you could create a plugin button so that you could toggle the feature on/off. Otherwise, you can literally save that code in a script as a plugin, and it will just run in the background.

Edit: I think going into RunMode would reset the plugins, thus the line checking IsRunMode might just be able to be placed outside the event listener function.

3 Likes

You might want to keep track of whether the object has ever been in the game hierarchy, and only set it if this is the first time it enters the hierarchy. Otherwise, it will reset the AnchorPoint every time someone deletes and undoes a deletion.

Maybe also check if it doesn’t fire DescendantAdded on place load, that could be problematic if it does. An arbitrary wait(x) on top of the script may fix that.

3 Likes

Good ideas. Here’s an updated version:

local ANCHOR_POINT = Vector2.new(0.5, 0.5)

if (game:GetService("RunService"):IsRunMode()) then return end
local tracked = {}

function DescendantAdded(object)
	if (object:IsA("GuiObject") and (not tracked[object])) then
		tracked[object] = true
		object.AnchorPoint = ANCHOR_POINT
	end
end

function DescendantRemoving(object)
	if (object:IsA("GuiObject") and (not tracked[object])) then
		tracked[object] = true
	end
end

wait(1)
game.DescendantAdded:Connect(DescendantAdded)
game.DescendantRemoving:Connect(DescendantRemoving)

This behavior will only set the AnchorPoint when originally placed into the game hierarchy. I removed the check for StarterGui because there could be instances where you would want to place this into a SurfaceGui or BillboardGui within the Workspace. All new GUI objects are tracked in a table.

3 Likes

You also need to check DescendantRemoved and add them to the added table if they are a GuiObject. Right now if I delete an existing item in my place file and undo it, your script will reset its AnchorPoint.

5 Likes

Oops, forgot to do that. Just updated my last post to include that.

3 Likes

Decided while making the plugin to test whether this actually effected the AnchorPoint but when I changed the AnchorPoint > Deleted > Undid and even tried re-parenting, the AnchorPoint did not change from it’s newly changed AnchorPoint.

What I did notice is that the game.DescendantAdded effected UI’s inside of CoreGui which I had to fix up. I’ll post what I’ve done shortly.

@Aorda Here is the plugin: https://www.roblox.com/library/1338200775/MiddleAnchorPoint

Source if anyone wants to take a look: https://pastebin.com/CfP5MxKs

2 Likes

Thanks man! I appreciate it!

1 Like

No problem, make sure to mark my post as a solution if this helped you!

There you go! Thanks again!

1 Like