A function that allows you to find parts in a region, given a white list is now enabled!

Discourse doesn’t like “FindPartsInRegion3WithWhitelist” for some reason, otherwise that would be in the title

Hello again, developers. Hope you all made it back safely from RDC and are ready to get back to working on your projects. Today we released something that I’m sure some of you have been hoping for, Workspace.FindPartsInRegion3WithWhiteList.

Starting off with the specifics, I implemented it in a similar way that I implemented FindPartOnRayWithWhitelist last year, which means there’s no functional difference between it and its IgnoreList counterpart, except one checks if parts are in the list provided, and one checks if they’re not.

Anyway, this allows you to provide a whitelist to check if any of its contents are parts within a provided region. Previously in order to do this, you had to either make an inverted blacklist, or look for all the parts and search the resulting table for the parts you want. Each case is not really ideal performance-wise, and is more work than should be necessary.

Here’s an example of how it works:

In this picture you’ll see I have 2 models, Reds, consisting of red parts, and Blues, consisting of blue parts

We are going to use this script to print out what we find:

local blues = workspace.Blues
local list1 = {reds, blues}
local list2 = {reds}

local region = Region3.new(
	Vector3.new(-100,-20, -100),
	Vector3.new(100,20,100)	
)

local parts = workspace:FindPartsInRegion3WithWhiteList(region, list1, 10000)
for i = 1, #parts do
	print(parts[i].Name)
end

print("-------------------TEST------------------------")

local parts = workspace:FindPartsInRegion3WithWhiteList(region, list2, 10000)
for i = 1, #parts do
	print(parts[i].Name)
end

print("-------------------TEST------------------------")

local parts = workspace:FindPartsInRegion3WithIgnoreList(region, list2, 10000)
for i = 1, #parts do
	print(parts[i].Name)
end

When this script runs, it prints out everything in the first list, which is all the red and blue parts, but not the Baseplate. Secondly it prints out everything in the second list, which is only red parts. Thirdly it prints out everything but red parts, including the Baseplate, since we’re using FPIR3WIL.

And that’s pretty much it. Let me know if you run into any issues and thanks for reading!

52 Likes

Thank you good sir!

Out of interest on the basis of whitelists, what is stopping an ignore list for pathfinding?
It’d be super useful for ignoring doors, or for example, selectively blocking off certain routes for SatNav style systems?

2 Likes

Yes this is amazing, just what I need. My only problem is that I’m horrid at creating Region3s, but this will definitely help. A lot of my old code used to recurse through the parts found and it just generally looked ugly to me because of how many lines I’d put for my items.

Easiest code for Region3 generation:

local function CreateRegion3FromLocAndSize(Position, Size)
	local SizeOffset = Size/2
	local Point1 = Position - SizeOffset
	local Point2 = Position + SizeOffset
	return Region3.new(Point1, Point2)
end
5 Likes

Does this work for rotated region 3s?

Awesome. Also I love the trend of incredibly-long method names.

3 Likes

I believe @AxisAngle has a module for that

Good luck writing these functions without intellisense!

3 Likes

FindPartsInRegionWithWhitelistWhichIsA(region, whitelist, num, class)

2 Likes

Nice one!

On a more on topic note - can we perhaps get a more concatenated function name? I understand it’d be hard to fit it in but it’ll be a bad trend if this continued :stuck_out_tongue:

1 Like

Ah self documenting code. You need fewer comments, but your method and variable names get larger.

10 Likes

The only thing that could make it better would be separating the words of the method with underscores