I’ve been working through learning ECS (relearning from pre .5… SO MUCH has changed)
Before I attempt to go too deep into trying this process I thought I’d run it by the group to see if I have the right idea, or more likely, wtf I am doing wrong.
Usage: I have roads and with them I am making a series of circuits, a series of vector3/float3 points in a loop around 1 or more blocks. (These are basic for HIGH flying hover cars, so simple loops are find for traffic at this level. Game: r/HeartbeatCityVR )
I found that I can use BlobAssets so that multiple vehicles can access the same ‘circuit’ data. (yay for optimization, ESP for VR!!!)
I am still working on understanding the full Bake lifecycle, so here is my thought on how the process would go for this:
Traffic Circuit Baker: This goes through a List of Vector3 points (actually a List of Lists, each circuit has it’s own set of points) Create a Blob Asset, add those points to the asset, then set that asset reference to a new “Circuit” Entity (for use in a System for spawning autos on these circuits)
Auto Prefab Baker: This is a List of GameObjects, Hover Autos, that are converted to Entities
Spawner System: A system that queries the Circuits and Autos. Loop through each Circuit and spawn autos and adds that Circuit reference to these new Auto Entities.
Does this process make sense? I’m I forgetting something?
If there is anything you are forgetting, then I would have to read in between the lines to identify it. I’d probably call “Auto Prefab Baker” “SpawnerBaker” because what that really is going to do is call GetEntity() on a bunch of other prefabs which each have their own components and associated bakers. But otherwise, what you are looking to do looks pretty much how I would do it. Maybe I would try to give the spawners the blob asset references at bake time so that they can more easily initialize everything else.
Maybe I would try to give the spawners the blob asset references at bake time so that they can more easily initialize everything else.
Sounds GREAT… um, how? This is my base problem, finding how how to init data and communicate between objects/entities/etc
Maybe if I show what I want: One BAKER to make the Autos and Crcuits
public class SkyTrafficData
{
public List<UnityEngine.Vector3> points = new List<UnityEngine.Vector3>();
}
public class HoverTrafficAuthoring : MonoBehaviour
{
public List<SkyTrafficData> skyTrafficData = new List<SkyTrafficData>();
public List<GameObject> trafficPrefabs = new List<GameObject>();
}
public class HoverTrafficBaker : Baker<HoverTrafficAuthoring>
public override void Bake(HoverTrafficAuthoring authoring)
{
foreach (var trafficData in authoring.skyTrafficData)
{
//Create the Circuit
int dataCount = trafficData.points.Count;
using var blobBuilder = new BlobBuilder(Allocator.Temp);
ref var waypointBlobAsset = ref blobBuilder.ConstructRoot<WaypointBlobAsset>();
BlobBuilderArray<Waypoint> waypointArray = blobBuilder.Allocate(ref waypointBlobAsset.waypointArray, dataCount);
for (int i = 0; i < dataCount; i++)
{
waypointArray[i] = new Waypoint { position = trafficData.points[i] };
}
BlobAssetReference<WaypointBlobAsset> waypointBlobAssetRef = blobBuilder.CreateBlobAssetReference<WaypointBlobAsset>(Allocator.Persistent);
//Create the Auto
var auto = GetEntity(authoring.trafficPrefabs[0], TransformUsageFlags.Dynamic);
LocalTransform localTransform = new LocalTransform();
localTransform.Position = point;
//SetComponent(auto, localTransform); //This fails!
//AddComponent(auto, localTransform); //This fails!
//AddComponent(auto, new AutoTag { }); //This fails!
//WOULD LIKE TO ADD THE "CIRCUIT" TO THE
//NEWLY CREATED AUTO ENTITY, BUT NOPE,
//CAN'T ADD ANYTHING.
}
}
}
The entity returned by GetEntity is a shared entity associated with whatever GameObject you pass in. For that reason, you wouldn’t want to initialize the circuit blob directly on the autos during baking because then you could only have one circuit. You probably want to initialize the circuit on the autos at runtime when you spawn them.
However, the spawner could receive a reference to the circuit blob directly in this baker, assuming this baker is also responsible for creating the spawner.
My question is, how do I pass a ref from the baker to the Spawner? (I’m going to guess my answer is “duh”) I assume my Spawner script can have a array/list that I can pass the newly created Circuits (there will be multiple) Then I can loop through those circuits, spawn the autos and assign said Circuits. Do I have that right?
Well that’s the question I guess, I’m attempting to do this the right way, whatever pattern is most beneficial.
I COULD just add the known points to each Auto, duplicate the points in each Auto. Boooo, bad idea. So I found out about Blob Assets, WOO HOO, a single point for all Autos to get their path. Now I’m trying to understand Baking and Spawning… and as you see, I’m not doing well.
And because I learn best by example, AND because this is all so new, the documentation sucks, as usual, not a lot of examples yet, etc etc, I keep floundering. I REALLY appreciate all your help, so thanks.
I’m awake from dev machine atm, but my spawner was just looping through Circuit entities, via a query in a SystemBase, and creating instances of autos made via another Baker.
But, again, I’ll do it whenever way is the correct. I’m just having a hard time finding out, figuring it out.
There’s multiple ways to do it. The main thing is that your spawner somehow needs the BlobAssetReference of the circuit at runtime so it can assign those to the autos it spawns. If the spawner and the circuit are baked in separate bakers, then you need a fancy blob deduplication baking system. I have some tech built to make that easier. But if the circuit and the spawner are baked by a single baker, then the baker can just bake the blob and you are good to go.