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.
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.
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.
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.
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.
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
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.