It’s really hard to understand what you’re trying to do.
Rant about variable naming
The SlideImages
variable name isn’t great. The way I read it is “SlideImages is a dictionary relating the keys (Slides) to values (Images)”. So the “Normal” slide has images 7, 9, 10, 12, etc. and the “Waving” slide has images 1 and 2. But judging from the comment, it’s actually the opposite. Associating each type of image with all of the slides it appears on.
Which variable name is the right one is obviously subjective, but usually a “SlideImage” is an image that appears on a slide, and an “ImageSlide” is a slide on which an image appears. A FishingPole is a pole that is used for fishing, while PoleFishing is fishing with a pole. It matters which word comes first.
🌟Here's how I'd do what you're trying to do 🌟
--An (image name) -> (list of slide numbers) dictionary.
--Each number is a slide number on which the associated image appears.
ImageSlides = {
Normal = {7, 9, 10, 12, 14, 16, 17, 18, 20, 22, 25};
Waving = {1, 2};
Crying = {5, 6};
Girls = {3, 4, 19};
Teacher = {8};
Mum = {11};
Dad = {13, 15, 24};
DadMystery = {21, 23};
}
--Given a slide number, returns the name of the image that appears on that slide.
--If no image appears on that slide, returns nil.
local function getSlideImage(slideNumber)
for imageName, imageSlideNumbers in pairs(ImageSlides) do
if table.find(imageSlideNumbers, slideNumber) then
return imageName
end
end
return nil
end
More ranting
This whole way of doing things is pretty weird though. Why not represent each slide as an object (not talking about OOP btw), and have an object property that describes which image appears on that slide? Right now you’re doing “Here’s all the images, and all the slides where they appear on”. In my mind it makes more sense to do “Here’s all the slides, which define which images appears on each slides”.
Not only does it make more sense in my brain, you also don’t need to search any tables to find out which image appears on a slide. You just do slide.Image
. Imagine you want to add a sound effect to each slide or something, with your old approach you’d have to define for each sound which slides they appear on, and then write a function to figure out which sound appears of which slide.
Another thing is that with your approach, each slide number could appear in multiple image categories. Typos are easy to make, or you might just forget to remove a slide number from an image category if you e.g. want to change which image appears on a slide. What happens when each slide has two images? That can be a hard- to- squash bug.
Here’s how I’d do the thing I described:
local Slides = {
{Image = "Waving", Text = "HI! :D", Sound = "sound1"},
{Image = "Waving", Text = "Welcome to my humble abode!"},
{Image = "Girls", Text = "I have no idea what this image is xD"},
{Image = "Girls", Text = "Why is it called Girls? Dunno", Sound = "sound2"},
{Image = "Crying", Text = "Now I feel sad ;c"}
--etc etcc
}
--Don't need this, just do slide.Image
local function getSlideImage(slide)
return slide.Image
end