So i am just starting to implement tables
local data = {}
type into my code and have come up with a question.
So i have a local function
[lua]
local choice = {}
local ranNum = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
local rdny
local function RandomSelection()
local s
for s = 1, 5 do
rdny = math.random(#ranNum)
choice[s] = ranNum[rdny]
table.remove(ranNum, rdny) --remove that number.
end
end
– Option 2
local choice = {}
local function RandomSelection()
local ranNum = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
local rdny
local s
for s = 1, 5 do
rdny = math.random(#ranNum)
choice[s] = ranNum[rdny]
table.remove(ranNum, rdny) --remove that number.
end
end
[/lua]
So the first version forward declares choice, the table of possible ranNum , and rdny .
These can obviously be used within the function.
In option 2 i only declare choice outside as it will be used elsewhere in code, but ranNum and rdny are only needed for that function.
I wish ranNum to always have the possible 15 choices - to do this i believe it needs to be within the function so that each time the function is called the table is “repopulated” with all 15 numbers.
I assumed that if i were to do option 1 - declare the table - then do randomSelection after 3 - table would be empty and on 4th call produces an error.
By using prints i have confirmed this in the terminal.
So my feeling is option 2 is the best way to proceed. am i correct?
Question - So option 2 i declare ranNum, rdny & s for that matter as local within the function - What happens to these variables after the function is finished??
Technically ranNum will still have 10 numbers still in the table - So a Valid Variable?
Does lua say - " hey function finished - they were local to that function so hey don’t need them anymore - lets delete them from memory"
Or for correct programming do i need to do something to clean them up??
Thanks for any help clarification.
T.