Skip to content

Properties Loader

This module loads data from scriptable arrays of serializable IComponentData into entities. Then you can query/get these data in your systems with ecs-y way.

How to prepare data

  1. Mark component you want to serialize with [Serializable] attribute
    [Serializable]
    public struct UnitSpeed : IComponentData {
        public float Value;
    }
    
  2. Prepare scriptable object which can be serialized as an asset:
    [CreateAssetMenu(fileName = "UnitSpeedScriptable", menuName = "Stratkit/Units/UnitSpeedScriptable", order = 2)]
    public class UnitSpeedScriptable : PropertyScriptableUnmanaged<UnitSpeed> { }
    
  3. Create new asset in your project of the newly created scriptable object using the CreateAssetMenu, and fulfill the list inside with some data.
    picture
    picture
  4. Repeat point 3. with DataIdScriptable, because each entity has to be mapped by custom Id:
    picture
  5. If you have more components that you want to serialize and load, repeat point 1-3. for them.
  6. Using the menu under Create/Stratkit/Properties/PropertiesCollection, create new asset of PropertiesCollectionScriptable, and assign there all scriptable assets you created before:
    picture
  7. Prepare new tag with unique name which distinguishes your newly created collection from other collections:
    public struct UnitDataTag : IComponentData { }
    

How to load data

Load your data as an asset with any way you want or need, e.g. via addressables:

string path = $"path_to_asset/UnitsBalancingData.asset";
Addressables.LoadAssetAsync<PropertiesCollectionScriptable>(path).Completed += handle => { ... };

How to convert data

When your data are loaded, trigger converting them:

PropertiesCollectionScriptable loadedObject = ...
PropertiesLoaderModule.Add(World, loadedObject, typeof(UnitDataTag));

How to query data

Just make a query of any component you want, e.g. WithAll<UnitSpeed>.

How to get data by Id

If you dont want to query all data but only the ones that come from your scriptable object you have prepared previously, use your unique tag, e.g., UnitDataTag, to get a dictionary containing them:

EntityQuery query = new EntityQueryBuilder(Allocator.Temp)
                    .WithAll<EntitiesDictionary, UnitDataTag>().Build(EntityManager);
EntitiesDictionary dict = query.ToComponentDataArray<EntitiesDictionary>(Allocator.Temp)[0];
Then you can get any entity from the dict:
Entity entry0 = dict.Map[4]; // these ids: 4, 8, 6 are exactly the same as you typed in DataIdScriptable
Entity entry1 = dict.Map[8];
Entity entry2 = dict.Map[6];
UnitSpeed speedComponent0 = EntityManager.GetComponentData<UnitSpeed>(entry0);
UnitSpeed speedComponent1 = EntityManager.GetComponentData<UnitSpeed>(entry1);
UnitSpeed speedComponent2 = EntityManager.GetComponentData<UnitSpeed>(entry2);