Freezing due to construction script being spammed

G’day,

I’m pretty new to UE4 so please excuse my terminology.

I’ve got a C++ actor with an OnConstruction function which basically tears down the old object (if it existed) and recreates a new one (including a procedural mesh component and some UObjects). It works fine dragging the actor’s transform around… it rebuilds each time unnecessarily but that doesn’t really matter.

My issue is that when I ‘drag’ a float property that’s exposed to blueprint to increase/decrease it in the inspector, it freezes up the editor for a few secs (or longer depending on how long I drag for). I put some logging in to work out what’s going on and it seems that dragging for 2 seconds runs the construction script about 600 times without showing a frame (causing the editor to freeze for ~30 seconds). The same issue occurs editing an inherited blueprint or an actor in the level.

Is there a way to limit how often the OnConstruction function will be called when adjusting these properties? Even if it’s just limited to 1 per frame. Or is there somewhere else I should be performing this type of task?

Just to clarify: you are defining the object and its components in the constructor and then destroying it again? If this is the case you’d be far better of just destroying its components and re-instantiating them in the Tick function.

Though I still think tick is by far your best option, if you really want to achieve this in your constructor, you would have to use the timer to check if an instance should be created (float ms = UGameplayStatics::GetRealTimeSeconds(GetWorld()):wink: and if you wanted it to run, say, 50 times per second, check against the modulo of 20 and make sure it’s 0 (ie. if(!ms % 20){//do the destroy and recreate}. This is of course assuming that this script is running during your level gameplay. In the editor it won’t work. Best of luck!

@NikDouglas Cheers for the response. I’m destroying the old components and then recreating shared mesh and some other components. I was sort of trying to avoid having a Tick event handler built in to the the objects for performance reasons (even if it’s just if(bNeedsCreating) { rebuildobject() };, there could be thousands of these actors).

To be more specific, the actor is a house with a procedural mesh; with exposed blueprint properties like ‘height’ and ‘slope’ (not just transforms). If I drag to adjust the transforms in the editor, it probably recreates once per frame, so the FPS goes a little rough while dragging it but it’s fine; however when I do the same to adjust my custom properties such as height/slope, onconstruction event handles 60 times per second).

If using ‘Tick’ to check if the house needs to be recreated in this scenario is the best solution, I’ll accept it; I was just hoping I was missing a tick box or property somewhere to prevent onConstruction being called 300 times/sec while dragging custom properties. Logically it seems inefficient to run a function every ‘frame’ (I haven’t checked exactly when ‘tick’ is called); so perhaps I’m asking the wrong question; so let me rephrase:

If i’m creating a procedural mesh based on blueprint parameters (mainly changable in the editor but would prefer it to be set at runtime when the level/world starts); what is the best method to do so without spamming the recreation function causing a big freeze?

Using ‘tick’ fixes the issue anyway, thanks.