Anotther difficulty

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
BoonyBuny
Prole
Posts: 31
Joined: Mon Jun 13, 2022 1:24 am
Location: France
Contact:

Re: Anotther difficulty

Post by BoonyBuny »

Well it seems that I was beeing stupid, and thought it was writting the data to the data.txt that is inside my game's folder, beeing a custom path

after reading the return of love.filesystem.getSaveDirectory(), the data.txt is just there, in appdata/roaming/LOVE/Balloon_adventure ; and successfuly wrote on...

i t w o r k e d a l l a l o n g.................... :? :? :? :? :? :?

well now that It works it was just me beeing stupid, to read the data.txt and assign the values to their respective variables (bestscore, coins)
i just gotta use love.filesystem.read() right?
A coding begginer that is migrating from 8 years of Scratch... yea yikes. :emo:
User avatar
BrotSagtMist
Party member
Posts: 611
Joined: Fri Aug 06, 2021 10:30 pm

Re: Anotther difficulty

Post by BrotSagtMist »

If you have the \n part in table.concat you can use readline for each entry.
obey
User avatar
BoonyBuny
Prole
Posts: 31
Joined: Mon Jun 13, 2022 1:24 am
Location: France
Contact:

Re: Anotther difficulty

Post by BoonyBuny »

Oh thanks! and yes I used your method without any libraries, im gonna tinker things around and if it works I'm gonna post my solution here, so other people that may have my problem have the solution
A coding begginer that is migrating from 8 years of Scratch... yea yikes. :emo:
User avatar
BoonyBuny
Prole
Posts: 31
Joined: Mon Jun 13, 2022 1:24 am
Location: France
Contact:

Re: Anotther difficulty

Post by BoonyBuny »

Code: Select all

local highscores={}
    for line in love.filesystem.lines("data.txt") do
        table.insert(highscores, line)
    end
    bestscore=highscores[1]
    coins=highscores[2]
works fine, it' sin my love.load() function, the UI shows perfectly the best score and the coins collected in that best run, but there's one catch in my gamestates.lua

Code: Select all

function newGameState(x)
    if x == "Menu" then
        GameState = "Menu"
        score = 0
        coins = 0
        scoretimer = 0
    elseif x == "Play" then
        GameState = "Play"
    elseif x == "Death" then
        GameState = "Death"
        Player.x = 300
        Player.y = 500
        if score > bestscore then
            bestscore = score
            saveScore()
        end
    end
end
if score > bestcore then... Outputs an error, because bestcore is considered a String variable and not a integer/float etc, how can i fix this?
A coding begginer that is migrating from 8 years of Scratch... yea yikes. :emo:
User avatar
BrotSagtMist
Party member
Posts: 611
Joined: Fri Aug 06, 2021 10:30 pm

Re: Anotther difficulty

Post by BrotSagtMist »

table.insert(highscores, tonumber(line))

Sometimes lua can use strings as they where numbers or vise verca, sometimes it can not.
When in doubt you need to explicitly call tonumber/tostring on values.
obey
User avatar
BoonyBuny
Prole
Posts: 31
Joined: Mon Jun 13, 2022 1:24 am
Location: France
Contact:

Re: Anotther difficulty

Post by BoonyBuny »

]Thanks to all of you, my saving and loading system works perfectly.

codes:

loads the save in love.load()

Code: Select all

    local highscores={}
    for line in love.filesystem.lines("data.txt") do
        table.insert(highscores, tonumber(line))
    end
    bestscore=highscores[1]
    coins=highscores[2]
saves the game

Code: Select all

function saveScore()
    local ScoreData={bestscore, coins}
    love.filesystem.write("data.txt", table.concat(ScoreData,"\n"))
end
in my gamestates.lua, if you die, it checks if you did a best score, and proceeds to save

Code: Select all

function newGameState(x)
    if x == "Menu" then
        GameState = "Menu"
        score = 0
        coinsCollected = 0
        scoretimer = 0
    elseif x == "Play" then
        GameState = "Play"
    elseif x == "Death" then
        GameState = "Death"
        Player.x = 300
        Player.y = 500
        if score > bestscore then
            bestscore = score
            coins = coinsCollected
            saveScore()
        end
    end
end
The priority in my score system is seconds, you may have 5 coins in a 50seconds run, but if you have a 55 seconds run with 0 coins, that 55seconds run will be the one saved
A coding begginer that is migrating from 8 years of Scratch... yea yikes. :emo:
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 27 guests