M.format is nil. It needs to be a string.
Rob
M.format is nil. It needs to be a string.
Rob
does this make M.format a string?
M.scoreText.text = string.format(M.format, M.score)
no. a string is just a word or sequence of characters, if its in a variable it should be prefixed and closed with " "
M.format = "hello"
i guess I’m just confused because this exact code works in my other program but not on this one. it is the code from the corona website on how to save high scores.
The purpose of the format is to determine how you want the score to be displayed. Typically I will use a format like:
M.format = “%06d”
which gives me 6 integer (decimal, ergo the “d”) digits that are prefixed with 0’s so I always get leading zeros. You can read about this in the documentation for string.format().
Now I looked at the tutorial and there is this line:
M.format = “%” … prefix … opt.maxDigits … “d”
which I don’t see in your code above. This will create the format string with a percent sign. Prefix (which was built a few lines before it) will either be a “0” or “” and then the maxDigits you want or default to 6 and ends with the required “d”. That whole block of code which appears to have been removed:
opt.maxDigits = customOptions.maxDigits or 6
opt.leadingZeros = customOptions.leadingZeros or false
M.filename = customOptions.filename or “scorefile.txt”
local prefix = “”
if ( opt.leadingZeros ) then
prefix = “0”
end
M.format = “%” … prefix … opt.maxDigits … “d”
This code is needed so that the M.format will be programmed to control how string.format() outputs the string. If you don’t want to deal with all of these options, simply make:
M.format = “%6d”
and be done with it.
Rob
M.format = “%” … prefix … opt.maxDigits … “d” is in the score.lua file…
[lua]local M = {} – create our local M = {}
M.score = 0
function M.init( options )
local customOptions = options or {}
local opt = {}
opt.fontSize = customOptions.fontSize or 50
opt.font = customOptions.font or native.systemFontBold
opt.x = customOptions.x or display.contentCenterX
opt.y = customOptions.y or opt.fontSize * 0.5
opt.maxDigits = customOptions.maxDigits or 6
opt.leadingZeros = customOptions.leadingZeros or false
M.filename = customOptions.filename or “scorefile.txt”
local prefix = “”
if opt.leadingZeros then
prefix = “0”
end
M.format = “%” … prefix … opt.maxDigits … “d”
M.scoreText = display.newText(string.format(M.format, 0), opt.x, opt.y, opt.font, opt.fontSize)
return M.scoreText
end
function M.set( value )
M.score = 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 + amount
M.scoreText.text = string.format(M.format, M.score)
end
function M.save()
local path = system.pathForFile( M.filename, system.DocumentsDirectory)
local file = io.open(path, “w”)
if file then
local contents = tostring( M.score )
file:write( contents )
io.close( file )
return true
else
print("Error: could not read ", M.filename, “.”)
return false
end
end
function M.load()
local path = system.pathForFile( M.filename, system.DocumentsDirectory)
local contents = “”
local file = io.open( path, “r” )
if file then
– read all contents of file into a string
local contents = file:read( “*a” )
local score = tonumber(contents);
io.close( file )
return score
end
print("Could not read scores from ", M.filename, “.”)
return nil
end
return M[/lua]
That’s not the code you posted in the first post.
The second code shouldn’t be having that issue. Are you doing something that would overwrite parts of the object?
the first code was just the part that is says the error was referring to. I have the full score.lua file that the corona high score tutorial says to put in. i don’t believe that I’m doing anything to overwrite anything. i made sure to not use any variables to conflicted with the score.lua file
this is what i have in my game.lua file to uses anything to do with my score:
[lua]mydata.score = 0
count = 0
function shoot(event)
if event.phase == “began” then
bullsEye.x = math.random(0, display.contentWidth - 40)
bullsEye.y = math.random(0, display.contentHeight - 100)
mydata.score = mydata.score + 1
count = count + 1
scoreText.text = mydata.score
if count == 15 then
timeLimit = timeLimit + 5
count = 0
end
end
return true
end[/lua]
and then in my restart.lua file:
[lua]function restartGame(event)
if event.phase == “ended” then
saveScore()
storyboard.purgeScene(“restart”)
storyboard.gotoScene(“instructions”)
end
end
function loadScore()
local prevScore = score.load()
if prevScore ~= nil then
if prevScore <= mydata.score then
score.set(mydata.score)
else
score.set(prevScore)
end
else
score.set(mydata.score)
score.save()
end
end
function saveScore()
score.save()
end
scoreText = display.newText(mydata.score, display.contentCenterX, display.contentCenterY + 75,system.nativeFont,200)
function scene:enterScene(event)
restartBttn:addEventListener(“touch”,restartGame)
loadScore()
end
[/lua]
Still haven’t been able to figure out what is going on with this… Anyone have any ideas?
There have been several suggestions and various versions of the code posted. I’m not sure exactly what problem you are having. Can you summarized the issue, the current error messages you are getting?
Thanks
Rob
Im getting these errors:
2014-05-06 17:30:26.753 Corona Simulator[473:507] Runtime error
/score.lua:30: bad argument #1 to ‘format’ (string expected, got nil)
stack traceback:
[C]: in function ‘format’
/score.lua:30: in function ‘set’
/restart.lua:42: in function ‘loadScore’
/restart.lua:80: in function </restart.lua:78>
?: in function ‘dispatchEvent’
?: in function <?:1096>
(tail call): ?
?: in function <?:481>
?: in function <?:218>
2014-05-06 17:30:26.851 Corona Simulator[473:507] Runtime error
/score.lua:30: bad argument #1 to ‘format’ (string expected, got nil)
stack traceback:
[C]: in function ‘format’
/score.lua:30: in function ‘set’
/restart.lua:42: in function ‘loadScore’
/restart.lua:80: in function </restart.lua:78>
?: in function ‘dispatchEvent’
?: in function ‘_listener’
?: in function <?:141>
?: in function <?:218>
which is referring to the following code:
[lua]function M.set( value )
M.score = value
M.scoreText.text = string.format(M.format, M.score) --Line 30
end
function loadScore()
local prevScore = score.load()
if prevScore ~= nil then
if prevScore <= mydata.score then
score.set(mydata.score) – Line 42
else
score.set(prevScore)
end
else
score.set(mydata.score)
score.save()
end
end
function scene:enterScene(event)
restartBttn:addEventListener(“touch”,restartGame)
loadScore() -Line 80
end[/lua]
To which you said M.format was nil and need to be a string. In the score.lua file it has the M.format set to a string with:
M.format ="%"… prefix … opt.maxDigits …“d”
I got the score.lua file from the corona library.
This is the part of my game.lua file that deals with the score:
[lua]mydata.score = 0
count = 0
function shoot(event)
if event.phase == “began” then
bullsEye.x = math.random(0, display.contentWidth - 40)
bullsEye.y = math.random(0, display.contentHeight - 100)
mydata.score = mydata.score + 1
count = count + 1
scoreText.text = mydata.score
if count == 15 then
timeLimit = timeLimit + 5
count = 0
end
end
return true
end[/lua]
And then when the game ends the score adds 3 the next time it is ran instead of 1. This is the restart.lua
[lua]function restartGame(event)
if event.phase == “ended” then
saveScore()
storyboard.purgeScene(“restart”)
storyboard.gotoScene(“instructions”)
end
end
function loadScore()
local prevScore = score.load()
if prevScore ~= nil then
if prevScore <= mydata.score then
score.set(mydata.score)
else
score.set(prevScore)
end
else
score.set(mydata.score)
score.save()
end
end
function saveScore()
score.save()
end
scoreText = display.newText(mydata.score, display.contentCenterX, display.contentCenterY + 75,system.nativeFont,200)
function scene:enterScene(event)
restartBttn:addEventListener(“touch”,restartGame)
loadScore()
end[/lua]
See this is where I’m very confused. Just above you posted the entire score module from the tutorial which has the required code in it and you said you were using the module unedited:
the first code was just the part that is says the error was referring to. I have the full score.lua file that the corona high score tutorial says to put in.
If you just put the score code in it’s own file and require it in and get rid of this block of code:
function M.set( value )
M.score = value
M.scoreText.text = string.format(M.format, M.score) --Line 30
end
that might clear up your issue.
so make another lua such as scorecode.lua and put that code in it and then put local score code = required (“score code”) ?
Im confused on why I would have to take part of the code out of the score.lua file and put in it a different .lua file and then require that file. it doesn’t make any sense…
Yes create a “score.lua” file. Put the entire module from the tutorial in it.
In your code:
local score = require( “score” )
Rob
I already have the the whole score.lua file with the entire module with the require code in my game.
and?
thats when I got all the errors i listed before. I’ve had the score.lua file and the local score = require(“score”) from the beginning
I think I possibly see what’s going on here. That tutorial presents the entire score module in several blocks that all need to go into score.lua. Any section of code that has an M. goes in score.lua. You don’t put that code in your app. Your code starts with the “Putting it to use” section. All code before that is part of the score module.
I would recommend that you download the sample project attached “To see our code in action, please download a version of the ManyCrates sample app that has been modified to work with this module.”
Then see how that app uses the code. You can copy the whole score module from it.
Rob
Sorry to bring up an old post but I had exactly the same error and found a fix.
I needed to move the ‘loadScore’ and ‘saveScore’ functions to the ‘local forward references’ section prior to my scene:create function.
HTH