Saving a game

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.
Post Reply
Oblivion_123
Prole
Posts: 18
Joined: Thu Oct 01, 2015 5:25 pm

Saving a game

Post by Oblivion_123 »

Hello,

Can someone help me out onto coding a button in which you would click on the menu to save your score in a game using tserial?

Any help is much appreciated,

Thanks
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: Saving a game

Post by xNick1 »

I made something similar a while ago using a library called ser.
It's not that hard.
If you just want to save a score it's even easier.
You just want to write that number in the file.
So create a file, write the number on the file and save it.
Then read that number every time you start the game and use it as your highest score.
When saving again remember to empty the file first.

I could share my code code tomorrow, but I've used "ser" in my case
Oblivion_123
Prole
Posts: 18
Joined: Thu Oct 01, 2015 5:25 pm

Re: Saving a game

Post by Oblivion_123 »

xNick1 wrote: Thu Dec 14, 2017 8:22 pm I made something similar a while ago using a library called ser.
It's not that hard.
If you just want to save a score it's even easier.
You just want to write that number in the file.
So create a file, write the number on the file and save it.
Then read that number every time you start the game and use it as your highest score.
When saving again remember to empty the file first.

I could share my code code tomorrow, but I've used "ser" in my case
Thank you for the help, I understand what you're saying however this is more of an arcade kind of game where I'd like to show a table of the top highscores so idk if it'd still work the same way?
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: Saving a game

Post by xNick1 »

I attached the lib you need.
Obviously adapt the code to what you need, I tried to make it as easy as possible.

Basically you create a table and populate it with the data you want and then you save it.
When loading it, you check if the saving file already exists and if it doesn't just create it with default values.

Code: Select all

serialize = require("lib/ser")

Code: Select all

function saveMaxScore()
    local data = { }-- Make a table to store variables in.
    data.maxScor = maxScor
    data.whatever = "Nick"
    -- Save the table to the "savegame.txt" file:
    love.filesystem.write("savegame.txt", serialize(data))
end

Code: Select all

function loadMaxScore()    
    if not love.filesystem.exists("savegame.txt") then
        maxScor = 0
        saveMaxScore()
    end
    -- Load the data table:
    local data = love.filesystem.load("savegame.txt")()
    -- Copy the variables out of the table:
    maxScor = data.maxScor
    return maxScor
end
To make the top 10 scores thing...
I'd save 10 numbers in a table. (0, 0, 0, 0, 0, 0, 0, 0, 0, 0 at the beginning)
Then at the end of the game I would iterate over the high scores and check if your score is higher than the worst score.
What's the worst score? It is 10. What is my score? 11.
I'm gonna replace 10 with 11.
Then when showing the high scores I would order them from the highest to the lowest.
ser.lua
(3.52 KiB) Downloaded 288 times
Oblivion_123
Prole
Posts: 18
Joined: Thu Oct 01, 2015 5:25 pm

Re: Saving a game

Post by Oblivion_123 »

xNick1 wrote: Fri Dec 15, 2017 8:48 am I attached the lib you need.
Obviously adapt the code to what you need, I tried to make it as easy as possible.

Basically you create a table and populate it with the data you want and then you save it.
When loading it, you check if the saving file already exists and if it doesn't just create it with default values.

Code: Select all

serialize = require("lib/ser")

Code: Select all

function saveMaxScore()
    local data = { }-- Make a table to store variables in.
    data.maxScor = maxScor
    data.whatever = "Nick"
    -- Save the table to the "savegame.txt" file:
    love.filesystem.write("savegame.txt", serialize(data))
end

Code: Select all

function loadMaxScore()    
    if not love.filesystem.exists("savegame.txt") then
        maxScor = 0
        saveMaxScore()
    end
    -- Load the data table:
    local data = love.filesystem.load("savegame.txt")()
    -- Copy the variables out of the table:
    maxScor = data.maxScor
    return maxScor
end
To make the top 10 scores thing...
I'd save 10 numbers in a table. (0, 0, 0, 0, 0, 0, 0, 0, 0, 0 at the beginning)
Then at the end of the game I would iterate over the high scores and check if your score is higher than the worst score.
What's the worst score? It is 10. What is my score? 11.
I'm gonna replace 10 with 11.
Then when showing the high scores I would order them from the highest to the lowest.

ser.lua
Okay thank you so much, this helps out so much
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: Saving a game

Post by xNick1 »

You're welcome!
Don't be afraid to ask for more help if you need it!
Obviously try to figure it out on your own first :crazy:
User avatar
SirRanjid
Prole
Posts: 39
Joined: Sun Nov 19, 2017 1:44 pm
Contact:

Re: Saving a game

Post by SirRanjid »

I've made a more general purpose data save/load script, which relies on serpent(MIT license) as serializer.

Here you can find it.

Basic usage:

(nm is the name of the data table and the savefile like %appdata%/data/<nm>.txt)

Code: Select all

GAME.EZF:CreateNewData(nm[,tbl]) --creates a new data table for data to store in (if you provide it with a table(tbl)) its merged in right away

GAME.EZF:ChangeData(nm,tbl) --merges new data into the table[nm]

GAME.EZF:SaveData(nm) --serializes and saves the datainto the file of name <nm>

GAME.EZF:LoadDataInto(nm,itbl,func) --creates data of name <nm>, and loads the file deserialized into the provided itbl table ,the func should be a callback function if the itbl changes but somehow doesn'T work consistently (just leave it out/remove or fix that part)
GAME is my global table to store everything in you may wanna change it in the first line. Or change the entire code (permission granted CC0 etc.)

This way i have a file for my options, one for my last console entries and can easily expand for whatever i need a new file. It's functional but not 100% done.

And it's restricted to only save strings,numbers and bools but can be expanded via the whitelist table.

EDIT:
For buttons you can use my UI_Base and Button (CC BY-NC-SA 3.0)scripts if you want.
They might not function out of the box for your problem because you have to make sure to load UI_Base, then Button.lua (which i manage with my folder structure)
EDIT2:Also they rely on my implementation of hooks, ScrW,ScrH(as short fucntions for screen width and height), and a varfuncthat creates set and getfunctions like:

Code: Select all

function VarFunc(tbl,name,default)
	tbl[name] = default
	
	tbl["Get"..name] = function(self)
		return self[name]
	end
	
	tbl["Set"..name] = function(self,v)
		self[name] = v
	end
end
But it lets you easily create a button like:
local Button = UI("Button"[,Xpos,Ypos,width,height,rotation])
And has a nice Button:Align(enable,X,Y,W,H,Xoffset,Yoffset,Woffset,Hoffset) that sizes your button in real time as the window resizes or the the parent of it etc. If you write 'false' where X,Y,W and/or H belongs it takes the standard values there and the respective offset is also ignored.

If you don't wanna use it you can still reverse engineer it for learning purpose if you can read my code^^ (they got many more functions that might be useful for you)
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests