User Tools

Site Tools


custom-data

DATA classes are the most basic type in Foundation that all other types are based on.

For reference all available pure DATA classes can be found in this section of the API.

Creating & Using a Custom DATA Object

Step 1: Defining a DATA Object using a Lua Table

To create a custom DATA object we will need to make use of the mod:registerClass(DataTable) function providing it with an associated Lua Table.

Here you decide what properties your custom DATA object will contain.

local MY_DATA_OBJECT = {
    TypeName = "MY_DATA_OBJECT ",
    Properties = {
        { Name = "Property1", Type = "string", Default = "SomeString", Flags = { "SAVE_GAME" }},
        { Name = "Property2", Type = "list<RESOURCE_QUANTITY_PAIR>", Default = {} },
        { Name = "Property3", Type = "integer", Default = 1 }
    }
}
 
mod:registerClass(MY_DATA_OBJECT)

Make sure not to forget the SAVE_GAME flag if you intend to store runtime values that need to persist accross a reload!

Also remember that you can define properties based on the basic data types (i.e. string, integer, float, etc.) or based on any of the existing DATA or ASSET objects, as well as lists therof!

Step 2: Using a DATA Object

Note: This also aplies to using vanilla DATA Objects!

During gameplay when you will need to instantiate a DATA object you will have to use the foundation.createData(…) method as follows.

local myData = foundation.createData(
    {
        DataType = "MY_DATA_OBJECT", 
        Property1 = "SomeOtherString",
        Property3 = 5
    }
)

In case you omit any properties you don't yet need to explicitly set they will use their default values until otherwise specified. As we have done withProperty2 above.

If you want to alter a property after the DATA object has been instantiated you can of course do so at will like this.

myData.Property1 = "SomeAlteredString"
custom-data.txt · Last modified: 2022/03/29 20:58 by minotorious

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki