Git Product home page Git Product logo

library-defold-persist's Introduction

Defold Persist

Defold Persist provides a simple interface for saving and loading data in a Defold game engine project.

Please click the โ˜† button on GitHub if this repository is useful or interesting. Thank you!

thumbnail.png

Installation

Add the latest version to your project's dependencies:
https://github.com/whiteboxdev/library-defold-persist/archive/main.zip

Tutorial

Import Persist into script files that need to save or load data:

local persist = require "persist.persist"

A file must exist before it can be accessed, otherwise an error message will be printed to standard output. Let's create a settings file:

local default_settings_data =
{
    master_volume = 100,
    music_volume = 100,
    sound_volume = 100
}
persist.create("settings", default_settings_data)

If the file already exists, then persist.create() will simply be ignored. This function should be called as part of a project's startup routine for each file to ensure that it exists.

If the file has the ".json" extension, then its data will be saved as JSON instead of Defold's built-in format. Be cautious to only save data types that are supported by Defold's json API. For example, the hashed string hash("test") cannot be encoded or decoded properly, so it should instead be saved as just "test" then manually hashed.

Each OS has its own conventions and preferences for where applications should create custom files. See the following table for details:

OS Path More
Windows C:\Users\<user>\AppData\Roaming\<project_title>\<file_name>
MacOS ~/Library/Application Support/<project_title>/<file_name>
Linux <config>/<project_title>/<file_name> The config variable adopts the contents of the XDG_CONFIG_HOME environment variable, otherwise it defaults to ~/.config.
(Other Platforms) (Please submit a pull request!)

Let's change the music volume from 100 to 75, and change the sound volume from 100 to 25:

persist.write("settings", "music_volume", 75)
persist.write("settings", "sound_volume", 25)

Persist differentiates between saved data and written data. Written data has not yet been transferred to non-volatile memory. It only exists as a table within the running process. This allows us to abort file changes before they overwrite previously saved data.

Let's revert back to whatever the sound volume was before we changed it, then save our changes:

persist.flush("settings", "sound_volume")
persist.save("settings")

Next let's load the data from the settings file so that we know how loudly to play our wonderful background music:

local settings_data = persist.load("settings")
local master_volume = settings_data.master_volume
local music_volume = settings_data.music_volume
sound.play(msg.url(nil, nil, "background_music"), { gain = master_volume * music_volume })

When loading data, written data is prioritized over saved data. This means that persist.load() will always return the latest version of a file, even if it has not yet been saved.

API

persist.create(file_name, data [, overwrite])

Creates a file with the specified data. If the file already exists, then its data can be overwritten.

persist.write(file_name, key, value)

Writes data to a file.

persist.flush(file_name [, key])

Flushes unsaved data from a file. If a key is specified, then only that field is flushed.

persist.save(file_name)

Saves data that was written to a file.

persist.load(file_name)

Loads data from a file, including written data.

persist.exists(file_name)

Checks if a file exists.

library-defold-persist's People

Contributors

whiteboxdev avatar justapotota avatar

Stargazers

Steve Biedermann avatar Robert K. Pauls avatar hadi ahmadi avatar Pete Garcin avatar Elias Mascheroni avatar  avatar Jake Larson avatar  avatar Patrick Lafleur avatar Pawel avatar kacevoid avatar Nathan Bolton avatar Willem avatar yeqwep avatar

Watchers

 avatar

Forkers

justapotota

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.