The first thing that we’re going to do in this function is add a Return Node, and then add T In and T Out Transform variable pins to the Input and Return nodes, respectively, of this function.

The T In pin will be fed the T value from our Random Transform (Editable) function that we wrote, so drag that value in and create a non-editable transform variable out of it, and name it something like, “Comparative Transform.” The reason that we’re storing this into a temporary variable instead of just calling a random transform within this function, is because we need to create a random transform once, but then examine the values and weigh them against the values of the previously spawned instance, without generating a new transform every time we look for the information, as would happen if we nested the random transform function within this function. Doing this, within our order of operations in the Spawn Rocks function, will maintain the informational integrity of the transform info across our functions.

The next event delegation in this script is a branch node, but there’s a lot to define in what determines the yes/no value of the branch, so I’ll start from the left and work towards the right. The first thing that we want to do is get our HISM Array variable that we populated in the construction script. We need to use a Get (A Copy) node to get the previously used HISM component in our loop to find its most recent entry’s transform info. So in your Get (A Copy) node, in the index input, use a non-editable integer variable, and call it something like, “Last Used HISM.” This variable hasn’t actually been set yet, and will default to 0, which is fine, as the first spawn instance will use that 0 to look for a transform to compare against, see that there’s nothing there, and then go ahead with spawning, as it doesn’t have to worry about spawning too close to another instance.

In the case of instances spawning after the first actor, we need to get the transform of the last spawn instance to compare the location against, so we’ll drag off of the Output pin of our Get (A Copy) node, and create a Get Instance Transform node. Make sure that World Space is checked, as we’re comparing global locations, not relative. Since we need to determine which instance we’re comparing transform information to, we need to also create a Get Instance Count node from our Get (A Copy) node, which will return the max value (or most recent instance) of the given HISM component that was last active.

Now that we have which instance we’re going to weigh against for our differential spawn distance check, it’s time to extract the location of the instance from its transform, as well as the location of our most recent randomly generated transform, stored earlier in this function in the variable “Comparative Transform.” So we’re going to break the transforms, get the locations, subtract them from each other (Previous Instance Transform Location-Comparitive Transform Location) and get the Vector Length. Then we need to compare that length value to a Min Spawn Distance Between Instances editable float variable, and check to see if it’s greater than or equal to that definable value. If it is, the branch node that the return value for the ≥ node is feeding will move forward with spawning an instance, as it’s determined that the last spawn location is far enough away from the most recently generated spawn location. If it’s not, the branch will fire into a Set node for the # of Rocks to Spawn editable Integer variable, where the value is the # of Rocks to Spawn subtracted by 1, so that the script knows that it didn’t successfully spawn a mesh on this loop, and that it needs to go back and try again with another random transform.

Custom Spawn Rocks Function (Part 2)