Traverse table of strings

Hello, I hope that everyone is doing well.

I am trying to build a little dialogue system. I have a table of things for a character to say in a script. Attached to the character is a label. Each time the player clicks on the character, the label is changed to the next item in the table. How might I go about accomplishing this? Is this the most effective way to accomplish my goal?

Thanks!

Best,
Vishal

As the title of your post says you need to traverse a table of strings, one by one on mouse click. The simplest solution is to track which string you are currently showing (index into the list of strings) and on each click increment the index and show the next string.

function init(self)
	self.strings = { "Hello", "World" }
	self.index = 1
end

function on_input(self, action_is, action)
	if action_id == hash("click") and action.pressed then
		local s = self.strings[self.index]
		label.set_text("#dialog_line", s)
		self.index = self.index + 1
	end
end
4 Likes

Thanks for this!

I probably should have specified that it was the syntax that I needed help with. Thanks for the example!

Best,
Vishal