How to use defsave

how do i use defsave. examples will be realy appretiated

DefSave comes with a built in example. Have you looked at that?

1 Like

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.

2 Likes

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.

2 Likes

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)
4 Likes

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

3 Likes

yup thats whati missed the entire time :rofl:

2 Likes

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

3 Likes