How can I check if there's a part above that part?

Hey there, I have made a procedural generation world where it uses “Perlin Noise” to make the map. A problem I ran into is that all blocks are the same which I fixed with there position on the Y, but now I ran into a bigger problem. How can I change the part if its under another part? Im wanting to do a Grass and Dirt set up where if there is a block below the grass it would become dirt. I was thinking of using the onTouch event but figured that wont work since it get touched from surrounding parts. As well as if there isn’t a block then it would be grass, or any block. So long story short, How can I set a parts color based on if there’s another part above it/on top of it?

How its set up:

If you store the values of your blocks in a 3 dimensional matrix format, you could easily extract information regarding the surroundings.

How about would I do that? Is it like putting the parts position in a array? I found this post, is this what your talking about? 3D Arrays in Lua? - #11 by Elttob but how would I apply it?

Example:

Block Numbers:

0 - Air
1 - Dirt
2 - Grass
3 - Stone

Now, let’s say I wanted to generate grass with a block of stone on top, within a 3x3x3 space.
The format would look like this:
[[0, 0, 0,
2, 2, 2,
1, 1, 1],

[0, 1, 0,
2, 2, 2,
1, 1, 1],

[0, 0, 0,
2, 2, 2,
1, 1, 1]]

You see there’s 3 matrices. Each of them represents a slice, or a plane - this gives us the Y coordinate. Inside them, the rows represent the Z coordinates and the columns represent the X coordinates. The numbers inside the matrices represent the block type. This way we know where each block is located and what type it is.

Edit: Adding to this, you can rotate the plane axis to your preference.

So I made something like this:

local function create_3D_Array(rows, cols, defaultValue)
	local arr = {}
	
	for x = 1, rows do
		arr[x] = {}
		for y = 1, cols do
			arr[x][y] = defaultValue
		end
	end
	
	return arr
end

local colums = 3
local rows = 3
local partType = 0
local newArray = create_3D_Array(rows, colums, partType)

print(newArray[rows][colums])

Is this how it should be done?

That’s actually a 2D array. But by introducing a new function, you can give it a 3rd dimension.

local function extrude(array, amount)
    local newArr = {}
    
    for e = 1, amount do
        newArr[e] = array
    end

    return newArr
end

(By the way rename the create_3D_Array function to create_2D_Array)
Let’s combine both:

local columns = 3
local rows = 3
local extrusion = 3
local defaultPartType = 0
local partArray = extrude(create_2D_Array(rows, cols, defaultPartType), extrusion)

Now, by using coordinate indexing you can access the part at any given point.

partArray[1][1][1] --To access the part type at (0,0,0)
2 Likes