DataManager

An object designed to flexibly store and retrieve numbers and strings very quickly use key/value pairs.

About

In addition to any Entity having its own unique DataManager via Entity::Data a global one is always available through GameLogic::Data as well.

Data is persistant automatically, ie, saved between sessions.  There are no size limits and lookups are extremely fast.  (Internally, a binary search tree is used)

Access from the editor

Using the Entity Properties Editor, you can also add/remove/modify data from within editor, in addition to normal script access.

Summary
DataManagerAn object designed to flexibly store and retrieve numbers and strings very quickly use key/value pairs.
Storing Data
Set
SetNum
SetIfNull
SetNumIfNull
Retrieving Data
Get
GetNum
GetNumWithDefault
GetWithDefault
Miscellaneous
Exists
ModNum
Delete
Clear

Storing Data

Set

boolean Set(string keyName, string value)

Store a string as a named piece of data to be retrieved later.  Replaces data by this key name if it already exists.

keyNameAny name you wish.
valueA string of any length with the text/data that you wish to store.

Returns

True if a new piece of data was created, false if existing data was replaced.

SetNum

boolean SetNum(string keyName, number num)

Similar to Set but saves the data as a number which saves space and is faster to access internally.

When <Get>is used with a number (instead of GetNum) it is automatically converted into a string.

Decimal points are ok to use.  The accuracy maintained is that of a C “float”.

keyNameAny name you wish.
numThe number you wish to store.

Returns

True if a new piece of data was created, false if existing data was replaced.

SetIfNull

string SetIfNull(string keyName, string value)

Similar to Set but stores the data only if the key didn’t already exist.

keyNameAny name you wish.
valueThe string data you wish to store.

Returns

String that was set, or existed before.

SetNumIfNull

number SetNumIfNull(string keyName, number value)

Similar to Set but stores the data only if the key didn’t already exist.

Like <Data:SetIfNull> but hints that we want it stored as a number, not a string.

keyNameAny name you wish.
valueThe number you wish to store.

Returns

Returns the number that was set or existed before.

Retrieving Data

Get

string Get(string keyName)

Retrieve a previously stored value by its key name.

keyNameThe key-name used when it was stored.

Returns

The data in string form or a blank string if the key wasn’t found.  Use Exists to verify if a key exists or not.

GetNum

number Get(string keyName)

Like Get but returns the data as a number.  If the data was stored as a number, this is the fastest way to access it.

If the data was stored as a string, an attempt to convert it to a number is made.

keyNameThe key-name used when it was stored.

Returns

The number that was stored.

GetNumWithDefault

number GetNumWithDefault(string keyName, number defaultValue)

Like Get but allows you to also set the default data if the key doesn’t exist yet.

keyNameThe key-name used when it was stored.
defaultValueIf the key doesn’t exist, it will be created with this value.  (this value will be returned as well)

Returns

The value that was stored or created.

GetWithDefault

number GetWithDefault(string keyName, string defaultValue)

Like Get but allows you to also set the default data if the key doesn’t exist yet.

keyNameThe key-name used when it was stored.
defaultValueIf the key doesn’t exist, it will be created with this value.  (this value will be returned as well)

Returns

The value that was stored or created.

Miscellaneous

Exists

boolean Exists(string keyName)
keyNameThe key name used when it was stored.

Returns

True if data with this key name exists.

ModNum

number Exists(string keyName, number modAmount)

Modifies an existing key by a number.  Creates the key if it didn’t exist.

Usage

//set hitpoints to 100
this:Data():SetAsNum("life", 100);

//remove 5
local curHitpoints = this:Data():ModNum("life", -5);

LogMsg("I only have " .. curHitpoints .. " hitpoints!");
keyNameThe key name used when it was stored.
modAmountHow much the number should be changed by.

Returns

The new number that was stored.

Delete

nil Delete(string keyName)

Completely removes a key/value pair from the database.

keyNameThe key name used when it was stored.

Clear

nil Clear()

Completely removes all stored data from this database.

The Entity object.
An object designed to flexibly store and retrieve numbers and strings very quickly use key/value pairs.