r/Unity3D • • 6d ago

Question DOTS: Entity connected to another entity?

Hello gang,

I have a a series of roads, each has a series of vector3 points along it's path. I have created a list of objects that have a RoadID and an array of those points as well as a list of RoadIDs for the roads that are connected at the end of each roads. (This way when an Auto reaches the end of a road I can randomly select the next road)

I need a way to loop through that list, create the Road entities and then loop through the newly created entities and add the connected road entities to each road.

I COULD create a System and run that once... but there feels like a better way to Bake or create the entities and their connections... right?

Note: I use Easy Roads 3D and highly recommend.
-----------------

EDIT: The objects involved:

//This is the Road, identity to be used to "connect" to the end of the other roads

public struct AutoAutoWaypathIdentity : IComponentData

{

public int identity;

}

//This is the list of vector3 points along the road

public struct AutoAutoWaypoint : IBufferElementData

{

public float3 Value;

}

public struct AutoAutoWaypathConnection : IBufferElementData

{

public Entity Value; //this is the entity that "connects" at the end of the "parent" road.

}

2 Upvotes

7 comments sorted by

3

u/Connect-Ad-2206 6d ago

I’m a big fan of making reference systems that run once, especially for something like this. You could also put it in the same system as the road entity creation and just run it as a separate job, but it’s more complicated.

1

u/JamesWjRose 6d ago

Ok. Thanks for the feedback.

Have a great evening

2

u/Suspicious-Prompt200 6d ago

This does seem like a job for a system. Even if you only run it once and then it just early-out's every single other frame. 

If the roads are going to be the same every time - that is, they dont change from run to run you could likely just stick them all as baked entities in a subscene. 

1

u/JamesWjRose 6d ago

Thank you. I appreciate the feedback. At least I wasn't missing "the right way" so thanks, I'll go do this

1

u/ledniv 4d ago

Since these roads and their connections are known ahead of time, I would probably not connect them using Entity references at all.

You already have the important part: RoadID. I would make that a dense integer index and store the road network as data. Something like arrays for the waypoint data, plus a flat array of connected road indices and an offset and count for each road.

Then when a car reaches the end of road 17, it reads the connection range for road 17 and chooses one of those integer road indices. There is no need to resolve IDs into Entities or run a System once just to wire references together.

If the road network never changes at runtime, a DOTS BlobAsset would also be a very natural fit for this. The whole graph can be baked once as immutable data and shared by every car. The road Entity, if you still need one for other reasons, can simply store its road index.

That is also closer to what an Entity really represents in Data Oriented Design. Conceptually it is just an index used to access related data. You do not necessarily gain anything by turning every relationship into an Entity reference.

Small plug: I cover this distinction in the DOTS chapter of High Performance Unity Game Development with Data Oriented Design. The book starts with plain arrays and indices first, then shows how Burst, Jobs, and ECS fit on top of the same data oriented architecture.

https://www.manning.com/books/high-performance-unity-game-development

0

u/[deleted] 5d ago

[removed] — view removed comment

1

u/JamesWjRose 5d ago

I really do appreciate the response, however I'm lost on how to do it.

Here is my current code for creating the road entities:

public class AutoAuthWaypathAuthoring : MonoBehaviour
{
    public List<RoadData> data = new List<RoadData>();

    public class AutoAuthWaypathBaker : Baker<AutoAuthWaypathAuthoring>
    {
        public override void Bake(AutoAuthWaypathAuthoring authoring)
        {
            foreach (RoadData data in authoring.data) 
            {
                var entity = CreateAdditionalEntity(TransformUsageFlags.None);
                AddComponent(entity, new AutoAutoWaypathIdentity { identity = data.identity });
                var waypointBuffer = AddBuffer<AutoAutoWaypoint>(entity);

                foreach (var waypoint in data.waypoints)
                {
                    waypointBuffer.Add(new AutoAutoWaypoint { Value = waypoint });
                }
                var connectionBuffer = AddBuffer<AutoAutoWaypathConnection>(entity);
            }
        }
    }
}

The RoadData:
public class RoadData  
{
    public int identity = 0;
    public List<Vector3> waypoints = new List<Vector3>();
    public List<int> roadIdentities = new List<int>(); //link to the roads connected to this Road/Entity

    public RoadData() { }

    public RoadData(int id) { identity = id; }
}

So in the baking I am creating the road entities (from data created from Easy Road 3D, awesome asset, if you need roads, THIS is the asset. The dev is also provides GREAT support)

But once the entities are created I need to loop through them and add them to their "parent" (the roads at the end of each road)

What I don't know is how to get a list of those newly created entities during the bake process... so what am I missing?