Permission issues in lua

2019-07-15 14:26发布

Is it required to set some specific permissions in corona build.settings to save high score in file permanently?

I'm getting and error every time I run the code saying "Permission denied" How to rectify this error?

Here is the code I tried:

function read_score()

local f1 = assert(io.open(path, "r+"))
local contents = f1:read( "*a" )
highScore = tonumber(contents)  
if highScore==nil  
then highScore=0


elseif score>highScore
    then
    highScore=score
end
    f1:write(highScore)
    f1:close()
disp_permScore()

end


function disp_permScore() --Function to display the high score
local f1 = assert(io.open(path, "r"))
local contents = f1:read( "*a" )
highScore = tonumber(contents)  

    text_display2= display.newText(" BEST: " ..highScore, 0, 0, "Helvetica", 90)
    text_display2.x = centerX
    text_display2.y = centerY + 80
    text_display2.alpha=1 
f2:close()
end


function gameOver()
local f1 = assert(io.open(path, "r+"))
local contents = f1:read( "*a" )
highScore = tonumber(contents)  

if score<highScore
    then
    disp_permScore()
    else
    read_score()
end

Please tell where do I go wrong? Also please explain how to rectify it? I'm new to this language and this is my first ever build I'm trying.

Thanks

EDIT:

function read_score()

local f1 = assert(io.open(path, "r"))
local contents = f1:read( "*a" )
highScore = tonumber(contents)
f1:close()  
if highScore==nil  
then highScore=0
elseif score>highScore
    then
    highScore=score
    local f2=assert(io.open(path, "w"))
    f2:write(highScore)
    f2:close()
end


end


function disp_permScore()
local f1 = assert(io.open(path, "r"))
local contents = f1:read( "*a" )
highScore = tonumber(contents)  

text_display2= display.newText("GAME OVER!\n BEST: " ..highScore, 0, 0, "native.systemFontBold", 80)
text_display2.x = centerX
text_display2.y = centerY
text_display2.alpha=1 
f1:close()
end


function gameOver()

mainScreen()
disp_permScore()

Please heave a look above at the edited code now. Now whenI run this code by using an old file( which had been opened earlier, it runs good then saving the code permanently).. but when I try opening a new file, the code fails. (I think thats because I call the read_score() and disp_permScore() functions which initially opens file in 'read' mode - is raising the error) But then, how to rectify this error? P.S : when I change "r" mode to "r+" mode, the same error rises again. Please help

EDIT 2:

function saveScore()
local path = system.pathForFile("scoredata001.txt", system.DocumentsDirectory)
local file = io.open(path, "w")
if file then
    local score=get_score() --The get_score() returns the value of current score which is saved in 'score'.
    local newScore = compareScore()
    local contents = tostring( newScore )
    file:write( contents )

    io.close( file )
    return true
else
    print("Error: could not write Score")
    return false
end
end


function loadScore()
local path = system.pathForFile("scoredata001.txt", system.DocumentsDirectory)

local contents = ""
local file = io.open( path, "r" )
if file then

     local contents = file:read( "*a" )
     local score = tonumber(contents);
     io.close( file )
     return score
end
print("Could not read scores from scoredata.txt")
return nil
end

function return_highScore()
local highscore=loadScore()
if highscore==nil
    then highscore=0
end
return highscore
end

function compareScore()
local highscore=return_highScore()
if highscore
    then
local currscore=get_score()
if highscore==0
    then
    return highscore
elseif currscore>highscore
    then 
    return currscore
end
end
return true
end


function disp_permScore()
local display_score=return_highScore()
    text_display2= display.newText("GAME OVER!\n BEST: " ..display_score, 0, 0, "Helvetica", 80)
    text_display2.x = centerX
    text_display2.y = centerY
    text_display2.alpha=1 

function gameOver()

mainScreen()
saveScore()
disp_permScore()
end

Please have a look at this one? Since I wanted to display both high score and current score, I modified the previous score.. Current score is being displayed perfect. This is something I tried yesterday night. But now, the high score is not being saved in file. (i.e. the best always displays 0) Also, the cmd says "unable to read scores form scoredata.txt) I'm unable to find where I went wrong. Please help with this?

1条回答
你好瞎i
2楼-- · 2019-07-15 15:02

You are opening file for reading (local f1 = assert(io.open(path, "r+"))) as specified by "r" parameter, but later are trying to write to it (f1:write(highScore)).

You need to open file, read the context, and close it; then reopen it for writing (using "w" mode) and write the content.

The updated code fails because you are opening in read mode the file that doesn't exist. You should get nil as the result of open() call and "No such file or directory" or something similar as the second value returned. You need to replace assert with a check on the result of the open command and if open fails, then check the error to see if it fails because the file is new.

It would be quite helpful to see the exact error you are getting next time (along with any line information and the stack trace if available).

查看更多
登录 后发表回答