Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Unable to save score between sessions "Error could not read nil "
Started by farjadfarabi_czs Sep 17 2015 04:34 AM

6 replies to this topic
save could not read scorefile.txt score.save() lua

Best Answer tomaswesterlund , 18 September 2015 - 10:14 AM

Hi,

I tried the code and it works on my end, I could create, save and load the file without any problems.

 

This makes me think that there are some permissions issues on your Windows version.

 

What version of Windows are you running?

Are you running Corona "As Administrator"?

Do you close the file before running Corona?

 

Best regards,

Tomas

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

farjadfarabi_czs

[GLOBAL: userInfoPane.html]
farjadfarabi_czs
  • Enthusiast

  • 97 posts
  • Corona SDK

Hello there,
 
I am new to Corona SDK and I am having this problem while testing my build on the simulator. I am trying to

 

use    "score.lua" module from https://coronalabs.com/blog/2013/12/10/tutorial-howtosavescores/ to save

 

and update scores.

 

Only modification I have made to the code are :
 

function M.init( options )
   local customOptions = options or {}
   local opt = {}
   opt.fontSize = customOptions.fontSize or 24
   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 =  4
   M.filename = "scorefile.txt"
   local prefix = ""
   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

-- AND THE FOLLOWING

function M.set( value )
M.score = value 
M.text = tostring( M.score ) 
end

 

 

 

set and get score modules are working fine. However, when I try to save or load, I get these  errors respectively,

 

Error: could not read  nil

Error: could not read scores form nil

 

Is "scorefile.txt" a file I should create myself? If so where should it be? I have placed

 

"scorefile.txt" in the same directory where main.lua is. I have also placed one in the directory 

 

File > Project Sandbox > Documents. However, it is still unable to read/write score.

 

Any help/suggestions would be greatly appreciated. 

 

Thanks.



[TOPIC: post.html]
#2

farjadfarabi_czs

[GLOBAL: userInfoPane.html]
farjadfarabi_czs
  • Enthusiast

  • 97 posts
  • Corona SDK

Okay, so I read the documentation on Reading/Writing Files and this is the error I am getting on the log

 

"C:\Users\Fj\AppData\Roaming\Corona Labs\Corona Simulator\Sandbox\ <long string of file name here> \Documents: Permission denied"

 

It seems that the reason I have been unable to read/write scores is because Corona does not have access to the text file. How can I change/give Corona Permission? OR is there a way to change the project sandbox folder?

 

Thanks



[TOPIC: post.html]
#3

tomaswesterlund

[GLOBAL: userInfoPane.html]
tomaswesterlund
  • Contributor

  • 306 posts
  • Corona SDK

Could you post all of the code?

io.open(path, "w") creates the file for you if it doesn't exist so no need to create it manually.



[TOPIC: post.html]
#4

farjadfarabi_czs

[GLOBAL: userInfoPane.html]
farjadfarabi_czs
  • Enthusiast

  • 97 posts
  • Corona SDK

local M = {}  -- Create the local module table (this will hold our functions and data)
M.score = 0  -- Set the initial score to 0
function M.init( options )
   local customOptions = options or {}
   local opt = {}
   opt.fontSize = customOptions.fontSize or 24
   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 =  4
   M.filename = "file.txt"
   local prefix = ""
   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.text = tostring( M.score )
end
function M.get()
   return M.score
end
function M.add( amount )
   M.score = M.score + amount
   M.text = tostring( M.score )
end

function M.save()
   local path = system.pathForFile( M.filename, system.DocumentsDirectory )
   local file,reason = 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, ". "..reason )
      return false
   end
end
function M.load()
   local path = system.pathForFile( M.filename, system.DocumentsDirectory )
   local contents = ""
   local file,reason = 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
   else
      print( "Error: could not read scores from ", M.filename, ". " .. reason)
   end
   return nil
end
return M

Hi Tomas, 

 

This is the score.lua file I am using to save/load/and update scores. The get and set modules work as expected, but save and load,

 

which involve reading/ writing a file into the sandbox folder is unable to read or write to 'file.txt'. According to the console, the error i

 

get is 

 

Error: could not read nil . C:\Users\Fj\AppData\Roaming\Corona Labs\Corona Simulator\Sandbox\A-7307D68A6746C2D3RDKCAC3F50A03\Documents: Permission denied

 

As for manually creating the file, does it have to be done through Corona?

 

Thanks.



[TOPIC: post.html]
#5

tomaswesterlund

[GLOBAL: userInfoPane.html]
tomaswesterlund
  • Contributor

  • 306 posts
  • Corona SDK

  Best Answer

Hi,

I tried the code and it works on my end, I could create, save and load the file without any problems.

 

This makes me think that there are some permissions issues on your Windows version.

 

What version of Windows are you running?

Are you running Corona "As Administrator"?

Do you close the file before running Corona?

 

Best regards,

Tomas



[TOPIC: post.html]
#6

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

Hi @farjadfarabi_czs,

I agree with Tomas that you probably have some permissions restriction on your local system which is preventing the file from being accessed. The "Permission denied" error message you're receiving indicates that as well.

 

Brent



[TOPIC: post.html]
#7

farjadfarabi_czs

[GLOBAL: userInfoPane.html]
farjadfarabi_czs
  • Enthusiast

  • 97 posts
  • Corona SDK

Hi Brent and Tom, thanks a lot for the input. As it turns out, the issue was my computer. I just tested the game on my Nexus 7 and scores are indeed saved in between sessions. I am gonna have to dig around to see what is causing Corona to have denied access. I am running Windows 7 Home Premium as an administrator

 

Anyone else having same issue, try your app on an actual iOS/Android device and see what results you get.

 

Thanks,

Farjad




[topic_controls]
[/topic_controls]