User Tools

Site Tools


custom-asset

ASSET classes are the basic registerable types in Foundation.

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

Creating & Using a Custom ASSET Object

Step 1: Defining an ASSET Object using a Lua Table

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

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

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

Note that ASSETs unlike DATA do not need the SAVE_GAME flag as their properties are read from their registration on load!

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: Registering an Instance of an ASSET Object

Note: This also aplies to using vanilla ASSET objects!

In your mod's lua code you when you want to register an instance of your new ASSET object you will have to use the mod:registerAsset(AssetTable) method as follows.

mod:registerAsset({
    DataType = "MY_ASSET_OBJECT",
    Id = "MY_ASSET_OBJECT_INSTANCE",
    Property1 = "SomeOtherString",
    Property3 = 5
})

Remember, each unique ASSET instance requires an Id that identifies it from other ASSETs of the same class.

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

Step 3: Using an Instance of an ASSET Object

When you want to use your new ASSET instance be it in a COMPONENT property, BUILDING_FUNCTION, or other ASSET/DATA class you can use the string Id to reference it by name.

For example here we are using our custom ASSET instance as the default value in a COMPONENT

local COMP_EXAMPLE = {
	TypeName = "COMP_EXAMPLE ",
	ParentType = "COMPONENT",
	Properties = {
        { Name = "Property1", Type = "MY_ASSET_OBJECT", Default = "MY_ASSET_OBJECT_INSTANCE" }
    }
}
custom-asset.txt · Last modified: 2022/03/30 10:16 by minotorious

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki