Hi. i wanted to know if there was a way to save a file without hard coding a file path? i have my project on github to transfer between devices, and so a hard coded file path is not useful for me. i’m trying to use sys.get_save_file.
I recommend you use DefSave - takes away the headache!
i was eyeing that before, how complicated is it to make your self? if it’s completely out of my depth i’ll just use that, but i’m curious…
Unless you need some really specific functionality then I strongly recommend using it. No need to reinvent the wheel.
Of course, reinventing the wheel might be a good learning experience… So it depends what you want to achieve.
DefSave handles a lot of annoying quirks and exceptions, such as differences between operating systems.
OH right if it gets to OS level stuff it’s probably best to just use it then
ok so this is making my brain hurt. i kind of udnerstand how save and load work, but i have no idea how the data gets in there.
local defsave = require "defsave.defsave"
local scores = require "main.saving.scores"
local save file - sys.get_save_file(application_id, file_name)
defsave.set_appname(test)
function save()
local score = scores
local filename = sys.get_save_file("saving", score)
sys.save(filename, score)
defsave.save(file, force)
end
local function load()
local scorefile = sys.get_save_file("sys_save_load", score)
local data = sys.load(filename)
defsave.load(file)
return data.score or 0
end
so defsave.save/load definately replace the sys.load/save, but i don’t really know how they pass any values. is a table really needed or is it delt with by defsave?
local A = {
player = {
highscore = 0
score = 0
}
}
return A
The sys.save() and sys.load() functions (and I suppose DefSave as well) can be used to save and load data from a Lua table. The only additional argument you need to give the function is the full path to the file in question.
You can use sys.get_save_file() to get a path that works on all supported platforms. I suppose DefSave takes care of this but for you.
You are mixing both options, not sure why you are doing that? Ignore any of the sys.* functionality if you’re using defsave.
To save the score:
defsave.set("filename", "score", score)
To load the score:
local score = defsave.get("filename", "score")
is the return data.score bit useful?
In what sense? It’s useful sometimes to add a default value (“or 0”) if that’s what you mean. But the function you’ve written mixes DefSave and sys which is not useful.
i’ve changed it now, and i’m struggling to convert table data to a string for gui.set_text. i’ve found tostring and looked about but i can’t find a simple explaination. this should hopefully be the last headscratcher before trying to make the actual file save.
It’s a table, so you can’t tostring it. Put it in a pprint to see what’s inside.
the problem i have is that i want the data inside the table as a string so i can set it as the text of a node.
so how can i display the values?
You access values in a table like this:
scores["score"]
Or like this:
scores.score
local scores = {score = 123, highscore = 456}
print(scores.score, scores.highscore)
print(scores["score"], scores["highscore"])
Check the output of the above here.
oh yeah i literally did this earlier in my code how did i manage to forget that brrruh
Here is the Programming in Lua chapter on tables. I recommend you read this:
https://www.lua.org/pil/2.5.html
Actually, I recommend you read most of the early parts of this book to get a good grasp of the language. It’s a great and concise resource.
is there a way to mix text and variables into one variable? i don’t really know how to explain it but it was somthing like this in VB.NET
gui.set_text("score is " & scores.score &
"highscore is " & scores.highscore)
i need to get out of my habit of skim reading tbh. i find it hard to concentrate on large blocks of text but i’ll look for it then