Defold Token - Countable Items Container

logo

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.

:heart: Support project :heart:

Your donation helps me stay engaged in creating valuable projects for Defold. If you appreciate what I’m doing, please consider supporting me!

Github-sponsors Ko-Fi BuyMeACoffee

4 Likes

There is several Debug Panels Druid’s Widgets in the Asset Store. One of them is Token Debug Panel which allows you to see the current state of the tokens and containers and make some actions with them.

Live example: Asset Store 1.0
Source code: asset-store/widget/Insality/debug_page_token/example/example_debug_page_token.gui_script at main · Insality/asset-store · GitHub

This debug panel built with Druid Properties Panel Widget. You can easily extend it to your own needs by adding new properties or modifying the existing ones.

3 Likes

Thank you

2 Likes