Hello, so I’m working on a laser machine, which fires one solid laser beam object that is anchored. The catch is that this laser can deflect into as many mirror objects that are in its path. If a mirror is found, another ray is created (so this is basically recursion for as many rays that hit additional mirrors.) And the laser drawing process basically repeats for each reflection that is found, I’ve done this already with no issues.
However. The problem I’m having is figuring out how I can efficiently store this information in memory so I can clean up lasers that are deflecting off a mirror in which the laser prior is no longer hitting, if that makes any sense.
Here’s a picture of my scenario if that’s more helpful:
local mirrors = {}
while wait() do
for i,v in pairs(mirrors) do
if v == nil then --check if v no longer exists
--laser1 will hit mirrors[i + 1] or something like that
end
end
end
Maybe have an object value as a child of the mirror, and the object value will contain the object that they are deflecting, for example, if the first mirror was deflecting the laser from the laser, then the object values value would be the laser. You can check if the object values object has changed every frame or something similar to that and if it did, you can check if it can still be deflected.
Solution 2
The second solution is to have each mirror control each ray, so if a ray hit the mirror, the mirror would respond by deflecting the ray, and it keeps checking if the mirror is having a ray casted onto it, and once it stops, it also stops deflecting a ray.
I have an idea: make a dictionary containing the Mirrors:
local Mirrors = {}
-- when you create a new Mirror, make a profile for it:
Mirrors[newMirror] = {}
-- then when you create the rays...
table.insert( Mirrors[intoThisMirror], TheRay )
-- then everytime you update the Mirrors table, call this (make sure you disconnect the old connections)
function updateMirrorConns()
for _, mirror in pairs(Mirrors) do
mirror.AncestryChanged:Connect(function(_, parent)
if not parent then
Mirrors[mirror] = nil
end
end)
end
end