### saver.init Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Initialize the Saver module. Should be called at the start of your game to set up the module. This function loads the game state from a file and starts the autosave timer. ```APIDOC ## saver.init ### Description Initialize the Saver module. Should be called at the start of your game to set up the module. Call it after saver.set_migrations if you are using migrations. This function loads the game state from a file and starts the autosave timer. If the game state file does not exist, a new game state is created. ### Parameters #### Parameters - **config** (saver.config|nil) - Optional - Configuration table. ### Example Usage ```lua saver.init() ``` ``` -------------------------------- ### Initialize Defold Saver Module Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Initialize the Saver module at the start of your game. This function loads the game state, starts the autosave timer, and creates a new game state if none exists. Call this after setting migrations. ```lua saver.init() ``` -------------------------------- ### Main Functions Source: https://github.com/insality/defold-saver/blob/main/README.md Core functions for initializing, binding save states, saving, loading, setting, getting, and deleting game states. ```APIDOC ## Main Functions ### `saver.init()` Initializes the saver library. ### `saver.bind_save_state(table_key_id, table_reference)` Binds a Lua table to a save state identifier. - **table_key_id** (string): A unique identifier for the save state. - **table_reference** (table): The Lua table to bind. ### `saver.save_game_state([save_name])` Manually saves the current game state. If `save_name` is not provided, it saves the default state. - **save_name** (string, optional): The name of the save state to save. ### `saver.load_game_state([save_name])` Loads a previously saved game state. If `save_name` is not provided, it loads the default state. - **save_name** (string, optional): The name of the save state to load. ### `saver.set_game_state(game_state)` Sets the current game state directly. This is typically used after loading a state. - **game_state** (table): The game state to set. ### `saver.get_game_state()` Retrieves the current game state. ### `saver.delete_game_state([save_name])` Deletes a saved game state. If `save_name` is not provided, it deletes the default state. - **save_name** (string, optional): The name of the save state to delete. ``` -------------------------------- ### Example: Update and Set Game State Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Retrieves the current game state, modifies a property, and then sets the updated state. Useful for incremental game state updates. ```lua local game_state = saver.get_game_state() game_state.game.level = 5 saver.set_game_state(game_state) ``` -------------------------------- ### Initialize Saver Module and Bind Save States Source: https://github.com/insality/defold-saver/blob/main/USE_CASES.md Initializes the saver module and binds various game states (language, token, quest, sound) to be saved. This setup is typically done in the bootstrap loader script. ```lua --@param self scene.loader local function init_saver(self) --@class saver.game_state --@field lang lang.state --@field token token.state --@field quest quest.state --@field sound sound.state saver.init() saver.bind_save_state("lang", lang.state) saver.bind_save_state("token", token.state) saver.bind_save_state("quest", quest.state) saver.bind_save_state("sound", sound.state) end function init_lang(self) lang.init() end function init_token(self) token.init() end function init_quest(self) quest.init() end local function init(self) init_saver(self) init_lang(self) init_token(self) init_quest(self) end ``` -------------------------------- ### Get Current Game Project Folder Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Retrieves the absolute path to the game's project folder. This is useful for saving/loading files during development and is only available on desktop platforms when the game is launched from the Defold Editor. Returns nil if the folder is not found. ```lua saver.get_current_game_project_folder() ``` ```lua local project_folder = saver.get_current_game_project_folder() print(project_folder) -- "/Users/user/projects/my_game" ``` -------------------------------- ### Get Save Version Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Retrieves the current save version of the game state, used for version checking. ```lua saver.get_save_version() ``` ```lua local save_version = saver.get_save_version() print(save_version) ``` -------------------------------- ### Get Save Path Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Returns the absolute path to the game save folder or a specific file within it. Supports subfolders. ```lua saver.get_save_path([filename]) ``` ```lua local folder_path = saver.get_save_path() print(folder_path) -- "/Users/user/Library/Application Support/Defold Saver/" local file_path = saver.get_save_path("data.json") print(file_path) -- "/Users/user/Library/Application Support/Defold Saver/data.json" local file_path_2 = saver.get_save_path("profiles/profile1.json") print(file_path_2) -- "/Users/user/Library/Application Support/Defold Saver/profiles/profile1.json" ``` -------------------------------- ### Get Value from Storage Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Retrieves a value from the saver storage using a specified key. If the key does not exist, a provided default value is returned. ```lua saver.get_value(key_id, [default_value]) ``` -------------------------------- ### Get Autosave Timer Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Retrieves the current autosave interval in seconds. ```lua saver.get_autosave_timer() ``` -------------------------------- ### saver.get_value Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Gets the value from the saver storage. If the value does not exist, it will return the default value. ```APIDOC ## saver.get_value ### Description Gets the value from the saver storage. If the value does not exist, it will return the default value. ### Method Not applicable (Lua function) ### Endpoint Not applicable ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **key_id** (string) - Required - The storage field name - **[default_value]** (?) - Optional - The default value ### Response #### Success Response (200) - **value** () - The value from the saver storage #### Response Example ```lua -- Assuming 'player_score' exists in storage local score = saver.get_value("player_score", 0) print(score) -- Outputs the stored score or 0 if it doesn't exist ``` ``` -------------------------------- ### saver.get_current_game_project_folder Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Returns the absolute path to the current game project folder. Returns nil if the game project folder is not found. Used only at desktop platforms and if game started from the Defold Editor. ```APIDOC ## saver.get_current_game_project_folder ### Description Returns the absolute path to the current game project folder. It is useful when you need to save or load files from the game project folder at development. Returns nil if the game project folder is not found. Used only at desktop platforms and if game started from the Defold Editor. ### Method Not applicable (Lua function) ### Endpoint Not applicable ### Parameters None ### Request Example ```lua local project_folder = saver.get_current_game_project_folder() print(project_folder) -- "/Users/user/projects/my_game" ``` ### Response #### Success Response (200) - **The** (string|nil) - absolute path to the current game project folder. Nil if the game.project folder is not found. #### Response Example ```lua "/Users/user/projects/my_game" ``` ``` -------------------------------- ### Get Current Game State Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Retrieve the current game state object. This function returns the entire game state, which can then be inspected or manipulated. ```lua local game_state = saver.get_game_state() pprint(game_state) ``` -------------------------------- ### Using Simple Storage API Source: https://github.com/insality/defold-saver/blob/main/README.md Demonstrates how to use the simple key-value storage API to store and retrieve values. Useful for simple data like run counts. ```lua local saver = require("saver.saver") function init(self) local run_count = saver.get_value("run_count", 0) run_count = run_count + 1 saver.set_value("run_count", run_count) end ``` -------------------------------- ### Integrating a Library with Defold Saver Source: https://github.com/insality/defold-saver/blob/main/USE_CASES.md Demonstrates how to make a library's state persistent by binding its state table using `saver.bind_save_state`. ```lua -- my_library.lua local M = {} -- Make an persistent state for your library, which should be saved/loaded M.state = { value = 0, other_value = 0, } -- Other functions of your library -- Integration in the game local saver = require("saver.saver") local my_library = require("my_library") function init(self) saver.init() saver.bind_save_state("my_library", my_library.state) -- After bind save state, you can use your library functions -- The state will be loaded from the save file and will be saved automatically my_library.init() end ``` -------------------------------- ### Load Raw Binary Data by Name and Create Texture Source: https://github.com/insality/defold-saver/blob/main/USE_CASES.md Loads raw binary data from the save directory by name, then creates and applies an image texture to a GUI node. Handles potential loading and texture creation failures. ```lua -- Load a saved PNG image from the save directory local function load_image(save_name, node_id) -- Load the binary data local image_data = saver.load_binary_by_name(save_name) local img = image.load(image_data) if not image_data then print("Failed to load saved image:", save_name) return nil end -- Create an image from the binary data if not img then print("Unable to load image data") return nil end -- Create a texture ID based on the save name local texture_id = save_name .. "_texture" -- Create a new texture from the image and set it to the GUI node if gui.new_texture(texture_id, img.width, img.height, img.type, img.buffer) then gui.set_texture(node_id, texture_id) return texture_id else print("Failed to create texture from image data") return nil end end -- Usage load_image("saved_image.png", gui.get_node("image_node")) ``` -------------------------------- ### Explicitly Saving and Loading Files with Format Selection Source: https://github.com/insality/defold-saver/blob/main/USE_CASES.md Saves and loads game settings using different formats (JSON, Lua, Binary). Allows for human-readable JSON, flexible Lua, or efficient binary serialization. Auto-detection of format based on file extension is also demonstrated. ```lua local saver = require("saver.saver") -- Available formats local JSON_FORMAT = saver.FORMAT.JSON -- For human-readable JSON files local LUA_FORMAT = saver.FORMAT.LUA -- For Lua files (more flexible than JSON) local SERIALIZED_FORMAT = saver.FORMAT.SERIALIZED -- For Lua tables with userdata using sys.save/sys.load -- Save game settings in JSON format for human readability local settings = { music_volume = 0.8, sfx_volume = 1.0, difficulty = "normal", controls = { up = "w", down = "s", left = "a", right = "d" } } -- Save as JSON for human-readability saver.save_file_by_name(settings, "settings.json", JSON_FORMAT) -- Save the same data in Lua format saver.save_file_by_name(settings, "settings.lua", LUA_FORMAT) -- Save the same data in binary format saver.save_binary_by_name(settings, "settings") -- Load from any format local json_settings = saver.load_file_by_name("settings.json", JSON_FORMAT) local lua_settings = saver.load_file_by_name("settings.lua", LUA_FORMAT) local binary_settings = saver.load_binary_by_name("settings") -- You can also let the library auto-detect the format based on file extension local auto_json_settings = saver.load_file_by_name("settings.json") local auto_lua_settings = saver.load_file_by_name("settings.lua") ``` -------------------------------- ### Load Binary Data from File by Path Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Loads raw binary data from a file at a specified absolute path. Use this for reading non-textual data. ```lua saver.load_binary_by_path(path) ``` -------------------------------- ### Load Data from File by Path Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Loads data from a file at a specified absolute path. The format is detected from the file extension or an optional format parameter. Returns nil if the file does not exist. ```lua -- Get project path works on build from the Defold Editor only local project_path = saver.get_current_game_project_folder() -- Use path to the resources folder local file_path = saver.get_save_path(project_path .. "/resources/data.json") local data = saver.load_file_by_path(file_path) pprint(data) ``` -------------------------------- ### Apply Migrations Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Executes the migrations previously set using `saver.set_migrations()`. This function should be called after `saver.init()` when manually loading game state. ```lua saver.apply_migrations() ``` -------------------------------- ### Load Binary Data by Name Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Loads binary data from a file with a specified name in the game save folder. Supports subfolders. Returns nil if the file does not exist. ```lua saver.load_binary_by_name(filename) ``` -------------------------------- ### Downloading and Saving Binary File from URL Source: https://github.com/insality/defold-saver/blob/main/USE_CASES.md Downloads a binary file (e.g., an image) from a given URL and saves it to the project's data directory. Ensures the file is saved only if the download was successful (HTTP status 200 or 304). ```lua local saver = require("saver.saver") local file_url = "https://raw.githubusercontent.com/Insality/defold-saver/refs/heads/main/media/logo.png" http.request(file_url, "GET", function(_, id, response) if response.status == 200 or response.status == 304 then saver.save_binary_by_name(response.response, "cache/logo.png") end end) ``` -------------------------------- ### Defold Saver API Reference Source: https://github.com/insality/defold-saver/blob/main/README.md Provides a quick reference for the main functions available in the Defold Saver library, including save state management, file handling, storage, and migrations. ```lua local saver = require("saver.saver") -- Main functions saver.init() saver.bind_save_state(table_key_id, table_reference) saver.save_game_state([save_name]) saver.load_game_state([save_name]) saver.set_game_state(game_state) saver.get_game_state() saver.delete_game_state([save_name]) -- File Handling by absolute path saver.save_file_by_path(data, absolute_file_path, [format]) saver.load_file_by_path(absolute_file_path, [format]) saver.save_binary_by_path(data, absolute_file_path) saver.load_binary_by_path(absolute_file_path) saver.delete_file_by_path(absolute_file_path) saver.is_file_exists_by_path(absolute_file_path) -- File Handling by relative path in application data folder (from sys.get_save_path()) saver.save_file_by_name(data, file_name, [format]) saver.load_file_by_name(file_name, [format]) saver.save_binary_by_name(data, file_name) saver.load_binary_by_name(file_name) saver.delete_file_by_name(file_name) saver.is_file_exists_by_name(file_name) -- File format constants saver.FORMAT.SERIALIZED -- "serialized", save and load as Lua serialized table. Default format. saver.FORMAT.JSON -- "json", save and load as JSON. Autoselect if path ends with .json saver.FORMAT.LUA -- "lua", save and load as Lua. Autoselect if path ends with .lua -- Storage saver.set_value(key_id, value) saver.get_value(key_id, [default_value]) saver.is_value_exists(key_id) -- Migrations saver.set_migrations(migration_list) saver.apply_migrations() -- Other saver.set_autosave_timer(seconds) saver.get_save_path(file_name) saver.get_save_version() saver.set_logger(logger) saver.get_current_game_project_folder() saver.before_save_callback = function() "Called before saver saves data" end ``` -------------------------------- ### Save and Load Files by Path Source: https://github.com/insality/defold-saver/blob/main/USE_CASES.md Saves or loads data to or from an absolute file path. The file path must include the file name and extension. ```lua -- This function will save the data to the absolute path -- file path should contain the file name and extension saver.save_file_by_path(data, file_path) saver.load_file_by_path(path) ``` -------------------------------- ### Load Game State with Defold Saver Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Load the game state from a file. You can specify a custom file name or use the default name configured in game.project. Returns true if the state was loaded successfully, or false if a new state was created. ```lua local is_loaded = saver.load_game_state() -- Load the game state with default name local is_loaded = saver.load_game_state("custom_save") -- Load the game state with custom name ``` -------------------------------- ### Save Binary Data to File by Path Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Saves raw binary data to a file at a specified absolute path. Use this for non-textual data like images or custom binary formats. ```lua saver.save_binary_by_path(data, path) ``` -------------------------------- ### Configure Saver Programmatically Source: https://github.com/insality/defold-saver/blob/main/README.md Initialize the Saver module with custom configuration options using a Lua table. This allows for dynamic configuration at runtime. ```lua saver.init({ save_name = "game", save_folder = "Defold Saver", autosave_timer = 3, saver_key = "saver", lua_require_as_string = 0, }) ``` -------------------------------- ### Configure Saver in game.project Source: https://github.com/insality/defold-saver/blob/main/README.md Optional configuration for the Saver module can be set in the game.project file. These settings can also be applied programmatically. ```ini [saver] save_name = game save_folder = Defold Saver autosave_timer = 3 saver_key = saver lua_require_as_string = 0 ``` -------------------------------- ### Using Key-Value Storage with Defold Saver Source: https://github.com/insality/defold-saver/blob/main/USE_CASES.md Utilizes the `saver.storage` module for simple key-value storage supporting string, number, and boolean types. Provides type-specific getter functions. ```lua local storage = require("saver.storage") function init(self) storage.set("key", "value") local value = storage.get("key") -- Returns "value" local value_str = storage.get_string("key", "default_value") -- Returns "value" local value_num = storage.get_number("key", 0) -- Returns 0 local value_bool = storage.get_boolean("key", false) -- Returns false end ``` -------------------------------- ### Load Data by Name Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Loads data from a file with a specified name in the game save folder. Supports subfolders and optional format overrides. Returns nil if the file does not exist. ```lua saver.load_file_by_name(filename, [format]) ``` ```lua local data = saver.load_file_by_name("data.json") pprint(data) ``` -------------------------------- ### Saver Configuration Fields Source: https://github.com/insality/defold-saver/blob/main/api/saver_config_api.md This section details the available fields for configuring the saver.init function. These fields allow customization of save folder, save file name, saver and storage keys, autosave timer, and how Lua modules are loaded. ```APIDOC ## Saver Configuration This API provides configuration options for the `saver.init` function. ### Fields - **save_folder** (_string_): Specifies the directory where save files will be stored. Defaults to the project name with special characters removed. - **save_name** (_string_): Defines the base name for save files. Can include '.json' or '.lua' extensions to specify the format. Defaults to 'game'. - **saver_key** (_string_): The key used internally by the saver to store its data. Defaults to 'saver'. - **storage_key** (_string_): The key used internally by the storage mechanism. This field is deprecated. Defaults to 'storage'. - **autosave_timer** (_number_): Sets the interval in seconds for automatic saving. Defaults to 3 seconds. - **lua_require_as_string** (_boolean_): When true, `require()` loads Lua files as strings. Defaults to false. ``` -------------------------------- ### saver.load_binary_by_path Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Loads binary data from a file at the specified absolute path. ```APIDOC ## saver.load_binary_by_path ### Description Loads the binary data from a file at the specified path. ### Method ```lua saver.load_binary_by_path(path) ``` ### Parameters #### Parameters - **path** (string) - Required - The absolute path to the file to load. Contains the file name and extension. ### Returns - **data** (string|nil) - The binary data loaded from the file. If the file does not exist, returns nil. ``` -------------------------------- ### Set and Apply Migrations Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Defines a series of functions to update the game state structure when loading saved data. Migrations are applied sequentially after loading the game state manually. Each migration function receives the game state and a logger, and must return the updated game state. ```lua saver.set_migrations([migrations_table]) ``` ```lua local migrations = { -- Migration 1 function(game_state, logger) -- Assume we have new level_data field in the game state and we need to move level and score to it game_state.game.level_data = { level = game_state.game.level, score = game_state.game.score } game_state.game.level = nil game_state.game.score = nil return game_state end, -- Migration 2 function(game_state, logger) -- Just an example, multiply the score by 1000. For example we changed our score system game_state.game.level_data.score = game_state.game.level_data.score * 1000 return game_state end } saver.set_migrations(migrations) saver.init() saver.bind_save_state("game", game_state) saver.apply_migrations() ``` -------------------------------- ### Migrations API Source: https://github.com/insality/defold-saver/blob/main/README.md Functions for setting up and applying data migrations. ```APIDOC ## Migrations API ### `saver.set_migrations(migration_list)` Sets the list of migration functions to be applied. - **migration_list** (table): A list of functions, where each function represents a migration step. ### `saver.apply_migrations()` Applies the configured migrations to the save data if the save data's version is less than the number of migrations. ``` -------------------------------- ### Save Binary Data by Name Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Saves binary data to a file with a specified name in the game save folder. Supports subfolders. ```lua saver.save_binary_by_name(data, filename) ``` -------------------------------- ### Storage API Source: https://github.com/insality/defold-saver/blob/main/README.md Functions for interacting with the simple key-value storage. ```APIDOC ## Storage API ### `saver.set_value(key_id, value)` Sets a value in the key-value storage. - **key_id** (string): The unique key for the value. - **value**: The value to store. ### `saver.get_value(key_id, [default_value])` Retrieves a value from the key-value storage. - **key_id** (string): The key of the value to retrieve. - **default_value** (any, optional): The default value to return if the key does not exist. ### `saver.is_value_exists(key_id)` Checks if a value exists in the key-value storage for the given key. - **key_id** (string): The key to check. - Returns: (boolean) True if the value exists, false otherwise. ``` -------------------------------- ### saver.load_file_by_path Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Loads data from a file at the specified absolute path. The format is detected from the file extension. Use `load_binary_by_path` for binary data. ```APIDOC ## saver.load_file_by_path ### Description Loads the data from a file at the specified path. NOTE: For binary data like images, use `saver.load_binary_by_path` instead. ### Method ```lua saver.load_file_by_path(path, [format]) ``` ### Parameters #### Parameters - **path** (string) - Required - The absolute path to load the file from. Contains the file name and extension. - **format** (string|nil) - Optional - Optional format override (json, lua, serialized). If not specified, format will be detected from paths extension. ### Returns - **data** (table|nil) - The data loaded from the file. If the file does not exist, returns nil. ### Example Usage ```lua -- Get project path works on build from the Defold Editor only local project_path = saver.get_current_game_project_folder() -- Use path to the resources folder local file_path = saver.get_save_path(project_path .. "/resources/data.json") local data = saver.load_file_by_path(file_path) pprint(data) ``` ``` -------------------------------- ### Add Defold Saver Dependency Source: https://github.com/insality/defold-saver/blob/main/README.md Add the Defold Saver library to your project's dependencies in the game.project file. ```text https://github.com/Insality/defold-saver/archive/refs/tags/6.zip ``` -------------------------------- ### saver.apply_migrations Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Applies the migrations set by saver.set_migrations function. It should be called after loading the game state manually with saver.init() function. ```APIDOC ## saver.apply_migrations ### Description Applies the migrations set by saver.set_migrations function. It should be called after loading the game state manually with saver.init() function. ### Method Not applicable (Lua function) ### Endpoint Not applicable ### Parameters None ### Request Example ```lua saver.apply_migrations() ``` ### Response #### Success Response None #### Response Example None ``` -------------------------------- ### File Handling by Absolute Path Source: https://github.com/insality/defold-saver/blob/main/README.md Functions for saving, loading, deleting, and checking the existence of files using their absolute paths. ```APIDOC ## File Handling by Absolute Path ### `saver.save_file_by_path(data, absolute_file_path, [format])` Saves data to a file at the specified absolute path. - **data**: The data to save. - **absolute_file_path** (string): The absolute path to the file. - **format** (constant, optional): The format to save the data in (e.g., `saver.FORMAT.SERIALIZED`, `saver.FORMAT.JSON`, `saver.FORMAT.LUA`). ### `saver.load_file_by_path(absolute_file_path, [format])` Loads data from a file at the specified absolute path. - **absolute_file_path** (string): The absolute path to the file. - **format** (constant, optional): The format of the data in the file. ### `saver.save_binary_by_path(data, absolute_file_path)` Saves raw binary data to a file at the specified absolute path. - **data**: The binary data to save. - **absolute_file_path** (string): The absolute path to the file. ### `saver.load_binary_by_path(absolute_file_path)` Loads raw binary data from a file at the specified absolute path. - **absolute_file_path** (string): The absolute path to the file. ### `saver.delete_file_by_path(absolute_file_path)` Deletes a file at the specified absolute path. - **absolute_file_path** (string): The absolute path to the file. ### `saver.is_file_exists_by_path(absolute_file_path)` Checks if a file exists at the specified absolute path. - **absolute_file_path** (string): The absolute path to the file. - Returns: (boolean) True if the file exists, false otherwise. ``` -------------------------------- ### saver.load_game_state Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Load the game state from a file. If no file name is provided, the default file name specified in the game.project file is used. ```APIDOC ## saver.load_game_state ### Description Load the game state from a file. If no file name is provided, the default file name specified in the game.project file is used. ### Parameters #### Parameters - **save_name** (string|nil) - Optional - The name of the file to load the game state from. Default is the file name specified in the game.project file. ### Returns - **is_loaded** (boolean) - true if the game state was loaded successfully, false if the new game state is created ### Example Usage: ```lua local is_loaded = saver.load_game_state() -- Load the game state with default name local is_loaded = saver.load_game_state("custom_save") -- Load the game state with custom name ``` ``` -------------------------------- ### Save Game State with Defold Saver Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Save the current game state to a file. You can specify a custom file name or use the default name configured in game.project. Returns true if the save was successful. ```lua -- Save the game with default name saver.save_game_state() -- Save the game with custom name saver.save_game_state("custom_save") ``` -------------------------------- ### Applying Migrations to Update Save Data Structure Source: https://github.com/insality/defold-saver/blob/main/USE_CASES.md Sets up and applies migration functions to update save data when the structure changes, ensuring compatibility with older save files. Call `set_migrations` before `init` and `apply_migrations`. ```lua local saver = require("saver.saver") local migrations = { -- First migration function(data, logger) -- Make some changes in the data data.game.level = 10 data.game.score = nil end, -- Second migration function(data, logger) data.settings.ui_params = { scale = 1, theme = "dark", } end, } function init(data) saver.set_migrations(migrations) saver.init() saver.bind_save_state("game", game_data) saver.bind_save_state("settings", settings_data) -- We need to call `set_migrations` before `init` to correct -- tracking of current migration version saver.apply_migrations() end ``` -------------------------------- ### saver.load_binary_by_name Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Loads binary data from a file with the specified name in the game save folder. Supports subfolders in the filename. Returns nil if the file does not exist. ```APIDOC ## saver.load_binary_by_name ### Description Loads the binary data from a file with the specified name. The file is loaded from the game save folder. Filename supports subfolders. ### Method ```lua saver.load_binary_by_name(filename) ``` ### Parameters #### Parameters - **filename** (string) - The name of the file to load the binary data from. Can contain subfolders. ### Returns #### Returns - **data** (string|nil) - The binary data loaded from the file. If the file does not exist, returns nil. ``` -------------------------------- ### saver.get_game_state Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Returns the current game state. ```APIDOC ## saver.get_game_state ### Description Returns the current game state. ### Returns - **The** (saver.game_state) - current game state. ### Example Usage: ```lua local game_state = saver.get_game_state() pprint(game_state) ``` ``` -------------------------------- ### Save and Load Files by Name Source: https://github.com/insality/defold-saver/blob/main/USE_CASES.md Saves or loads data within the game's save folder using a specified file name. Subfolders are supported in the path. ```lua -- This function will save the data inside you game save folder. You can use subfolders in the path -- file name should contain the file name and extension saver.save_file_by_name(data, file_name) saver.load_file_by_name(file_name) ``` -------------------------------- ### Configure Saver for Serialized Format Source: https://github.com/insality/defold-saver/blob/main/USE_CASES.md When saving data containing Defold userdata (like vmath.vector3, hash), use the 'serialized' format by not specifying a file extension in `saver.save_name` in `game.project`. This prevents data loss. ```lua saver.save_name = "game" ``` -------------------------------- ### saver.save_binary_by_name Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Saves the specified data as binary to a file with the specified name in the game save folder. Supports subfolders in the filename. ```APIDOC ## saver.save_binary_by_name ### Description Saves the specified data to a file with the specified name. The data format is binary. ### Method ```lua saver.save_binary_by_name(data, filename) ``` ### Parameters #### Parameters - **data** (string) - The binary data to save to the file. - **filename** (string) - The name of the file to save the data to. Can contain subfolders. ### Returns #### Returns - **is_saved** (boolean) - true if the file was saved successfully, false otherwise. ``` -------------------------------- ### Save Raw Binary Data by Name Source: https://github.com/insality/defold-saver/blob/main/USE_CASES.md Saves raw binary data, such as images or audio files, to the game's save directory using a specified file name. Requires the `saver` module to be required. ```lua local saver = require("saver.saver") -- Save a PNG image to the save directory local function save_image(file_url, file_name) -- Load image from url local file_url = "https://raw.githubusercontent.com/Insality/defold-saver/refs/heads/main/media/logo.png" http.request(file_url, "GET", function(_, id, response) if response.status == 200 or response.status == 304 then local image_data = response.response local success = saver.save_binary_by_name(image_data, file_name) return success end end) end -- Usage save_image("assets/my_image.png", "saved_image.png") ``` -------------------------------- ### File Handling by Relative Path Source: https://github.com/insality/defold-saver/blob/main/README.md Functions for saving, loading, deleting, and checking the existence of files using relative paths within the application data folder. ```APIDOC ## File Handling by Relative Path ### `saver.save_file_by_name(data, file_name, [format])` Saves data to a file in the application data folder using its name. - **data**: The data to save. - **file_name** (string): The name of the file. - **format** (constant, optional): The format to save the data in (e.g., `saver.FORMAT.SERIALIZED`, `saver.FORMAT.JSON`, `saver.FORMAT.LUA`). ### `saver.load_file_by_name(file_name, [format])` Loads data from a file in the application data folder using its name. - **file_name** (string): The name of the file. - **format** (constant, optional): The format of the data in the file. ### `saver.save_binary_by_name(data, file_name)` Saves raw binary data to a file in the application data folder using its name. - **data**: The binary data to save. - **file_name** (string): The name of the file. ### `saver.load_binary_by_name(file_name)` Loads raw binary data from a file in the application data folder using its name. - **file_name** (string): The name of the file. ### `saver.delete_file_by_name(file_name)` Deletes a file in the application data folder by its name. - **file_name** (string): The name of the file. ### `saver.is_file_exists_by_name(file_name)` Checks if a file exists in the application data folder by its name. - **file_name** (string): The name of the file. - Returns: (boolean) True if the file exists, false otherwise. ``` -------------------------------- ### Check if File Exists by Name Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Checks if a file with the specified name exists in the game save folder. Supports subfolders. ```lua saver.is_file_exists_by_name(filename) ``` ```lua local is_header_downloaded = saver.is_file_exists_by_name("/cache/header.png") ``` -------------------------------- ### saver.save_binary_by_path Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Saves the specified binary data to a file at the given absolute path. ```APIDOC ## saver.save_binary_by_path ### Description Saves the specified data to a file at the specified path. The data format is binary. ### Method ```lua saver.save_binary_by_path(data, path) ``` ### Parameters #### Parameters - **data** (string) - Required - The binary data to save to the file. - **path** (string) - Required - The absolute path to save the file to. Contains the file name and extension. ### Returns - **is_saved** (boolean) - true if the file was saved successfully, false otherwise. ``` -------------------------------- ### Adding Annotations to Game Profile Data Source: https://github.com/insality/defold-saver/blob/main/USE_CASES.md Adds custom fields to the default `saver.game_state` annotation class using `saver.bind_game_state` before saving. ```lua function init(self) --- --@class saver.game_state --@field profile profile.state @The `profile.state` is your persistent data annotation class --@field settings settings.state @The `settings.state` is your persistent data annotation class saver.init() saver.bind_game_state("profile", profile.state) saver.bind_game_state("settings", settings.state) end ``` -------------------------------- ### Check if File Exists by Path Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Checks if a file exists at the specified absolute path. Returns a boolean indicating existence. ```lua local is_project_file_exists = saver.is_file_exists_by_path(absolute_path_to_file) ``` -------------------------------- ### saver.load_file_by_name Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Loads data from a file with the specified name in the game save folder. Supports subfolders in the filename and optional format overrides. Returns nil if the file does not exist. ```APIDOC ## saver.load_file_by_name ### Description Loads the data from a file with the specified name. The file is loaded from the game save folder. Filename supports subfolders. NOTE: For binary data like images, use saver.load_binary_by_name instead. ### Method ```lua saver.load_file_by_name(filename, [format]) ``` ### Parameters #### Parameters - **filename** (string) - The name of the file to load the data from. Can contain subfolders. - **[format]** (string|nil) - Optional format override (json, lua, serialized, binary) ### Returns #### Returns - **data** (table|nil) - The data loaded from the file. If the file does not exist, returns nil. ### Example Usage ```lua local data = saver.load_file_by_name("data.json") pprint(data) ``` ``` -------------------------------- ### Set Custom Logger for Defold Saver Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Customize the logging mechanism used by Defold Saver by providing a logger instance. This allows integration with Defold's log library or custom logging solutions. Pass nil to remove the default logger. ```lua local log = require("log.log") local saver = require("saver.saver") saver.set_logger(log.get_logger("saver")) ``` -------------------------------- ### saver.save_file_by_name Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Saves the specified data to a file with the specified name in the game save folder. Supports subfolders in the filename and optional format overrides. ```APIDOC ## saver.save_file_by_name ### Description Saves the specified data to a file with the specified name. The file is saved in the game save folder. Filename supports subfolders. ### Method ```lua saver.save_file_by_name(data, filename, [format]) ``` ### Parameters #### Parameters - **data** (table) - The lua table to save to the file. - **filename** (string) - The name of the file to save the data to. Can contain subfolders. - **[format]** (string|nil) - Optional format override (json, lua, serialized, binary) ### Returns #### Returns - **is_saved** (boolean) - true if the file was saved successfully, false otherwise. ### Example Usage ```lua local data = { score = 100, level = 1 } -- Save the data to the game save folder saver.save_file_by_name(data, "data.json") ``` ``` -------------------------------- ### saver.save_game_state Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Save the current game state to a file. If no file name is provided, the default file name specified in the game.project file is used. ```APIDOC ## saver.save_game_state ### Description Save the current game state to a file. If no file name is provided, the default file name specified in the game.project file is used. ### Parameters #### Parameters - **save_name** (string|nil) - Optional - The name of the file to save the game state to. Default is the file name specified in the game.project file under the saver.save_file key. ### Returns - **is_saved** (boolean) - true if the game state was saved successfully, false otherwise. ### Example Usage: ```lua -- Save the game with default name saver.save_game_state() -- Save the game with custom name saver.save_game_state("custom_save") ``` ``` -------------------------------- ### saver.set_migrations Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Sets the list of migrations to apply after loading the game state manually with saver.apply_migrations() function. Migrations are used to update the game state in case of changes to the game state structure. Migrations are applied in order. Each migration should be a function that takes the game state as a parameter and returns the updated game state. ```APIDOC ## saver.set_migrations ### Description Sets the list of migrations to apply after loading the game state manually with saver.apply_migrations() function. Migrations are used to update the game state in case of changes to the game state structure. Migrations are applied in order. Each migration should be a function that takes the game state as a parameter and returns the updated game state. ### Method Not applicable (Lua function) ### Endpoint Not applicable ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **[migrations_table]** ((fun(game_state: saver.game_state, logger: saver.logger):nil)[]) - Required - Array of migration functions ### Request Example ```lua local migrations = { -- Migration 1 function(game_state, logger) -- Assume we have new level_data field in the game state and we need to move level and score to it game_state.game.level_data = { level = game_state.game.level, score = game_state.game.score } game_state.game.level = nil game_state.game.score = nil return game_state end, -- Migration 2 function(game_state, logger) -- Just an example, multiply the score by 1000. For example we changed our score system game_state.game.level_data.score = game_state.game.level_data.score * 1000 return game_state end } saver.set_migrations(migrations) saver.init() saver.bind_save_state("game", game_state) saver.apply_migrations() ``` ### Response #### Success Response None #### Response Example None ``` -------------------------------- ### Saving and Loading Specific Game States Source: https://github.com/insality/defold-saver/blob/main/USE_CASES.md Provides functions to save a specific game state by name and to reboot the system to load a specific game state, disabling autosave. Useful for managing multiple game modes or profiles. ```lua -- Example game_state.json local function save_game_state(game_state_name) -- Save the specific game state saver.save_game_state(game_state_name) end -- Example game_state.json local function load_game_state(game_state_name) -- Reboot and load the specific game state -- Also disable autosave to prevent overwriting the game state sys.reboot("--config=saver.save_name=" .. game_state_name, "--config=saver.autosave_timer=0") end -- Usage -- When you want to save the game state save_game_state("game_state_1.json") -- When you want to load the game state load_game_state("game_state_1.json") ``` -------------------------------- ### saver.is_file_exists_by_name Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Checks if a file with the specified name exists in the game save folder. Supports subfolders in the filename. ```APIDOC ## saver.is_file_exists_by_name ### Description Checks if the file exists with the specified name. The file is checked in the game save folder. Filename supports subfolders. ### Method ```lua saver.is_file_exists_by_name(filename) ``` ### Parameters #### Parameters - **filename** (string) - The name of the file to check. Can contain subfolders. ### Returns #### Returns - **is_exists** (boolean) - true if the file exists, false otherwise. ### Example Usage ```lua local is_header_downloaded = saver.is_file_exists_by_name("/cache/header.png") ``` ``` -------------------------------- ### Save Data by Name Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Saves Lua table data to a file with a specified name in the game save folder. Supports subfolders and optional format overrides. ```lua saver.save_file_by_name(data, filename, [format]) ``` ```lua local data = { score = 100, level = 1 } -- Save the data to the game save folder saver.save_file_by_name(data, "data.json") ``` -------------------------------- ### Save Data to File by Path Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Saves a Lua table to a file at a specified absolute path. The format is determined by the file extension or an optional format parameter. Useful for persistent storage of game data. ```lua local data = { score = 100, level = 1 } -- Get project path works on build from the Defold Editor only local project_path = saver.get_current_game_project_folder() -- Use path to the resources folder local file_path = saver.get_save_path(project_path .. "/resources/data.json") saver.save_file_by_path(data, file_path) ``` -------------------------------- ### Other Functions Source: https://github.com/insality/defold-saver/blob/main/README.md Miscellaneous utility functions for configuring and managing the saver library. ```APIDOC ## Other Functions ### `saver.set_autosave_timer(seconds)` Sets the interval in seconds for automatic saving. - **seconds** (number): The autosave interval in seconds. ### `saver.get_save_path(file_name)` Returns the absolute path to the save directory, optionally appending a file name. - **file_name** (string, optional): A file name to append to the save path. - Returns: (string) The absolute path. ### `saver.get_save_version()` Returns the current version of the save data. ### `saver.set_logger(logger)` Sets a custom logger function for the saver library. - **logger** (function): The logger function to use. ### `saver.get_current_game_project_folder()` Returns the path to the current game project folder. ### `saver.before_save_callback` A function that is called before the saver saves data. It can be used to perform actions just before a save operation. Example: ```lua saver.before_save_callback = function() print("About to save data...") end ``` ``` -------------------------------- ### Delete File by Path Source: https://github.com/insality/defold-saver/blob/main/api/saver_api.md Deletes a file at the specified absolute path. Returns a boolean indicating whether the deletion was successful. ```lua saver.delete_file_by_path(path) ``` -------------------------------- ### File Format Constants Source: https://github.com/insality/defold-saver/blob/main/README.md Constants representing different file formats supported by the saver library. ```APIDOC ## File Format Constants ### `saver.FORMAT.SERIALIZED` Represents the Lua serialized table format. This is the default format. ### `saver.FORMAT.JSON` Represents the JSON format. The library can autoselect this format if the file path ends with `.json`. ### `saver.FORMAT.LUA` Represents the Lua format. The library can autoselect this format if the file path ends with `.lua`. ```