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¶
- Mark component you want to serialize with
[Serializable]
attribute - Prepare scriptable object which can be serialized as an asset:
- Create new asset in your project of the newly created scriptable object using the
CreateAssetMenu
, and fulfill the list inside with some data.
- Repeat point 3. with
DataIdScriptable
, because each entity has to be mapped by custom Id: - If you have more components that you want to serialize and load, repeat point 1-3. for them.
- Using the menu under
Create/Stratkit/Properties/PropertiesCollection
, create new asset ofPropertiesCollectionScriptable
, and assign there all scriptable assets you created before: - Prepare new tag with unique name which distinguishes your newly created collection from other collections:
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];
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);