I’m in the process of designing an editor for an upcoming game where the player can build whatever they want to. What I’m trying to achieve is a wall editor where the wall will position itself along a selected edge, which means you don’t have to rotate the walls yourself.
For example, Retail Tycoon uses this system for its walls. They align along the closest edge to the mouse.
How can I get the closest edge of a part to the mouse and then position the wall on it?
I first thought of the idea of using multiple parts to represent the edges and the center, but this doesn’t seem like a good way to do it especially since there are ~ 1000 grid spaces, which would each have 5 parts = 5000 parts per plot (not very good)
Ok, I got it to work with the left and right sides, thanks! Now I just have to do front and back and then get the lowest distance from them all. I originally tried to simply put them in a table with this format {Left = (mouseP - edgeP).magnitude} and then do table.sort but I haven’t really experimented much with tables.
Any way I can sort a table of tables (array), or a different method?
Yeah, its just I need to work out which distance corresponds to which edge, because a table returning {1, 2, 3, 4} doesn’t tell me which edge is which. I’ll probably go with @Jaycbee05 and make my own sort function
I got a method that sorts the array (I had to look it up as I got confused) however all it outputs is nil:
local function sortArray(dict)
local array = {}
for key, value in pairs(dict) do
array[#array+1] = {key = key, value = value}
end
table.sort(array, function(a, b)
return a.value > b.value
end)
return dict
end
local function gCE()
Table = {
['Left'] = (m.Hit.p - left.p ).magnitude,
['Right'] = (m.Hit.p - right.p).magnitude,
['Front'] = (m.Hit.p - front.p).magnitude,
['Back'] = (m.Hit.p - back.p ).magnitude
}
Table2 = sortArray(Table)
return Table2
end
I used httpservice to read the table and it is sorting it (although its biggest to smallest rn) however the output of the first value is nil.
I worked it out! Truns out what I was sorting wasn’t a table of tables, which I have now resolved. Everything works now, I’m getting the correct named side that my mouse is focused on. Thanks all!