Download

Array element age

Hey guys,

I’m at work and trying to wrap my head around building this function.

In my project I have some arrays which I manipulate often, for the most part I just add and remove items from them. What I want to implement is a check to remove an element that has been in the array for, lets say, 60s.

I am fairly new to unreal engine and it’s entirely possible there’s a simple node that does this check, but I wasn’t able to find anything suitable from my initial search online.

P.S. As a compromise I am thinking of building a function to remove i.e. 10% of elements from the begining of the array every 5seconds. The end result will be roughly the same, but ideally I’d want to have it set up properly.

Or limit the array to n elements and when a new element needs to be added to a full array, delete n0.

The last scenario you mentioned can be achieved through something like this:

Just set the value of MaxArraySize variable to be the maximum number of elements that your array can have.

Thanks stormrage! That looks very elegant.

Once I get home, I’ll fiddle around and try to figure out if I can track an elements time spent as part of the array.
It’s good to know I’ll have a backup waiting in case I can’t make it work.

What are you trying to achieve with this?

I thought of a work around to achieve the same result with a different-ish approach. I am about to test it and will post results. Below I will explain my original idea, if you care to read it and job your brain from a minute or two I would appreciate it, since I anticipate my solution to be a bit messy.

Original line of thought:

tl;dr
I want to display the average number of simultaneously active actors of class over the past minute.

I store all actors of class in an array. During gameplay when actors are destroyed I remove them from the array and when they spawn I add them in. I want to display on the screen the number of currently “active/alive” actors, which is easy enough, array length. Then I want to display the minimum and maximum simultaneously active actors (of a class) for the past minute.
i.e.
Active: 12
Min: 8
Max: 12
The reason I want to display min and max for last minute is that because during gameplay the average number of active actors can drastically change. Going from 12 to 50, will make the previous min value of 8 irrelevant as their range will be something like 45 - 53 and not 8 - 53. Vice versa for max value if the number of active characters decreases.
Now I could code a hard check and update min/max on drastic changes of number of characters, but I think doing this dynamically would look better. Whenever actor number changes, I store the array length into another array ( array of lengths/active actors). I get the lowest integer from that array and set it as min and highest as max. Each integer stays in the array for 60s and is then removed.

Ok, then don’t use an array :slight_smile:

All that matters is the number of active actors, you can hold that in an int.

Let me think a bit more…

So you have a count of the number of actors. You just have to make sure you ++ it when you spawn and – it when you kill:

Then you setup a time ( per second might be a bit much ) like this:

Every time this fires you update the log:

The sequence is 3 things:

  1. If the array length is > 60 ( we only want a minute ) take the oldest reading away

  2. Add the new reading at the end

  3. Print the average

And the average function:

OMG dude, this is amazing. I thought up the same approach of running an event every second, incrementing and checking for 60 to simulate storing a value for a minute, though I was trying work with arrays.
Also some guys I chatted with suggested using maps or structs, but I’ve never used them before and got a bit lost.
I’ll try your network and see how it goes

You also have to set the initial actor count:

To follow trough, I am using this and it works like a charm. I packaged everything into a function for ease of use between different classes.
I did end up making structs though not for saving timestamps of anything like that. Just needed to organize all the variable I use for this function.
I am have spawners which use arrays to keep track of the number of actors on spawn and destroy so I use those instead of incrementing integers.

Excellent :smiley: