Lets say I have these 2 tables:
ads = [18941,132582,28539, 23586] --Presume they're all decal asset id's sound = [189415,29356,289356, nil] --Presume these are sound ID's
I have a script that randomly selects a value from the ads table. I want to find the corresponding soundId, so the first ads id matches with the first sound ID. The second ads id matches with the second sound Id etc.
A little confusing, but I hope you can help me :D
This is not how I would do this. I will show you how to do this using your approach though, but I will also show you a way to make this faster and more efficient.
If you try to find the index you just go over the whole table to find the value and then return that index. In other words if your table has 500 items it will take a max of 500 iterations. That is all thrown away CPU time which you could also spend on something else!
ads = {18941,132582,28539, 23586} sounds = {189415,29356,289356, nil} function FindIndex(table, value) for index, val in pairs(table) do if val == value then return index end end end -- Presume that ad is 132582 - index 2; local index = FindIndex(ads, 132582) print("Sound: "..sounds[index])
Note that tables are constructed with {}
and not with []
:)
My approach would be to link the ads and the sounds together, so you don't need to find the right index.
It would look like this:
a = {{18941, 189415}, {132582, 29356}}
So a[2][1] is 132582; that's the ad. a[2][2] is the sound id: 29356.
I'll also provide a quick convertor.
ads = {18941,132582,28539, 23586} sounds = {189415,29356,289356, nil} local Catalog = {} for i,v in pairs(ads) do Catalog[i] = {v, sounds[i]} end function GetAd(index) return Catalog[index] and Catalog[index][1] end function GetSound(index) return Catalog[index] and Catalog[index][2] end print(GetAd(1), GetSound(1))
Note that the and operator is used to prevent errors when you try to index an ad which does not exist - if we don't do that your code will error, now the function just returns nil.
I hope this helped, if you have any questions about this code or approach feel free to comment.
Three* options.
One) Pick a random index, and use the same index for both tables.
local adid = math.random(1,#ads); local decal = ads[adid]; local sound = sounds[adid]; -- I would recommend the plural for the sound list -- because "ads" is parallel as well
Or, Two) Structure your data so that they are paired.
local ads = { {sound=189415,image=18941}, {sound=29356,image=132582}, -- etc }; local ad = ads[math.random(1,#ads)]; local decal = ad.decal; local sound = ad.sound;
I don't think in this case either is really a better solution, but you may prefer one.
This last one is not preferable, but depending on how you are doing this, it may be the only option.
Three) Find the index that the given random decal is at, and then use that to find the sound.
-- (assuming `decal` is already defined as -- an image asset id, chosen somehow from -- `ads` already) function tablefind(table,value) for dex,al in pairs(table) do if al == value then return dex; end end end local index = tablefind(ads,decal); local sound = sounds[index];
It would be much simpler to store both tables in one table, like so:
--Intead of this: ads = [18941,132582,28539, 23586] sound = [189415,29356,289356, nil] --Do this: ads = { {18941,18941}; --First number is the ad, second number is the sound {132582,29356}; {28539,289356}; {23586,nil}; }
However, if you wanted to keep both tables, you could just do this:
ads = [18941,132582,28539, 23586] sound = [189415,29356,289356, nil] randomAd = math.random(1,#ads) --Picks a random number between 1 and the size of the table chosenAd = ads[randomAd] --Gets the element from the table who's position is randomAd chosenSound = sound[randomAd] --Picks the sound from the sound table with the same position as randomAd
Hope this helped!