Better way to find average location of child components.

Hi, I am trying to get the average location of a dynamically changing amount of child components (CCs).

I am unable to use the Get Actor Array Average Location as I have used a CC for my game as they allow of physics. As I thought the function should be doing a simple calculation I decided to make my own, however, it tanks performance with each CCs added.

The snippet above is the cause of the lower performance. Ball array is created and set in a previous loop. Once that loop completes it then runs the loop seen above. This occurs every tick and I think this could be described as a nested for loop?

The average vector has to be in event tick (to my knowledge) as it’s being used to update a camera’s position.

The child components are static mesh types btw.

Any help would be appreciated!

A ticks duration cannot end until all loops finish execution. The larger the loop, the longer the frame time.

I’d look into using sequential timers.

You can combine filling the Ball array and the average vector into one loop.
This should already give an increase in X2 performance (in theory, most likely less).

If this is not enough, you can make a function in C++. The cycles in it work several orders of magnitude faster.

Also try using the Get Actor Bounds function. Origin pin is the middle position between the extreme components in the actor.

1 Like

Optimize math to only do a division once. Or make it a variable that changes only when array size changes. Then it won’t even need to do division on ticks.

Please can you provide more information on this?
I.e. how I should be using the sequential timers

So I tried this an it indeed improved performance, however the average vector I get now is totally inaccurate.

I tried this and saw no substantial improvement honestly.

(post deleted by author)

Hi everyone I think I found the issue. My first for loop was adding each ball to an array. Each loop, the same balls was being added to the array, making it grow out of control with each loop.

Swapping from Array > Add to Array > AddUnique, improved the fps, but now I have a new issue. One of my variables is reading very diffrently based on which array adding method I’m using.

Camera distance is reading around 2000(on average) in my simulations when using Array > Add.
While when using Array > AddUnique, Camera distance is reading 5000 consistently, despite no other changes.

You need to Set the BallArray once by BeginPlay using Get Components by Tag.

image

The issue there, is my game is intended to be multiplayer with drop in/out functionality. Doing the check at the start would mean players who join will not be accounted for.

You can track logins and logouts from the server in game mode using the OnPostLogin and OnLogout events.

I don’t understand what you mean here. In this project, users spawn in a ball from the ball class. Would the ball array not be a set amount if it’s set at begin play?

Edit: It’s a local multiplayer game with drop in/on

If you create new balls in runtime, then simply add it to the array after that.
You don’t need use Get Components function at all.

P.S. OnPostLogin and OnLogout events also work in local multiplayer.

Ok I will look into the OnPostLogin/out stuff.

balls made do not allow for physics. So I added a static mesh component with physics enabled and am using that. Should I not be doing this?

You can call Set Simulate Physics for any balls-components at any time.

You talked about a dynamically changing number of balls, so I decided that you create them.
So I suggested that you add them to the array after creation. You can also enable physics for the created ball.
You will then have a fully up-to-date list of balls in the array every tick and be able to find the middle position you need.

I think there has been a misunderstanding. I am able to find the centre position and and add the balls to an array. The issue is, with one method the camera distance is set to 5000.

When changing nothing else but the array method. The camera distance behaving normally, but the FPS begins to decrease after some time.

You’re right, I still don’t fully understand everything that’s happening there…
But I want to help anyway

If performance drops, it means your array is growing. Is the amount of balls also increasing, or are you adding the same balls over and over again?
If you are adding over and over again, perhaps you should clear the array at the beginning of the tick?

And you should probably use Distance(Vector) instead of Vector Length.

image

Thank you for the advice, I’ve gone for using ‘Add Unique’ array element which solved the growing arrays.

I then needed to add 1 integer to a division I was using based on the array length. All is working and running smoothly now. Thanks for the help.