-- Create a button for character speech
local chatBtn = display.newRect(100, 100, 200, 50)
chatBtn:setFillColor(0.5, 0.5, 0.8)
chatBtn.characterName = "hero"
chatBtn:addEventListener('tap', startConversation)
local dialogueCount = 0
function startConversation(event)
local speaker = event.target.characterName
showNextLine(speaker)
end
function showNextLine(speaker)
dialogueCount = dialogueCount + 1
fetchDialogue(dialogueCount, speaker)
end
function fetchDialogue(lineNum, speaker)
local db = sqlite3.open("mydb.sqlite")
local query = string.format(
"SELECT dialogue FROM conversations WHERE speaker='%s' LIMIT %d,1",
speaker, lineNum - 1
)
for row in db:nrows(query) do
print(row.dialogue)
end
db:close()
end
I’m having trouble with a Lua script that fetches character dialogue from a SQLite database. The first line shows up fine, but when I try to get the next one, I get an error about concatenating a table value. I think it’s related to how I’m passing the ‘speaker’ variable. Any ideas on how to fix this? I’m new to Lua and SQL, so I might be missing something obvious.