I saw a comment by Karma over on Discord about randomly turning off/on lights in a scene at Dusk/Dawn and thought that it would be pretty easy if you set up a Lua table containing all of the Light IDs, randomised the table and then just looped through it turning off (or on) each light. You could even have separate tables for each "district" in the city (N, NE, E, SE, S, SW, W, NW and CBD for example).
So, here is a script showing how to randomise a Lua table:
- Code: Select all
-- Function to Randomise a Lua Table
-- inTbl = Table to be Randomised
function randomiseTable ( inTbl )
-- Get the length of the Table
local nT = tableLength ( inTbl )
-- Loop BACKWARDS through the Table
while nT > 2 do
-- Get a Random Table Entry
local iRand = math.random ( nT )
-- Swap the Current Table Entry (nT) with the Random one (iRand)
inTbl[nT], inTbl[iRand] = inTbl[iRand], inTbl[nT]
-- Move to the next (i.e. previous) Table Entry
nT = nT - 1
end
-- Return the Randomised Table
return inTbl
end
-- Function to get the Length of a Table
-- inT = Table whose Length is required
function tableLength ( inT )
-- Initialise a Counter
local iC = 0
-- Loop through the Table
for _ in pairs ( inT ) do
-- Increment the Counter for each Table Entry
iC = iC + 1
end
-- Return the Counter
return iC
end
Hope this is helpful.
Regards
Shando
PS: I didn't write the randomise code, but I can't remember where I got it from (probably the main Lua site?)