- Home /
DOTS: Blob-compatible Dictionaries for use with YamlDotNet
I am making a kind of cellular automata experiment. Each type of block has its own YAML configuration file that is used to determine its behavior. Right now, I am deserializing each file as a blob asset, and storing a reference inside the entity. However, I am having trouble making a deserializable struct that will work with both Blobs and YamlDotNet, because of its use of dictionaries. The config file will not be changed once it is loaded into memory. What do I do here? My struct is the following:
public class ParsedRawDeserializationStruct
{
public enum Types
{
Block,
Creature,
Fluid
}
public enum BodyParts
{
Arm
}
public struct ParsedRawData
{
public GeneralData General;
public PropertiesData Properties;
public BehaviorsData Behaviors;
public NativeList<BodyParts> Anatomy;
}
public struct GeneralData
{
public FixedString32 id;
public NativeHashMap<FixedString32, FixedString32> name;
public NativeHashMap<FixedString32, FixedString32> desc;
public FixedString32 texture;
public Types type;
}
public struct PropertiesData
{
public NativeList<FixedString32> tokens;
public int hardness;
public NativeHashMap<FixedString32, int> species_stats;
public NativeHashMap<FixedString32, int> lifecycle;
public BlobPtr<UnsafeHashMap<FixedString32, int>> stats;
public NativeList<FixedString32> custom_props;
}
public struct BehaviorsData
{
public NativeHashMap<FixedString32, NativeList<FixedString32>> behaviors;
}
public struct RawBlobStruct
{
public BlobArray<ParsedRawData> Arr;
}
public static BlobAssetReference<ParsedRawDeserializationStruct.RawBlobStruct> CreateRawBlobAsset(ParsedRawData data)
{
using var blobBuilder = new BlobBuilder(Allocator.Temp); //new builder
ref var parsedRawBlobAsset = ref blobBuilder.ConstructRoot<RawBlobStruct>(); //create new asset
var arrays = blobBuilder.Allocate<ParsedRawData>(ref parsedRawBlobAsset.Arr, 1);
arrays[0] = new ParsedRawData();
BlobAssetReference<ParsedRawDeserializationStruct.RawBlobStruct> blobAsset = blobBuilder.CreateBlobAssetReference<RawBlobStruct>(Allocator.Persistent); //create reference
blobBuilder.Dispose();
return blobAsset;
}
}
also while we're at it: how big is a BlobAssetReference in bytes? just curious.
how big is a BlobAssetReference in bytes?
unsafe
{
int size = sizeof(BlobAssetReference<ParsedRawDeserializationStruct.RawBlobStruct>);
UnityEngine.Debug.Log($" size: {size} bytes");
}
prints: size: 8 bytes
ah ,thank you. I figured as much because I believed it to be a 64 bit pointer. A little larger than I would like, but I can make it work.
General advice: don't mix Unity.Collections
' types with blob assets. Choose either one of them.
Blobs are read-only so no dependencies to manage, which is convenient for all read-only types of data like this one.
System.Collections.Generic.Dictionary<T,K>
is something you won't see anywhere close blob assets and Unity.Collections
. If you need Blob-HashMap
you can check how others implemented it already and
copy
study their code samples. Alternatively, you can store hashmap entries as BlobArray<KeyValuePair<T,K>>
or BlobArray<T> keys
and BlobArray<K> values
to just run linear search if you can get away with it (low-overhead for short arrays only).
Yeah, I know that I shouldn't be using collections, I just don't know another way around it for deserialization. Curious. I'm using the implementation here, https://github.com/bartofzo/BlobHashMaps , but I still get the error ConstructBlobWithRefTypeViolation: You may not build a type RawBlobStruct with Construct as RawBlobStruct.Arr[].General.name.data.values[] is a reference or pointer. Only non-reference types are allowed in Blobs. error...
Your answer
Follow this Question
Related Questions
Do blob shadow projectors work on iPhone? 1 Answer
How to save blob in bytes to sqlite 0 Answers
ECS How can I pass a variable into rendermesh closure? 0 Answers
How to use multiple DynamicBuffer in one entity 1 Answer
Why Unity stops call OnUpdate in the ComponentSystem after an entity destroy? 1 Answer