Ive been trying to solve this for weeks now and its really annoying me whats going on. so basically I have a database that holds these values :
local M = {}
local sqlite3 = require("sqlite3")
M.highscore = 0
M.plays = 0
M.dodged = 0
Then my database:
local function setupDatabaseStats()
local dbPath = system.pathForFile("stats.db3", system.DocumentsDirectory)
local db = sqlite3.open(dbPath)
local playerSetup = [[
CREATE TABLE IF NOT EXISTS playerInfo (id INTEGER PRIMARY KEY autoincrement, highscore, dodged, plays);
INSERT INTO playerInfo VALUES(NULL, '0', ']] .. M.highscore ..[[', ']] .. M.dodged .. [[', ']] .. M.plays .. [[');
]]
db:exec(playerSetup)
db:close()
end
setupDatabaseStats()
M.loadAmount = function()
local dbPath = system.pathForFile("stats.db3", system.DocumentsDirectory)
local db = sqlite3.open(dbPath)
for row in db:nrows("SELECT * FROM playerInfo WHERE id = 1") do
M.highscore = tonumber(row.highscore)
M.dodged = tonumber(row.dodged)
M.plays = tonumber(row.plays)
end
db:close()
end
M.saveAmount = function()
local dbPath = system.pathForFile("stats.db3", system.DocumentsDirectory)
local db = sqlite3.open(dbPath)
local update = "UPDATE playerInfo SET highscore='" .. M.highscore .."', dodged='" .. M.dodged .."', plays='" .. M.plays .."', WHERE id=1"
db:exec(update)
db:close()
end
I then display these on the menu screen:
playAmount2 = display.newText(uiGroup, "Blocks dodged: " ..utils.dodged, 100, 100, "komika axis", 12)
playAmount2.anchorX = 0.5
playAmount2.anchorY = 0.5
playAmount2.x = 75
playAmount2.y = display.contentHeight - 70
bestScore = display.newText(uiGroup, "BEST SCORE: "..utils.highscore, 100, 100, "komika axis", 12)
bestScore.anchorX = 0.5
bestScore.anchorY = 0.5
bestScore.x = display.contentCenterX
bestScore.y = display.contentHeight - 70
playAmount = display.newText(uiGroup, "Games played: " ..utils.plays, 100, 100, "komika axis", 12)
playAmount.anchorX = 0.5
playAmount.anchorY = 0.5
playAmount.x = display.contentWidth - 75
playAmount.y = display.contentHeight - 70
save and load calls:
function scene:createScene( event )
local group = self.view
utils.loadAmount()
utils.saveAmount() -- (in gameover)
Now this seems to be working fine when playing, scores are saving and displaying on menu perfectly and across different scenes. But whenever I reload the simulator the scores and others show as zero again. I also notice that when I change the value of "playerInfo" inside the database creation, I get a " concatenate" on the display.newText which displays the values.
Aucun commentaire:
Enregistrer un commentaire