Wow. Thanks for the help!
My Main file now looks like this.
local settings = require("settings")
-- Test if it exists. If false then create it with a 0 value
if type(settings:loadVar("lastLevelUnlocked")) == "nil" then
settings:saveVar( "lastLevelUnlocked", 0 )
else
settings:setVar( "lastLevelUnlocked", settings:loadVar("lastLevelUnlocked") )
end
if settings:getVar("lastLevelUnlocked") \>= levelNumber then
levelPlayable=true
else
levelPlayable=false
end
settings:saveVar( "lastLevelUnlocked", settings:getVar("lastLevelUnlocked") + 1 )
-- Import director class
local director = require("director")
-- Create a main group
local mainGroup = display.newGroup()
-- Main function
local function main()
-- Add the group from director class
mainGroup:insert(director.directorView)
-- Change scene without effects
-- This sends user to the main menu
director:changeScene("screen1")
return true
end
-- Begin
main()
And my Settings.lua file looks like this.
module(..., package.seeall)
---------------------------------------------------------
-- DATABASE
---------------------------------------------------------
require "sqlite3"
--
local dbPath = system.pathForFile("settings.db", system.DocumentsDirectory)
local db = sqlite3.open( dbPath )
--
db:exec [[CREATE TABLE IF NOT EXISTS tb\_settings (
name VARCHAR(50) PRIMARY KEY,
type VARCHAR(1),
value\_string TEXT,
value\_number NUMBER);]]
--
local function onSystemEvent( event )
if( event.type == "applicationExit" ) then
db:close()
end
end
---------------------------------------------------------
-- VARIABLES
---------------------------------------------------------
local tabVars = {}
--
tabVars["numPlayers"] = 2
---------------------------------------------------------
-- FUNCTIONS
---------------------------------------------------------
-- GET
function settings:getVar ( pVarName )
return tabVars[pVarName]
end
-- SET
function settings:setVar ( pVarName, pVarValue )
tabVars[pVarName] = pVarValue
end
-- LOAD
function settings:loadVar( pVarName )
for row in db:nrows("SELECT \* FROM tb\_settings WHERE name = '" .. string.upper(pVarName) .. "'") do
if row.type == "N" then
return tonumber(row.value\_number)
else
return tostring(row.value\_string)
end
end
return nil
end
-- SAVE
function settings:saveVar( pVarName, pVarValue )
--
if type(pVarValue) == "nil" then
pVarValue = tabVars[pVarName]
else
settings:setVar ( pVarName, pVarValue )
end
--
local vVarType
--
if type(pVarValue) == "number" then
vVarType = 'N'
elseif type(pVarValue) == "string" then
vVarType = 'C'
else
return true
end
--
for row in db:nrows("SELECT count(\*) as cont FROM tb\_settings WHERE name = '" .. string.upper(pVarName) .. "'") do
if row.cont \> 0 then
if vVarType == "N" then
db:exec ("UPDATE tb\_settings SET type = 'N', value\_number = " .. pVarValue .. " WHERE name = '" .. string.upper(pVarName) .. "';")
else
db:exec ("UPDATE tb\_settings SET type = 'C', value\_string = '" .. pVarValue .. "' WHERE name = '" .. string.upper(pVarName) .. "';")
end
else
if vVarType == "N" then
db:exec ("INSERT INTO tb\_settings ( name, type, value\_number) VALUES ('" .. string.upper(pVarName) .. "','N'," .. pVarValue .. ");")
else
db:exec ("INSERT INTO tb\_settings ( name, type, value\_string) VALUES ('" .. string.upper(pVarName) .. "','C','" .. pVarValue .. "');")
end
end
end
end
Where do I enter the function that states when something happens, the level is completed, thus unlocking the next level?
Also, where do I put the list of levels or do I need to? [import]uid: 32061 topic_id: 5884 reply_id: 22965[/import]