Storing data in Lua objects

Several different kinds of objects in the Stingray Lua API offer you a generic interface for storing and retrieving data values.

Using this system has some advantages over storing the data in your own Lua variables:

Every type of object that offers this data storage mechanism has the following three functions:

set_data()
get_data()
has_data()

The stored data may be:

Each piece of data is indexed by a series of one or more keys, each of which may be either a string or an integer. When you call the set_data() function to store a piece of data, you pass the keys as arguments along with the data value you want to store. You can then retrieve the data or check if the data exists by passing the same series of keys to the get_data() or has_data() functions.

For example, in the following code the value 1 is indexed by a series of two keys, score and headshots:

stingray.Unit.set_data(player, "score", "headshots", 1)
...
local shots = stingray.Unit.get_data(player, "score", "headshots")