PCG Randomize mesh spawn along spline.

I’m trying to create a PCG spline that randomly spawns cars along it at different distances from one another and ensures they don’t intersect.

My idea is to have two variables (min & max distance values) between which a random value will be generated to set the distance increment for each spawning mesh along the spline.

Now, my problem is how exactly to achieve this. I’ve tried following some tutorials online but haven’t found one that covers this example yet.

Any and all feedback is appreciated!

Why pcg?

Do it manually.
Lenght along spline is an option you can read via BP.
Define the min and max, pick a random from it, spawn the car where you need.

You would do this for gameplay duing runtime, so you uaue an actor that is set up to be a spawn manager.

And you likely want to run the event through a timeline so as not to abuse ontick.

You also need to remove the cars once you are past a certain distance from them - leaving them would cause the system to just become slower as more spawns keep occurring…

Okay, I think I almost have it, but I’m still not doing something right.

So you know, this isn’t for runtime, just for static renders I’m doing so no need to worry about gameplay here.

I followed this tutorial, which has helped me get somewhat of the way. However, what I’m running into now is that the trucks are still evenly spaced out, not at varying lengths from one another like I want.

I understand it’s because I’m dividing the “Get Spline Length” by the (vehicle’s length + offset). I’m just unsure how to best replace that so I can tell it to spawn them the length of the spline at various amounts.

Hope this makes sense. :sweat_smile:

It does and it doesn’t at the same time.

The random value has to be generated every time you create things - otherwise it’s just the same value all the time.

Basically, if you move the code block from where you have it to “inside” the loop (copy and paste + use within the loop, you should get the result you are looking for.

Obviously that introduces an issue;
You can’t divide the spline length by a random number.
Probably substitute the max possible variable in there (car length + max random) so that you always have vehicles set up with values which are below that maximum distance.

Rendering or not - you can add an event as a bluetility, connect it to replace the OnBegin, and use that to just pace instances along the spline in a way that the editor saves them.
In case you need to re-open the scene and get the same stuff, you really should do that.

Personally, I’d just replace the instance code you have to work a bit different.

Add whatever objects or meshes you want to the level.
Give them the same gamplay tag.

Mod the script you have to get and loop all objects with that gamplay tag definition.

You can compute the max spline distribution and make the max a value that makes sense to the equation based on the total count: spline length / tot items = leftover value which / tot items = max possible random distance variable.
Min distance can be left as set, but you will need to check that min < max now.

Granted that is true, You then just use the instance loop to place the objects similar to what you had so far.

Once you click the bluetility button within the properties panel, the objects will get distributed in the level like if you had manually placed them - and if you save they’ll stay put whenever you re-open…
Which for all intensive purposes makes the whole thing far more usable, and eliminates performance concerns as well.