Skip to content

Instantly share code, notes, and snippets.

@cdwfs
Last active January 14, 2019 22:47
Show Gist options
  • Save cdwfs/99535cc37ecae5d6e9743eb46fbb7638 to your computer and use it in GitHub Desktop.
Save cdwfs/99535cc37ecae5d6e9743eb46fbb7638 to your computer and use it in GitHub Desktop.
Simple, pseudo-psuedocodey demonstration of how to communicate data from "parent" entities to "child" entities
/// The goal here is to have some processing in one set of entities (the parents) whose results need to
/// be consumed by a different set of entities (the children), where it's not possible to iterate over both
/// sets simultaneously.
/// In this specific example, each parent entity has two unique child entities.
/// Different approaches would be necessary for one-to-many relationships, many-to-many relationships, etc.
///
/// NB I've never actually compiled this code; there may be minor errors due to typos or API changes.
struct ParentData : IComponentData
{
Entity child1;
Entity child2;
//actual parent data goes here
}
struct ChildDataFromParent : IComponentData
{
int dummyData; // ...or whatever
}
class ParentChildBarrier : BarrierSystem {}
[UpdateBefore(typeof(ParentChildBarrier))]
class ParentSystem
{
// TODO: working on a way to avoid injecting this barrier but it's not released yet
[Inject] ParentChildBarrier _barrier;
struct ParentJob : IJobProcessComponentDataWithEntity<ParentData>
{
public EntityCommandBuffer.Concurrent CommandBuffer;
void Execute(Entity e, int index, ref ParentData parentData)
{
// perform actual computations on the parent here
// Write relevant results of parent computation to child entities
CommandBuffer.SetComponent(parentData.child1, index, new ChildDataFromParent { dummyData = ...; });
CommandBuffer.SetComponent(parentData.child2, index, new ChildDataFromParent { dummyData = ...; });
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
return new ParentJob
{
CommandBuffer = _barrer.CreateCommandBuffer().ToConcurrent();
}.Schedule(this, inputDeps);
}
}
[UpdateAfter(typeof(ParentChildBarrier))]
class ChildSystem
{
// iterate over child entities, reading from ChildDataFromParent components as necessary
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment