Optimizing construction scripts that dynamically create components

Hey all,

Looking to see if anyone out there has some suggestions on how I can make things run a bit better. The problem I am running into is performance of construction scripts that dynamically add components, when the number of components being added gets to be very large.

If during the construction script you add some components, those components obviously persist, in some manner between the times that the construction script is run. This is evident from the fact that they are not recreated every frame. If the construction script runs once and makes a bunch of components, then finished, those components continue to exist in the editor world for rendering purposes. This much is obvious.

However, each time the construction script runs, it seems all those components get completely wiped out and you have to recreate them if you wish to retain them. This isn’t that big of a problem for a few hundred, even a thousand or some components. But when you start having many thousands, recreating those components every single time, especially if the script is firing when say the user is simply transforming the actor, gets to be extremely extremely slow and hard to interact with.

Is there any possible way to somehow cache these so that they aren’t recreated unless explicitly told to do so? I thought about storing them in an array variable, but how would I actually put them back into the actor without incurring essentially the same overhead?

You could probably just have a variable that stores the amount of spawned components, and then when the construction script runs again, compare the current amount to spawn with the current spawned, and if the amount to spawn is greater than current spawned, spawn the difference of the two variables, otherwise remove some of the components by using an array.

Edit: I guess this actually won’t work considering that the components will be wiped out each time the construction script runs, I am guessing that the engine has some sort of built in thing that keeps track of how and where components were spawned, and the behavior is different if they were spawned in construction script.

Yeah that doesn’t work :slight_smile: It does work for all other variables, as long as they are editable the engine doesn’t wipe them out between runs, so I do exactly what you suggest. But for components, they get wiped out which is the whole problem.

Wanted to bump this up if I could because we are really running hard into this again :frowning:

Is there ANY way to get things like spawned HISM components to actually REMAIN between runs of the construction script? The penalty of recreating them every single change is absolutely killing performance :frowning: