Vector3:GetComponents()

Currently no function is available to return all of the components of a Vector3. However this functionality exists in CFrame.

It is possible to use this by constructing a CFrame from the Vector3 data type.

Example

local x, y, z = CFrame.new(Vector3.new(1,2,3)):GetComponents()

print(x,y,z) -- 1, 2, 3

Proposal

local x, y, z = Vector3.new(1,2,3):GetComponents()
print(x,y,z) -- prints 1, 2, 3

Why include this?

Overall I feel this would be adding syntactic sugar to the code by improving the readability of code written with a lot of Vector3/CFrame logic.

Secondly this would help to standardize functionality within Roblox because this could also be applied to some other Roblox data types to unpack its content as opposed to accessing each property directly.

43 Likes

Sorry for the visibility bump but wanted to +1 this

I appreciate this is more of a “nice to have” feature request but it would be sweet if we had a method to destruct/unpack both a Vector2 and a Vector3.

I’ve recently been doing a lot of geometry related/adjacent code and it has been somewhat tedious to write local x, y, z = ... out repeatedly compared to unpacking CFrame mats.

Would be incredibly grateful if this could get added to some “quick win” backlog at some point


P.S. while we’re here… I’ve seen that updating Vec3 components via index/key has been vetoed before due to some engine requirement, but would it be possible to get read access via indices? i.e.

local mat = CFrame.identity
for i = 1, 12, 1 do
  local component = mat[i]
  -- do stuff
end

local vec = Vector3.one
for i = 1, 3, 1 do
  local component = vec[i]
  -- do stuff
end
1 Like