Whilst developing mechanics for my upcoming Scrapyard game, I had this interesting thought where, as you salvage materials from wreckage, the parts that make up the trash model gradually shrink to nothingness until, when the Model has no base parts left inside, it gets deleted.
So, to add to this concept, I developed a function findLongest(part)
.
What does the code do?
The function does this:
function findLongest(part)
local s = part.Size -- Get the part's size.
local x = s.X
local y = s.Y
local z = s.Z
if x>y and x>z then -- X is longer than Y and Z
return "X"
elseif y>x and y>z then -- Y is longer than Z and X
return "Y"
elseif z>x and z>y then -- Z is longer than X and Y
return "Z"
elseif x==y and y>z then -- Z is shorter than X and Y; X:Y
return "XY"
elseif y==z and z>x then -- X is shorter than Y and Z; Y:Z
return "YZ"
elseif z==x and x>y then -- Y is shorter than Z and X; Z:X
return "XZ"
else -- X == Y == Z
return "XYZ"
end
end
- If all of a part’s sides are not equal to each other, it will return the longest of the sides.
- If two of the sides are equal to each other and longest of them all, it will return these two sides.
- If these two last statements are false, then this means the object is equilateral, so
return "XYZ"
.
FindLongestDemo.rbxl (23.5 KB)
This .rbxl file demonstrates the function.
What are you not satisfied with?
I am not sure if the code is efficient enough with the long if-elseif-else
chain.
What potential improvements have you considered?
So, I was thinking, “Could I do something else that is better than a long if-elseif-else
chain?”
How (specifically) do you want to improve the code?
I am trying to optimize the function by finding a better alternative to a long if-elseif-else
chain; if you have a better method than the chain, please notify me. Otherwise, if the code is fine enough, you could say that too.