Token
Token - library designed for the Defold game engine to manage countable items such as money, lives, and other numeric values. This library provides a robust and flexible system for handling various token-related operations, including creation, management, and restoration of token values.
Features
- Token Management - Create, delete, and manage tokens within containers.
- Container Management - Create, delete, and manage token containers.
- Visual Management - Control the displayed amount separately from the actual value for smooth UI animations.
- Callbacks - Customizable callbacks for token changes.
- Token Groups - Support for grouped token operations.
- Token Restoration - Configurable restoration mechanics for tokens.
- Infinity Tokens - Manage tokens with infinite time usage.
Setup
See the Defold Token repository on Github
Basic Usage
In Token library, you create a container which contains tokens.
local token = require("token.token")
token.init({
["money"] = { default = 100, min = 0, max = 10000 },
["exp"] = {},
["level"] = { default = 1, min = 1, max = 100 },
})
local state = token.get_state() -- get the current state for save/load
token.set_state(state) -- set the state for save/load
token.container("wallet"):add("exp", 100) -- Will be 100, due the default value is 0
token.container("wallet"):add("level", 1) -- Will be 2, due the default value is 1
token.container("wallet"):pay("money", 100) -- Will be 0, due the default value is 100
token.container("wallet"):add("gems", 20) -- Token configs is not required to operate with tokens
-- Configs is not required to operate with tokens
token.container("skill"):add("damage", 100) -- Return token instance
token.container("skill"):get("damage") -- Return 100
token.container("skill"):set("crit_chance", 0.1) -- Set crit chance to 10%
Read Use Cases for more examples.
Support project 
Your donation helps me stay engaged in creating valuable projects for Defold. If you appreciate what I’m doing, please consider supporting me!
