Skip to content

pocketgems/com.pocketgems.scriptableobject.flatbuffer

Repository files navigation

Scriptable Object - FlatBuffer

About

The package streamlines data synchronization between Scriptable Objects and CSVs. It automatically packages the data into Google FlatBuffers for runtime use for fast loading and reduced memory footprint. A data validation workflow is provided to enforce and maintain the integrity of data and its interrelationships.

Benefits

  • Allows bulk modifications through CSVs or fine-turning of entities through the Scriptable Object inspector.
  • Runtime advantages of Google FlatBuffers without imposing the complexities associated with the conventional FlatBuffer workflow.
    • No overhead of runtime deserializing of Scriptable Objects.
    • Compressed de-duplicated data in memory.
  • Auto data validation of generated runtime data & during inspector edit time.

High Level Flow

Features

Define Data Types

Define a Scriptable Object or struct by creating an interface.

public interface ICurrencyInfo : IBaseInfo
{
    bool IsPremiumCurrency { get; }
    LocalizedString DisplayName { get; }
    LocalizedString Description { get; }
    short StartingAmount { get; }
    AssetReferenceSprite Icon { get; }
}

Content Workflow

Create & modify Scriptable Objects in editor.

Scriptable Object Inspector

Or modify in bulk using the sync'd CSV(s).

Identifier IsPremiumCurrency DisplayName Description StartingAmount Icon
string bool LocalizedString LocalizedString short AssetReferenceSprite
Coin 0 Coin Common currency 100 4c36500ce4684478781316054b5e16d6-coin
Gems 1 Gem Shiny! 50 -
Lumber 0 Lumber Chop chop chop 10 -
Stone 0 Stone I'm a Stone 20 -

Generated Asset

A Parameter.bytes asset is automatically generated for runtime use and can be loaded through Resources or as an Addressable asset.

Querying Data

Streamlined API for inline data retrieval.

# get a specific currency item
ICurrencyInfo coinInfo = Params.Get<ICurrencyInfo>("Coin");
if (coinInfo != null)
{
  bool isPremiumCurrency = coinInfo.IsPremiumCurrency;
  // ...
}

# iterate over all CurrencyInfos
IEnumerable<ICurrencyInfo> currencyInfos = Params.Get<ICurrencyInfo>();
foreach (ICurrencyInfo info in currencyInfos)
{
  Debug.Log($"{info.Name}: {info.StartingAmount}")
}

Data Validation

Validation window to see all failed checks.

Validation Window

Inspector support for on-the-fly validation.

Inspector Validation

Quickly add validation with attributes.

public interface ICurrencyInfo : IBaseInfo
{
    bool IsPremiumCurrency { get; }

    [AssertStringNotEmpty]
    LocalizedString DisplayName { get; }

    [AssertStringNotEmpty]
    LocalizedString Description { get; }

    [AssertGreaterOrEqual(0)]
    short StartingAmount { get; }

    AssetReferenceSprite Icon { get; }
}

Support to implement custom validations as required.

protected override void ValidateInfo(IParameterManager parameterManager, ICurrencyInfo info)
{
  if (info.IsPremiumCurrency && string.IsNullOrEmpty(info.PremiumCurrencyIconSprite.AssetGUID))
    Error(nameof(ICurrencyInfo.PremiumCurrencyIconSprite), "required for premium currency");
}

Documentation

  1. One Time Setup: Guide for initial project configuration and code bootstrapping.
  2. Defining Data Types: Instructions to define and adjust data types.
    1. Interfaces & Enums: Specifics about defining interfaces & enums.
  3. Content Workflow: Process for creating and modifying data using CSVs or Scriptable Objects.
  4. Generated Asset: Details about the asset that holds the auto generated parameter data.
  5. Querying Data: Techniques and guidelines for querying data at runtime.
  6. Data Validation: Establish rules and criteria for ensuring data integrity.
    1. Validation Attributes: All valid attributes for validation use.
  7. Troubleshooting

This README follows the structure of Make a README.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages