Array Updating

So in my game, there is a static object which using the mouse you constantly shift its world location. I cannot calculate how quick the static object “moves” since it technically doesn’t move. However (here comes a long explanation) I was told I could use an array of 5 indexes, which could hold various locations and collecting an average to try and calculate speed by using distance over time. So for example, the 1st index would be the latest location, and the 5th would be an earlier location. And every 0.10 seconds it would get the latest location of said object, changing the value of index 1 to the latest location, and making the previous index 1 location into index 2. Then, whatever was the 5th index location would be erased and replaced by the 4th index location. My problem is that I don’t actually know how to do this? I know how to change these various indexes and where to pull these locations that I would use, but how can I make it so it kind of creates a sort of queue of different locations? Perhaps there is an easier way of solving this than the method I’m talking about?

An easier explanation of what I’m trying to do, just in case it confuses anyone:

How can I “insert” an index 1, pushing all the previous indexes into higher number indexes while also deleting any index past 5?

Have you tried out the Insert node for arrays?

You can use it to add a new element at the specified index and then remove the final index if the array length becomes greater than 5.

Like this? Would this be correct??? Can it be done a better way than the way I’ve done it here?

Here’s an approach that works.

Here’s a modified version. Custom Function - Array Unshift posted by anonymous | blueprintUE | PasteBin For Unreal Engine 4

My assumption is the parent array will be empty on begin play. So sequence “then 0” needs a conditional branch checking length.

If Length > 0 then the array has at least 1 element that needs to be shifted. So we set the temp array and move on to Sequence “Then 1”.

Otherwise, we just insert the 1st trace hit location to the parent array and return (escaping the function).

Sequence “Then 1” assumes the Temp Array has at least 1 element. So we “Set Array Elem 0” as the trace hit location and “Set Array Elem 1” as temp array[0].

Next we conditionally check the length of the temp array and set the next element etc and so forth.


Yes this could’ve been done with loops. Yet Loops are tick bound meaning the loop has to finish before the tick can proceed. I try and stay away from loops as much as possible.

@Tacube Correct me if I’m wrong here. What I’ve understood is that you have an array of 5 elements. And now you want to insert a new element at index 0. As a result, what was previously at index 0 is now at index 1, the element previously at index 1 is now at index 2, and so on. Since there is a max cap of 5 elements for the array, the final element (at index 5) should be removed everytime a new element is inserted in to the array.

If that’s what you’re looking for, you just have to Get the “Object Locations” array, and call the “Insert” node with index = 0, & value = Trace Hit Location. All the elements starting from index 1 will be automatically shifted up by one index. The Insert node will take care of that for you on its own. The only thing left at this point, is to check if the array length is greater than 5. If it is, remove the element at index 5.

Are you positive it’ll auto shift and not simply overwrite index 0?

*Insert *inserts at index by shifting other elements *up, *expanding the array. If you want to replace an element - the Set Array Element node is what you’re after.

Yes, you can see it happening in this video:

Nice. Learned something new.