should this be in code review? idk
i’m not necessarily asking for a global code review, just asking if there’s a better way to do what i did
the problem is:
- i technically loop through all the pois in the given folder
- each iteration, i call the :GetChildren() method so i can get the random poi
this is my main complaint, i don’t really like this part
function SpawnPoisFromGivenList(listOfPois: Folder, tileToSpawnOn: BasePart)
-- loop through all the pois in the list
local amountOfPois = #listOfPois:GetChildren()
for i = 1, amountOfPois, 1 do
-- then, for every iteration, generate a number between 1
-- and the amount of pois in the folder
local randomNumber = math.random(1, #listOfPois:GetChildren())
-- after generating the random number, get the poi
local chosenPoi = listOfPois:GetChildren()[randomNumber]
-- see if the poi exists already, if it does
-- skip the iteration
if mapContentFolder:FindFirstChild(chosenPoi.Name) then continue end
-- otherwise, clone it, parent it and then pivot it
-- along with a random orientation
local clonedPoi: Model = chosenPoi:Clone()
clonedPoi.Parent = mapContentFolder
clonedPoi:PivotTo(tileToSpawnOn.CFrame)
-- get a random orientation (0, 90, 180, 270)
-- by generating a random number between 0 and 3, and then
-- multiplying it by 90
-- rocket science!
local randomOrientation = math.random(0, 3) * 90
-- and now, rotate the clonedPoi by the random orientation
-- on the Y axis
clonedPoi:PivotTo(clonedPoi:GetPivot() * CFrame.Angles(0, math.rad(randomOrientation), 0))
-- now, we gotta move the spawn parts of the poi to the main folder
-- that's tracked by everything, otherwise the game won't
-- spawn objects (and enemies)
MoveSpawnPartsOfPoiToGlobalFolder(clonedPoi)
-- after all of that, set the occupied attribute of the tile to true
tileToSpawnOn:SetAttribute("Occupied", true)
end
end