Im basically trying to achive a thing where i have multiple objects next to each other and if 1 of them gets hot the others around it get hot too,with a temperature loss.
i tried doing 2d array but i just cant get it to work properly
first i only tried with 1 row but i didnt work out
array = {}
for i,v in pairs(workspace.blocks:GetChildren()) do
for x=1,11 do--this would represent the amount of blocks in a single row
array[x] = {}
for y=1,1 do--as im doing 1 row i set it to make only 1
array[x][y] = {}
end
end
array[i][1] = v.Number.Value--set the number in row 1 to the number of the blocks
end
for i=1,11 do
print(array[i][1])--prints out a completely different thing that i imagined
end
it basically just prints out
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
3
instead of all the numbers and i dont really know why
so if anyone could help me to set up the array properly i would be happy
That won’t work reliably because GetChildren doesn’t make any guarantees about the order that children are returned in. So the first child might be the 5th block or w/e. You’ll need a way of associating each (x, y) coordinate with a block or vise-versa. Here I’ll use the X and Z coordinates of each block’s position:
heatMap = {}
for _, block in pairs(workspace.blocks:GetChildren()) do
local x, y = block.Position.X, block.Position.Z
heatMap[x]=heatMap[x] or {}
heatMap[x][y] = block.Heat.Value
end
for x =1,11 do
print(array[x][1])
end
oo thank you so much that works much better,now have you got any idea how it could average the numbers but only in like a radius of 3? so basically i thought about looping trough the array 1 by one and seeing what temp is it at and then equalize the temp with the ones next to it,below it but i have no clue how could i detect the ones in the next Y colum
I usually use something like this for this kind of problem:
local v2 = Vector2.new
function mapSet(map, p, value)
local x, y = p.X, p.Y
map[x] = map[x] or {}
map[x][y] = value
end
function mapGet(map, p)
local x, y = p.X, p.Y
if map[x] then
return map[x][y]
end
end
That way you can get neighbors by changing X or Y coordinate by +1 or -1. E.g.
function getInRadius(p, r)
local result = {}
for x = p.X - r, p.X + r do
for y = p.Y - r, p.Y + r do
local new_p = v2(x, y)
local d = (new_p - p).Magnitude
if d <= r then
table.insert(result, new_p)
end
end
end
return result
end
As for updating the map you might have to do it all at once. Otherwise consider what happens when you update the 2nd block: it becomes the average of it’s surrounding blocks, one of which (the 1st) is already the average of several surrounding blocks. The way to do this is to have two maps, one for reading and one for writing. Once an entire update has been performed, which one is for reading and which one is for writing can be swapped (this is known in CG as double buffering).
oh wow lol this got complicated quickly,thank you so much for your help. i will try it out when i have the brain capacity to deal with this,thx once again
just a simple question,what is the reason to use it like this?
map[x] = map[x] or {}
also a thing that i found out is that the first digit is Y instead of X
since it goes like, array[colum][row],which is logical since it needs to get what row to choose the x from
So it just ensures that there’s a table to index into for the Y coordinate, allowing you to set values at whatever coordinates you want without initializing the map first with empty