So, I have the following struct that I get from a system before using it in jobs in other systems:
public struct SerializedIntMap
{
private readonly NativeHashMap<FixedString64, Entity> hashMap;
private readonly ComponentDataFromEntity<SerializedInt> componentDataFromEntity;
public SerializedIntMap(NativeHashMap<FixedString64, Entity> hashMap,
ComponentDataFromEntity<SerializedInt> componentDataFromEntity)
{
this.hashMap = hashMap;
this.componentDataFromEntity = componentDataFromEntity;
}
public int GetValue(FixedString64 id)
{
if (hashMap.TryGetValue(id, out var entity))
{
return componentDataFromEntity[entity].Value;
}
Debug.LogError($"Can't find SerializedInt named {id}");
return default;
}
}
That I use like that:
var map = mappingSystem.GetIntMap();
Entities.ForEach((Whatever whatever) =>
{
var value = map.GetValue(whatever.Id);
// Do stuff with value
}).Schedule();
It works out well, but if I have multiple system scheduling jobs using those, it will output an error because it thinks that I write to the same NativeHashMap (it’s always the same in all SerializedIntMap), even if I only read from it.
Now, I tried to do the following:
Entities.WithReadOnly(serializedIntMap)...
which changes nothing, I guess because SerializedIntMap isn’t considered a NativeCollection, or
var hashMap = serializedIntMap.hashMap;
Entities.WithReadOnly(hashMap)...
Which doesn’t work either because it says that hashMap isn’t used in the Lambda expression (even if it’s used in GetValue calls). Only way I made it work is by explicitely getting hashMap from the , adding it to WithReadOnly and adding it as an argument of GetValue. It works, but it’s not as clean as my previous design.
I guess the best way to do something like this would be to find a way for my SerializedIntMap to be considered a NativeCollection that will contain another NativeCollection (the NativeHashMap), but I’m not sure it’s even possible. I’m not really used to using unsafe, I’m not sure it’s needed in that case too. Anybody has any idea how I could make that work?