So this isn’t working and i’m kinda stumped (I know the matrix isn’t empty)
local pixelMatrix = require(script.Parent.Modules.Setup)
local count = 0
for x=0,#pixelMatrix do
for y=0,#pixelMatrix[x] do
count += 1
end
end
print(count)
You haven’t explained what the purpose is. From what I gather, you are computing the count of the matrix which is what my previous answer is doing.
Also, the += operator does not exist in Lua. I take it that you come from a Python background. The i, v in pairs() loop is the most versatile loop. I omitted the variable for the indices in the two loops on my previous answer, but if you need those you can just name the variables and use them.
But for an integer matrix, I don’t think enumeration will be that much more inefficient. Whatever memory is used for enumeration will be quickly garbage collected, if it isn’t utilized.
Can you show a sample of the matrix, so we have an idea of how the table is formatted?
You could have seen it if you downloaded the file but this is how the matrix is constructed
local resolutionX = workspace.CurrentCamera.ViewportSize.X/10
local resolutionY = workspace.CurrentCamera.ViewportSize.Y/10
local pixelMatrix = {}
for x=0, workspace.CurrentCamera.ViewportSize.X,resolutionX do
pixelMatrix[x] = {}
for y=0, workspace.CurrentCamera.ViewportSize.Y,resolutionY do
pixelMatrix[x][y] = Instance.new("Frame")
pixelMatrix[x][y].Size = UDim2.new(0,1*resolutionX,0,1*resolutionY)
pixelMatrix[x][y].Position = UDim2.new(0,x, 0, y)
pixelMatrix[x][y].Name = tostring(x).."-"..tostring(y)
pixelMatrix[x][y].Parent = script.Parent.Parent.Canvas
wait()
end
end
return pixelMatrix
They start from 1 when instancing them. However attempting to index them with stuff below -1 will simply create more logs. It is not a hard cap, it is a DSA, a scaling array (see memory management, the heap in C)
You might also consider removing the yield from your loop, as it’s not infinite it shouldn’t lock up, but you can correct me given your experience with the execution.
If starting from 1 doesn’t solve the issue, start using debug print statements to ensure you know how your matrix is being indexed, field by field.
So the reason it’s stopping on the first index is because while the matrix is using arrays, the array is indexed with floating points numbers rather than solely integers.
The Lua handbook stipulates that arrays cannot iterate through anything except integers in an array.
To solve this, you would need to index your matrix using a different method, though I’m not sure how you might do so without ruining your intended purpose of the matrix.
You could experiment with rounding, or more costly converting the index to a string, removing the decimal, and converting it back into a number…potentially allowing the array to iterate, though at the cost of a large array.
In the module I also returned the resolution on X and Y
local resolutionX = workspace.CurrentCamera.ViewportSize.X/10
local resolutionY = workspace.CurrentCamera.ViewportSize.Y/10
local pixelMatrix = {}
for x=0, workspace.CurrentCamera.ViewportSize.X,resolutionX do
pixelMatrix[x] = {}
for y=0, workspace.CurrentCamera.ViewportSize.Y,resolutionY do
pixelMatrix[x][y] = Instance.new("Frame")
pixelMatrix[x][y].Size = UDim2.new(0,1*resolutionX,0,1*resolutionY)
pixelMatrix[x][y].Position = UDim2.new(0,x, 0, y)
pixelMatrix[x][y].Name = tostring(x).."-"..tostring(y)
pixelMatrix[x][y].Parent = script.Parent.Parent.Canvas
wait()
end
end
return {pixelMatrix, resolutionX, resolutionY}
Here I simply loop through the viewport size with your given resolution on both axis
local pixelMatrix, resX, resY = unpack(require(script.Parent.Modules.Setup))
local count = 0
for x=0,workspace.CurrentCamera.ViewportSize.X, resX do
for y=0, workspace.CurrentCamera.ViewportSize.Y, resY do
count += 1
end
end
print(count)
Can’t you just floor the resolutionX and resolutionY values down? It’s becoming a floating point value because you’re dividing by an awkward value, I would just floor it down - and try to use power of 2 resolutions…