Help with tables

So I am busy making a story type game similar to those old sutart games, but with scripting, guis, animation etc.

This is what I am trying to do:
Whenever a character in the story talks a gui looking like this will appear:

So depending on the slide, I want to change the image.
I could do a whole bunch of if statements but that would be very repetitive and annoying to add more slides.

So I this is what I have tried:

SlideImages = {
	Normal = {7, 9, 10, 12, 14, 16, 17, 18, 20, 22, 25};--The numbers are which slides these images are suppose to show, eg. Slide 1 will be waving.
	Waving = {1, 2};
	Crying = {5, 6};
	Girls = {3, 4, 19};
	Teacher = {8};
	Mum = {11};
	Dad = {13, 15, 24};
	DadMystery = {21, 23};
}
--and I also have a function responsible for finding out which image is needed:
local function getImage(SlideNumber)

end

--What I tried:
local function getImage(SlideNumber)
for i, v in pairs(SlideImages) do
		for index, value in pairs(v) do
			if table.find(v, SlideNumber) then
				print(v)--Prints the table id which I need the name off.
			end
		end
	end
end

If I could get the name of the table I could get the image I need for the slide and it would be much easier and more efficient to add more slides and images.

1 Like

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
2 Likes

Yeah I see

I am going to do something similar to that, thanks.

This worked thanks

1 Like

I made some changes and this is what I have:

local SlidesInfo = {
	[1] = {Text = "oaihidashdhasdhasoidhoasihdoai", Image = Images.Waving, CameraPoint = "Cp1"};
}

Thanks for the idea, Idk what I was thinking with the old way I did it lol

1 Like