Gunther Gib
Gunther Gib

Reputation: 117

Having problem with getPreference and setPreferences in corona

I have been following the tutorials on corona website and everything has been going well so far.

But in the tutorial "Displaying and saving score" i seem to have the following runtime error.

attempt to call field setPreferences' (a nil value)

This is the code of score.lua

local M = {}

M.score = 0

function M.init( options )

    local customOptions = options or {}   -- nice use of "or" operator
    local opt = {}
    opt.fontSize = customOptions.fontSize or 24
    opt.font = customOptions.font or native.systemFont
    opt.x = customOptions.x or display.contentCenterX
    opt.y = customOptions.y or opt.fontSize*0.5   -- such that the score is positioned at the top, half of its font size.
    opt.maxDigits = customOptions.maxDigits or 6
    opt.leadingZeros = customOptions.leadingZeros or false

    local prefix = ""
    if ( opt.leadingZeros ) then
        prefix = "0"
    end
    M.format = "%" .. prefix .. opt.maxDigits .. "d"   -- so that its accesible in other modules.

    -- Create the score display object 
    M.scoreText = display.newText( string.format( M.format, 0 ), opt.x, opt.y, opt.font, opt.fontSize )   -- string.format() works like printf and scanf statements
    M.scoreText:setFillColor(1,0,0)
    return M.scoreText
end

function M.set( value )

    M.score = tonumber(value)
    M.scoreText.text = string.format( M.format, M.score )

end

function M.get()

    return M.score
end

function M.add( amount ) 

    M.score = M.score + tonumber(amount)
    M.scoreText.text = string.format( M.format, M.score )
end

function M.save()
    print (" the score is " .. M.score)
    local saved = system.setPreferences( "app", { currentScore=M.score } )
    if ( saved == false) then
        print ( "ERROR: could not save score" )
    end
end

function M.load()
    local score = system.getPreference( "app", "currentScore", "number" ) 

    if  ( score ) then
        return tonumber(score)
    else
        print( "ERROR: could not load score (score may not exist in storage)" )
    end
end

return M

This is the code of main.lua

local score = require( "score" )

local scoreText = score.init(
{
    fontSize = 20,
    font = "CoolCustomFont.ttf",
    x = display.contentCenterX,
    y = 30,
    maxDigits = 7,
    leadingZeros = true
})

local savedScore = score.load()
score.set( 1000 ) -- Sets the score to value
score.save()

I am aware there are other ways of keeping score, but I want to know what the problem is in my code. I googled everywhere but couldn't come up with any solution. Maybe I have made a mistake somewhere that I'm not able to identify.

Even tried a build on my smart phone, but ended up getting the same error.

Upvotes: 1

Views: 101

Answers (1)

wsha
wsha

Reputation: 964

From corona docs

Syntax

system.setPreferences( category, preferences )

category (required) String. Indicates which set of preferences should be accessed on the system. Currently, only the "app" category is supported — this is the application's custom preferences defined by the Corona app developer.

preferences (required) Table. Table of preferences to be written to storage. This table should contain key-value pairs where the key is the unique name of the preference and its value is either a boolean, number, or string.

if your M.score is nil then you might get an error try this

local saved = system.setPreferences( "app", { currentScore=M.score or 0 } )

Upvotes: 1

Related Questions