Make event only happen if its the first time app is ever opened

I am trying to make a game, and I need it to save a value for the high score as 0 the first time it is ever opened. If it happens every time the game is opened, it will replace what the high score is every time.

How can I make this function that will just run the first time the app is ever opened?

I have a function I can give you later today when I’m at my computer but for now I’ll tell you the logic of it. You can create a variable that checks for a file (io.open) and if its there then don’t over write it but if not create a new file called “highscores” or something. Sorry if it sounds confusing :frowning: I can explain it later when I can actually look at the code.

Sorry about the late reply :frowning: but put these functions in your main.lua:

[lua]

function Load( pathname )

    local data = nil

    local path = system.pathForFile( pathname…".json", system.DocumentsDirectory  ) 

    local fileHandle = io.open( path, “r” )

    if fileHandle then

        data = json.decode( fileHandle:read( “*a” ) or “” )

        io.close( fileHandle ) 

    end 

    return data 

end

function Save( data, pathname ) 

    local success = false 

    local path = system.pathForFile( pathname…".json", system.DocumentsDirectory  ) 

    local fileHandle = io.open( path, “w” ) 

    if fileHandle and data then 

        local encodedData = json.encode(data) 

        fileHandle:write( encodedData ) 

        io.close( fileHandle ) 

        success = true

    end

    return success 

end

[/lua]

So now you can do this to check if the scores file already exists, and if not, create one with a score of 0:

[lua]

local score = Load(“score”)

if score ~= nil then 

     --score file already exists

elseif score == nil then

     --score file doesn’t exist…create one!

     local score = 0

     Save(score, “score”)

end

[/lua]

Now here is how you would edit the score:

[lua]

–to load the score data

local score = Load(“score”)

–to view the score data

print(score)

–to edit the score data (temporary until saved to file)

score = 10

–to save these edits to the file

Save(score, “score”)

[/lua]

Hope this helps! :slight_smile:

Might considder using https://github.com/GlitchGames/GGData

I have a function I can give you later today when I’m at my computer but for now I’ll tell you the logic of it. You can create a variable that checks for a file (io.open) and if its there then don’t over write it but if not create a new file called “highscores” or something. Sorry if it sounds confusing :frowning: I can explain it later when I can actually look at the code.

Sorry about the late reply :frowning: but put these functions in your main.lua:

[lua]

function Load( pathname )

    local data = nil

    local path = system.pathForFile( pathname…".json", system.DocumentsDirectory  ) 

    local fileHandle = io.open( path, “r” )

    if fileHandle then

        data = json.decode( fileHandle:read( “*a” ) or “” )

        io.close( fileHandle ) 

    end 

    return data 

end

function Save( data, pathname ) 

    local success = false 

    local path = system.pathForFile( pathname…".json", system.DocumentsDirectory  ) 

    local fileHandle = io.open( path, “w” ) 

    if fileHandle and data then 

        local encodedData = json.encode(data) 

        fileHandle:write( encodedData ) 

        io.close( fileHandle ) 

        success = true

    end

    return success 

end

[/lua]

So now you can do this to check if the scores file already exists, and if not, create one with a score of 0:

[lua]

local score = Load(“score”)

if score ~= nil then 

     --score file already exists

elseif score == nil then

     --score file doesn’t exist…create one!

     local score = 0

     Save(score, “score”)

end

[/lua]

Now here is how you would edit the score:

[lua]

–to load the score data

local score = Load(“score”)

–to view the score data

print(score)

–to edit the score data (temporary until saved to file)

score = 10

–to save these edits to the file

Save(score, “score”)

[/lua]

Hope this helps! :slight_smile:

Might considder using https://github.com/GlitchGames/GGData