Having problem with setPreferences

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

But in the tutuorial “Displaying and saving score” I seem to have the following runtime error.

Displaying and saving score tutorial: [url=https://docs.coronalabs.com/tutorial/games/keepScores/index.html[/url]

attempt to call field setPreferences (a nil value)

 

This is the code of score.lua

[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 )

    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

– system.setPreferences

function M.save()

    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

[/lua]

This is the code of main.lua

[lua]

local score = require( “score” )

local scoreText = score.init(

{

    fontSize = 20,

    font = “CoolCustomFont.ttf”,

    x = display.contentCenterX,

    y = 30,

    maxDigits = 7,

    leadingZeros = true

})

score.set( 1000 ) – Sets the score to value

score.save()

[/lua]

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 an 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.

Any help appreciated.

 

What version of Corona are you using?

Rob

@Rob Miracle,

I’m using the 2016.2830 version.

Is it too outdated?

That’s 2 and a half years old and things have changed a lot in that time. SetPreferences is a relatively new addition.

Why not download the latest public version or even a daily build?

Yea, this might be the problem.

Thanks for clarifying. Ill get back to you if the update doesn’t change a thing.

The set/get/delete preferences was certainly added after that version.

I think it’s also important to understand that Apple and Google make updates that require Corona to add features and make changes that allow you to submit apps to Google Play and Apple’s App Connect sites. For instance, Google now requires all Android apps support Android 8.0, so you need 2018.3326 or later to even submit an Android app to Google Play.  I would use that version as a minimum.

Rob

What version of Corona are you using?

Rob

@Rob Miracle,

I’m using the 2016.2830 version.

Is it too outdated?

That’s 2 and a half years old and things have changed a lot in that time. SetPreferences is a relatively new addition.

Why not download the latest public version or even a daily build?

Yea, this might be the problem.

Thanks for clarifying. Ill get back to you if the update doesn’t change a thing.

The set/get/delete preferences was certainly added after that version.

I think it’s also important to understand that Apple and Google make updates that require Corona to add features and make changes that allow you to submit apps to Google Play and Apple’s App Connect sites. For instance, Google now requires all Android apps support Android 8.0, so you need 2018.3326 or later to even submit an Android app to Google Play.  I would use that version as a minimum.

Rob