how do i use defsave. examples will be realy appretiated
DefSave comes with a built in example. Have you looked at that?
yeh but it doesnât make sense to me at all.
I assume youâre exaggerating! You need to come up with some specific questions or specific parts you would like clarified, otherwise we will just end up making another example you canât make sense out of.
on the guide to use it it says to put the url into my dependencies which i have don then it says
" Once added, you must require the main Lua module in scripts via
local defsave = require("defsave.defsave")
"
which I cant tell weather it means I need it in all scripts or in its own specific script or how to use its save or load function.
You need to require
in every script where you intend to do saving/loading (i.e. anywhere you want to use any of the functions provided by DefSave). Normally that would be in one specific script, but youâre welcome to organise your project however you like.
Youâll need to elaborate on this, as the example includes how to use the save and load functions.
I mean how do I save the player characters position as how to load the game in those saved positions.
I really encourage you to experiment with this kind of stuff, itâs the best way to learn. If you still donât manage to find a solution, you can describe what youâve tested so far and what happened. This will make it much easier for people helping you to identify what exactly it is you are struggling with.
Hereâs a very simple example.
local defsave = require("defsave.defsave")
function init(self)
--set the game name
defsave.appname = "my_awesome_game"
--load the "player" save file
defsave.load("player")
--load the position variable in the player save file (will be nil first time since no data exists)
local loaded_position = defsave.get("player", "position")
print("loaded position", loaded_position)
--store a position in the player save file
defsave.set("player", "position", vmath.vector3(100,200,0))
--save all changes to the player save file
defsave.save("player")
end
The script loads a position (and prints the output), then saves a position. So first time you run it, you would except an empty print since there is no save file. If you run it again however, there will be a position stored.
First time I run it:
DEBUG:SCRIPT: loaded position nil
Second time I run it:
DEBUG:SCRIPT: loaded position vmath.vector3(100, 200, 0)
for some reason the code
local defsave = require("defsave.defsave")
keeps giving me âthe file â/defsave/defsave.luaâ could not be foundâ error
Adding library dependencies is described here:
This is the part youâve likely missed:
select Project ⸠Fetch Libraries to update library dependencies
yup thats whati missed the entire time
Just in case someone else has trouble working with DefSave. I created a YouTube video with a simple use case on it. I was having trouble with it until this past week.
Defold DefSave Example
Hi vigmu2gmes,
Many thanks for nice video tutorial but I have problem with get function. So I can see Youre example project with sound settings in GitHub.
How can I set the sfx or music level value?
Are you trying to save sounds with defsave?
No! Iâd like save and reload only sound settings ( sound on/off, gain level etc.).
Can I save a table with sound setting datas?
Is this correct?
defsave.set("config", "audio", { sfx = 0.5, music = 0.6})